Create a formatted contingency table for Word using R

One of the bigger challenges I have had with R is getting output into a shareable format. There are helpful packages, such as gmodels, that can produce nice-looking contingency tables in the console, but getting the results out of the console and into Word is difficult. For example, gmodels can produce a table like this…

   Cell Contents
|-------------------------|
|                       N |
| Chi-square contribution |
|           N / Row Total |
|           N / Col Total |
|         N / Table Total |
|-------------------------|

 
Total Observations in Table:  248 

 
                 | infert$induced 
infert$education |         0 |         1 |         2 | Row Total | 
-----------------|-----------|-----------|-----------|-----------|
          0-5yrs |         4 |         2 |         6 |        12 | 
                 |     1.232 |     0.506 |     9.898 |           | 
                 |     0.333 |     0.167 |     0.500 |     0.048 | 
                 |     0.028 |     0.029 |     0.162 |           | 
                 |     0.016 |     0.008 |     0.024 |           | 
-----------------|-----------|-----------|-----------|-----------|
         6-11yrs |        78 |        27 |        15 |       120 | 
                 |     1.121 |     1.059 |     0.471 |           | 
                 |     0.650 |     0.225 |     0.125 |     0.484 | 
                 |     0.545 |     0.397 |     0.405 |           | 
                 |     0.315 |     0.109 |     0.060 |           | 
-----------------|-----------|-----------|-----------|-----------|
         12+ yrs |        61 |        39 |        16 |       116 | 
                 |     0.518 |     1.627 |     0.099 |           | 
                 |     0.526 |     0.336 |     0.138 |     0.468 | 
                 |     0.427 |     0.574 |     0.432 |           | 
                 |     0.246 |     0.157 |     0.065 |           | 
-----------------|-----------|-----------|-----------|-----------|
    Column Total |       143 |        68 |        37 |       248 | 
                 |     0.577 |     0.274 |     0.149 |           | 
-----------------|-----------|-----------|-----------|-----------|

…which is nice, but doesn’t work well for Word.

I found this wonderful solution as suggested by David Gohel in this forum comment post. (David is also the author of the ReporteRs package).

First, install the necessary packages if you don’t have them:

install.packages("broom")
install.packages("rtable")
install.packages("ReporteRs")

Here is a complete code provided by David that produces a cross tab in Word. Below the code is an example of what the output looks like.

library( ReporteRs )
library( rtable )
library( broom )

data(infert, package = "datasets")
myft = freqtable(table(infert$education, infert$induced))
ct = chisq.test(infert$education, infert$induced)

mydoc = docx(title = "Summary")
mydoc = addTitle(mydoc, "Table", level = 2)
mydoc = addFlexTable( mydoc, myft )
mydoc = addTitle(mydoc, "Chi-squared Test", level = 2)
mydoc = addFlexTable( mydoc, vanilla.table( tidy(ct) ) )
writeDoc( mydoc, file = "Summary.docx")

The end results produces a Word document that looks like the below. (If you are not sure where your file saved, use the getwd() command to see what the current active directly is for R.)

Beautiful. This is the kind of functionality that I wish R had “out of the box” rather than needing to stitch together different packages.

Sample output from ReporteRs.
Sample output from ReporteRs.
Tags