Nice tables when knitting to Word

knitr

#1

After trying xtable, huxtable, and others, I have settled on using knitr::kable and kableExtra to format REALLY nice looking tables in HTML and PDF output. However, people still enjoy/insist on editing documents in Word, so I typically provide both to collaborators. Since I toggle between the different outputs, I often use:
doc.type <- knitr::opts_knit$get('rmarkdown.pandoc.to') to define the type of document, then format tables using an if statement like:
if (doc.type == "docx") { pander(df) } else { kable(df) }
However, the tables look terrible in Word and often require formatting before distribution to avoid people saying “that table looks horrible”.

What other packages/functions might produce more aesthetically pleasing tables without too much fuss?

Thanks!


#2

I use David Gohel’s Flextable package which works with his officeR package to provide formatting for direct output to the MS Office applications (see overview of Flextable at https://davidgohel.github.io/flextable/articles/overview.html).

The downside up until now of doing so is that it outputs to Word in a very different way than knitr/Markdown does, which meant abandoning markdown output for reporting purposes if I wanted to use Flextable, but I think the development version available from the author’s Github page does allow for markdown output.

Here’s a screenshot of one of the tables I output using Flextable in a quarterly performance report so you can see what can be done with this excellent package:

Table

One approach I haven’t tried as yet is to use Markdown to output to a Word document then to use Flextable to finalise the tables in the document by replacing placeholders inserted by Markdown or at specific bookmarks. The officeR package allows for opening a Word document, going to named bookmarks within, and replacing the contents. It’s on my to-do list to try at some stage.


#3

YES! This seems to work great. Requires pandoc version > 2.0 (which I had to upgrade, apparently I had 1.19) but then it worked just fine. Excellent documentation/examples.

Works in Markdown (critically)), but I’ve yet to figure out whether it supports Latex formatting. For now it just shows the raw latex. I’ll post back if I discover that option.

Thanks for the tip!


#4

For me personally, I will recommend you send out your work report in HTML so that when your collaborators need the table in Word, they can always do the copy and pasting by themselves. I’m not saying this workflow is the perfect solution but in my case, it solves the problem and for you as a data scientist/statistician, the only extra thing you need to do is to educate your team about this. In most cases, you don’t need to open up Word and copy the tables by yourself.

At my workplace, after I show them the benefit of using HTML (like a clickable table of contents, some dynamic plots and now this kind of copiable tables), most of people have already brought in this workflow. I use PDF when the report is for external people where they want a “formal” and stable document.

https://cran.r-project.org/web/packages/kableExtra/vignettes/kableExtra_and_word.html

The programmers of Microsoft Word did an amazing job on transforming HTML to XML and then to Word. We should take advantage of that.


#5

Hao,
Also a great suggestion! So far, I’ve been the lead on most documents generated this way, so providing a Word output for editing and a PDF for final figure/table layout has been sufficient, but if my collaborators are leading the effort in Word (which would be a likely scenario), I will aim to keep my portion of the work dynamic and reproducible in an Rmarkdown/HTML workflow and provide that to them. It’s nice to know that the lovely tables generated by kable/kableExtra are portable to Word! Flextable also works well, but doesn’t handle LaTeX formatting, and it doesn’t sound like it will, based on this Stack Overflow discussion.
Cheers,
Kevin