13.6 MS Word via knitr/R Markdown

When moving from a .R file to a Markdown (.Rmd) file, environment objects such as tables or data frames / tibbles usually require to be saved and loaded to R Markdown document.

In RStudio, select:
File > New File > R Markdown

A useful template file is produced by default. Try hitting knit to Word on the Knit button at the top of the .Rmd script window. If you have difficulties at this stage, refer to chapter 13.

Now paste this into the file (we’ll call it Example 1):

Knitting to Microsoft Word from R Markdown. Before (A) and after (B) adjustment.

FIGURE 13.2: Knitting to Microsoft Word from R Markdown. Before (A) and after (B) adjustment.

Knitting this into a Word document results in Figure 13.2A), which looks pretty decent but the some of the columns need some formatting and the plot needs resized. Do not be tempted to do this by hand directly in the Word document.

Yes, before Markdown, we would have to move and format each table and figure directly in Word, and we would repeat this every time something changed. Turns out some patient records were duplicated and you have to remove them before repeating the analysis over again? Or your colleague forgot to attach an extra file with 10 more patients?

No problem, you update the dataset, re-run the script that created the tables and hit Knit in the R Markdown document. No more mindless re-doing for you. We think this is pretty amazing.