rms-wordreportexport-to-word

Export data to table in Word format from R


I have encountered this problem for multiple times. I want to export a summary data set that I made from R to table in Word. The best I can do for now is to first export the data to Excel, and then copy the table in Excel to word.

My sample data:

> sum_tab
        col1 col2 col3
2    move up   10   10
3  no change    4    9
1  move down   12    7
21   move up   11    5
31 no change    4   16
11 move down   11    5
22   move up    9    6
32 no change   10   14
12 move down    7    6

Export to Excel:

library(xlsx)
write.xlsx(sum_tab, file = "sum_tab.xlsx")

Is there a neat way to export the sum_tab data to table in Word with 10 rows and 4 columns?


Solution

  • You can use one of these two options, use rmarkdown or the sjPlot package

    sum_tab = data.frame(col1 = c("move up","no change", "move down", "move up", "no change","move down","move up","no change","move down"), 
                         col2 = c(10,4,12,11,4,11,9,10,7), col3 = c(10,9,7,5,16,5,6,14,6))
    row.names(sum_tab) <- c(2,3,1,21,31,11,22,32,12)
    sum_tab
    library(sjPlot)
    tab_df(sum_tab)
    

    In the viewer you can select the table with the cursor and paste it in Word.