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

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


(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...


I have had the same need, and I have ended up using the package htmlTable, which is quite 'cost-efficient'. This creates a HTML table (in RStudio it is created in the "Viewer" windows in the bottom right which I just mark using the mouse copy-paste to Word. (Start marking form the bottom of the table and drag the mouse upwards, that way you are sure to include the start of the HTML code.) Word handles these tables quite nicely. The syntax of is quite simple involving just the function htmlTable(), but is still able to make somewhat more complex tables, such as grouped rows and primary and secondary column headers (i.e. column headers spanning more than one column). Check out the examples in the vignette.

One note of caution: htmlTable will not work will factor variables, i.e., they will come out as integer numbers (according to factor levels). So read the data using stringsAsFactors = FALSE or convert them using as.character().

Including trailing zeroes can be done using the txtRound function. Example:

mini_table <- data.frame(Name="A", x=runif(20), stringsAsFactors = FALSE)txt <- txtRound(mini_table, 2)

It is not completely straightforward to assign formatting soch as bold or italics, but it can be done by wrapping the table contents in HTML code. If you for instance want to make an entire column bold, it can be done like this (please note the use of single and double quotation marks inside paste0):

library(plyr)mini_table <- data.frame(Name="A", x=runif(20), stringsAsFactors = FALSE)txt <- txtRound(mini_table, 2)txt$x <- aaply(txt$x, 1, function(x)               paste0("<span style='font-weight:bold'>", x, "</span")              )htmlTable(txt)

Of course, that would be easier to to in Word. However, it is more interesting to add formatting to numbers according to some criteria. For instance, if we want to emphasize all values of x that are less than 0.2 by applying bold font, we can modify the code above as follows:

library(plyr)mini_table <- data.frame(Name="A", x=runif(20), stringsAsFactors = FALSE)txt <- txtRound(mini_table, 2)txt$x <- aaply(txt$x, 1, function(x)                if (as.numeric(x)<0.2) {                   paste0("<span style='font-weight:bold'>", x, "</span>")               } else {                  paste0("<span>", x, "</span>")               })htmlTable(txt)

If you want even fancier emphasis, you can for instance replace the bold font by red background color by using span style='background-color: red' in the code above. All these changes carry over to Word, at least on my computer (Windows 7).


The short answer is "not really." I've never had much luck getting well formatted tables into MS Word. The best approach I can offer you requires using Rmarkdown to render your tables into an HTML file. You can copy and paste you results from the HTML file to MS Word, but I make no guarantees about how well the formatting will follow.

To format your tables, you can try something like the xtable package, or the pixiedust package. But again, no guarantees that the formatting will transfer.