Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy export and table formatting of R dataframe to Word? [closed]

Tags:

r

ms-word

Is there an easy way to automatize the conversion of a R dataframe to a pretty Word table in APA format for publishing manuscripts? I'm currently doing this by saving the table in a csv, opening that in excel, copying the excel table to Word, and formatting it there, but I'm hoping there would be a way to automatize the formatting in R, so that when I convert it to Word, it would already be in APA format, because Word sucks in automatization.

Basically, I want to continue writing the manuscript itself in Word, while doing my analyses in R. Then gather all the results in R to a table (with manually modifiable formatting) by a script and convert it to whatever format I could then simply copy-paste to Word (so that the formatting actually holds). When I need to modify the table, I would make the changes in R and then just run the script again without the need to do any changes in Word.

I don't want to learn LaTeX, because everyone in my field uses Word with features like track changes, and I use Zotero add-in for citations, so it's simpler to just keep the writing separate from the analyses. Also, I am a psychologist, not a coder, so learning a lot of new technologies just for this is probably not worth the effort for me. Typically with new technologies come new technical problems, and I am aiming to make my workflow quicker, but not at the cost of unpredictability (which may make it slower exactly at the moment when I cannot afford it).

I found a R+knitr+rmarkdown+pander+pandoc solution "with as little overhead as possible", but it seems to be quite heavy still because I don't know any of those technologies apart from R. And I'm not eager to start learning all that, as it seems to be aimed for doing the writing and all in R to the very end, while I want to separate my writing and my code - I never need code in my writing, only the result tables. In addition, based on the examples, it seems to fetch the values directly from R code (e.g., from summary() to create a descriptive table), while I need to be able to tinker with my table manually before converting it, for instance, writing the title and notes (like a specific note to one cell and explaining it in the bottom). I also found R2wd, but it seems to be an older attempt for the same "whole workflow in R" problem as the solution above. SWord does not seem to be working anymore.

Any suggestions?

like image 940
RandomMonitor Avatar asked Aug 14 '15 13:08

RandomMonitor


People also ask

How do you copy a table in R?

The code write. table(x, "clipboard", sep="\t") will copy a table x to the clipboard in such a way that it can be pasted into Excel preserving the table structure. By default, the row and column names will come along with the table contents.


1 Answers

(Just to let you know, I am the author of the packages I recommend you...)

You can use package ReporteRs to output your table to Word. See here a tutorial (not mine): http://www.sthda.com/english/wiki/create-and-format-word-documents-using-r-software-and-reporters-package

Objects FlexTable let you format and arrange tables easily with some standard R code. For example, to set the 2nd column in bold, the code looks like:

myFlexTable[, 2] = textBold()

There are (old) examples here: http://davidgohel.github.io/ReporteRs/flextable_examples.html

These objects can be added to a Word report using the function addFlexTable. The word report can be generated with function writeDoc.

If you are working in RStudio, you can print the object and it will be rendered in the html viewer so you can export it in Word when you are satisfied with its content.

You can even add real Word footnotes (see the link below) http://davidgohel.github.io/ReporteRs/pot_objects.html#pot_footnotes

If you need more tabular output, I recommend you also the rtable package that handles xtable objects (and other things I have to develop to satisfy my colleagues or customers) - a quick demo can be seen here:

http://davidgohel.github.io/tabular/

Hope it helps...

like image 75
David Gohel Avatar answered Oct 20 '22 19:10

David Gohel