Creating Tables for Microsoft Word Output with R Markdown
R Markdown
I have switched to solely use R Markdown for the communication step of my tidy work flow and found it pleasant and actually prodocutive to use. However, there is one major hassle: output in Word format. In a perfect world, I would never need to use any Microsoft products ever again. In reality when I need to work with collaborators that do not use RMarkdown and funders of research projects that require reports in Word format (and a required style template), there are no other options. As a matter of fact, it seems that the only viable solution collaborating with anybody that doesn’t use RMarkdown and/or git is still MS Word as of 2017/10.
Rendering R Markdown file to word_document works for the most part, except for sophisticated tables, which, unfortunately, are prevalent in most of my work.
Possible Solutions
There are a few possible solutions, none of them good enough for me at this point:
Gmisc::docx_document(the output is a HTML file with an additional CSS file; no support for Word style template).- Convert HTML output to Word document through
soffice(tables in the output do not resemble the HTML input; some support for template). - Pure R solution
officer+flextable(they can generate good-looking Word output, but they do not work with R Markdown yet; see below).
My solution/hack (as of 2017/10)
- Render the R Markdown file with output set to
rmarkdown::word_document. - The R Markdown file uses
texreg/htmlregto generate HTML tables for regression results and saves them into separate HTML files during rendering (one for each table). - Convert HTML to DOCX:
- Via Word COM through the
py32winmodule (the option used). - Via docx-html-js.
- Via
soffice:soffice --headless --convert-to docx:"MS Word 2007 XML" --writer --outdir . Table\ 1.html.
- Via Word COM through the
- Patch the rendered
word_documentand replace placeholder tables with actual tables.
Steps and files
rmarkdown::render("reg_tables_demo.Rmd")python html2docx.py tabs/(requiringpy32winand Microsoft Word on a Windows PC; tested with MS Office 2016 and Python 2.7).source("patch_docx_tables.R"); patchFlexTables("reg_tables_demo.docx", "tabs/", "reg_tables_demo_final.docx")- Final reg_tables_demo.docx.
All files and scripts used can be found on GitHub.
Alternatives / Things to keep an eye on
broom>huxtable>flextable>pandoc (2.0).This solution directly
knitr::knits R Markdown files into Word, embedding flextables through XML chunks. It eliminates the need to run scripts outsideknitand is a more elegant solution than the hack above. At the time of writing, it was not yet acceptable for several reasons:- Pandoc 2.0 had not yet been released, so users needed to build it from source.
- The
flextableimplementation proposed by Maxim Nazarov had not yet been incorporated intoflextable, though his code should work already. broomsupported fewer model objects thantexreg, althoughbroom+huxtableseemed like a better long-term fit with the tidy philosophy andbroom’s inclusion in the tidyverse.
pandoc, when it improves support for sophisticated tables.At the time, table support in Pandoc was rudimentary, in particular with no support for table row/column spans, which is what sent me down this rabbit hole. The issue had been open for four years, and it was unclear when it would be resolved.
Credits
- Carsten Behring for the idea and code for the Word patching hack.
- Max Gordon.