Coverting R output table to editable Word document

335 Views Asked by At

Does anyone know how to covert an R table output to an editable word document? For reference I tried the following code for my table, but seem to be getting an XML error. Any help would be appreciated.

Code

2

There are 2 best solutions below

1
Muhammed Ahmed Abd Al Aleam On

The best way and simpler one I found is to:

1- save your data frame as .CSV file using R code but you shouldn't save the indexes also like this:
write.csv(table1, "table1.csv", row.names= F)

2- Open the .CSV file using Microsoft word by right-click and open with then choose word app

3- Click ctrl + a then click on Table>Convert>Conevrt text to table you'll find this dialoge box:convert text to table dialouge box

click OK.

Some notes: after doing this you can save the word file as XML file and you can design it as you want
Scources: https://www.rforecology.com/post/exporting-tables-from-r-to-microsoft-word/

2
Daniel D. Sjoberg On

You tagged the gtsummary package, so I presume you're using it to create the table?

There are various ways to convert a gtsummary table to Word output. Review this vignette for more details. https://www.danieldsjoberg.com/gtsummary/articles/rmarkdown.html

library(gtsummary)

trial %>%
  tbl_summary(by = trt)  %>%
  as_gt() %>%
  gt::gtsave(filename = "my_table.docx")