Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 358
mandy Avatar asked Dec 13 '25 18:12

mandy


2 Answers

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.

like image 54
Rafael Díaz Avatar answered Dec 16 '25 11:12

Rafael Díaz


The sjPlot has updated its functions with the important file parameter with which you can specify a .doc file (note: not .docx).

So to save the sum_tab dataframe, you merely write:

sjPlot::tab_df(sum_tab, file = "output.doc")
like image 34
Agile Bean Avatar answered Dec 16 '25 10:12

Agile Bean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!