Export Cox regression results to excel or word using publish package

856 views Asked by At

Looking at the code below

library(pec)
data(GBSG2,package="pec")
setDT(GBSG2)
GBSG2

library(survival)
library(prodlim)
library(Publish)

cox_lung <- coxph(Surv(time,cens)~tgrade+age+strata(menostat)+tsize+pnodes+progrec+estrec,data=GBSG2)
publish(cox_lung,org=TRUE)

Output:

> publish(cox_GBSG2,org=TRUE)
|  Variable | Units | HazardRatio |       CI.95 |   p-value |
|-----------+-------+-------------+-------------+-----------|
|    tgrade |     I |         Ref |             |           |
|           |    II |        1.86 | [1.14;3.03] |   0.01276 |
|           |   III |        2.24 | [1.32;3.78] |   0.00263 |
|       age |       |        0.99 | [0.97;1.01] |   0.27167 |
|     tsize |       |        1.01 | [1.00;1.01] |   0.06029 |
|    pnodes |       |        1.05 | [1.04;1.07] |   < 0.001 |
|   progrec |       |        1.00 | [1.00;1.00] |   < 0.001 |
|    estrec |       |        1.00 | [1.00;1.00] |   0.70522 |

I'd like to know if there is an easy way to export the bove output to a CSV, word or excel file.

1

There are 1 answers

4
IRTFM On BEST ANSWER

Here's a way to get a csv file using capture.output and gsub. I'm displaying the intermediate steps as well:

cox_out <- capture.output( publish(cox_lung,org=TRUE) )

# Demonstration of intermediates (don't need to do these

gsub("[|]", ",",cox_out)   # Change "|" to commas
 [1] ",  Variable , Units , HazardRatio ,       CI.95 ,   p-value ,"
 [2] ",-----------+-------+-------------+-------------+-----------,"
 [3] ",    tgrade ,     I ,         Ref ,             ,           ,"
 [4] ",           ,    II ,        1.86 , [1.14;3.03] ,   0.01276 ,"
 [5] ",           ,   III ,        2.24 , [1.32;3.78] ,   0.00263 ,"
 [6] ",       age ,       ,        0.99 , [0.97;1.01] ,   0.27167 ,"
 [7] ",     tsize ,       ,        1.01 , [1.00;1.01] ,   0.06029 ,"
 [8] ",    pnodes ,       ,        1.05 , [1.04;1.07] ,   < 0.001 ,"
 [9] ",   progrec ,       ,        1.00 , [1.00;1.00] ,   < 0.001 ,"
[10] ",    estrec ,       ,        1.00 , [1.00;1.00] ,   0.70522 ,"
[11] ""                                                             
gsub("[|]", ",",cox_out)[-2]   # Remove second line
 [1] ",  Variable , Units , HazardRatio ,       CI.95 ,   p-value ,"
 [2] ",    tgrade ,     I ,         Ref ,             ,           ,"
 [3] ",           ,    II ,        1.86 , [1.14;3.03] ,   0.01276 ,"
 [4] ",           ,   III ,        2.24 , [1.32;3.78] ,   0.00263 ,"
 [5] ",       age ,       ,        0.99 , [0.97;1.01] ,   0.27167 ,"
 [6] ",     tsize ,       ,        1.01 , [1.00;1.01] ,   0.06029 ,"
 [7] ",    pnodes ,       ,        1.05 , [1.04;1.07] ,   < 0.001 ,"
 [8] ",   progrec ,       ,        1.00 , [1.00;1.00] ,   < 0.001 ,"
 [9] ",    estrec ,       ,        1.00 , [1.00;1.00] ,   0.70522 ,"
[10] ""                                                             
gsub( "^,|,$", "", gsub("[|]", ",",cox_out)[-2] ) # wrap second gsub to take out leading and trailing commas
 [1] "  Variable , Units , HazardRatio ,       CI.95  ,   p-value "
 [2] "    tgrade ,     I ,         Ref ,             ,           "
 [3] "           ,    II ,        1.86 , [1.14;3.03] ,   0.01276 "
 [4] "           ,   III ,        2.24 , [1.32;3.78] ,   0.00263 "
 [5] "       age ,       ,        0.99 , [0.97;1.01] ,   0.27167 "
 [6] "     tsize ,       ,        1.01 , [1.00;1.01] ,   0.06029 "
 [7] "    pnodes ,       ,        1.05 , [1.04;1.07] ,   < 0.001 "
 [8] "   progrec ,       ,        1.00 , [1.00;1.00] ,   < 0.001 "
 [9] "    estrec ,       ,        1.00 , [1.00;1.00] ,   0.70522 "
[10] ""                                
#                         
cat( paste( gsub( "^,|,$", "", gsub("[|]", ",",cox_out)[-2] ), collapse="\n"), 
       # need paste( ... , colapse="\n") to append linefeeds at each line.
     file="cox_out.csv" )

Rather than using cat perhaps would have been cleaner to use write.table (since it avoids needing to add the linefeeds "by hand"):

cox_out <- capture.output( publish(cox_lung,org=TRUE) )
write.table( gsub( "^,|,$", "", gsub("[|]", ",",cox_out)[-2] ), file="cox_out.csv" )