WordR package enables creating MS Word files (.docx)
from given MS Word template. It can evaluate inline R code, insert
tables and plots.
WordR package is technically a wrapper
around a powerful package
R language has many ways for producing state-of-the-art reports. For
Sweave packages are very
effective in preparing reports in PDF format or R2wd for creating a MS
Word report from scratch. However, such techniques has some
R2wd are limited and with
(LateX knowledge required).
On the other hand,
WordR package, enables
WordR package is useful, for example, when
you need to
All examples, and instructions given in this document applies to MS Word 2013. No major differences are expected to other versions (as of 2017).
First we need to create a template (.docx) file. Such file may contain any formatting MS Word allows. Apart from that, the template may contain two other things:
Examples can be found in the examples folder of the package.
This functionality enables including simple R expression(s) in the Word document, which is evaluated during file rendering. The result of the expression need to be a string of length one (or something coercible to character(1)). Line breaks cannot be inserted in this way. Because of used workflow, each R inline expression need to be a separate paragraph. However, MS Word offers a way how to do an inline paragraph. Steps for creating an inline expression:
is preferable to have formatting symbols visible by clicking the “new
line” sign button on
Home>Paragraph panel in MS
`r expression`, where
expression is R
Styles menu. (Do not select the r code as that would change
the style for the text not the whole r inline code paragraph)
As a result you should see something like this:
Example can be seen in file
"): MS Word is, by default (maybe depends on local
settings), changing the quotes to ‘Smart quotes’. You can change it the
MS Word settings under
Proofing > AutoCorrect Options > Autoformat as you type...
You can de-select Straight Quotes as Smart Quotes and that should revert
the quotes to being straight. Or when typing, right after you type a
quote, if Word turns it into a “smart quote,” immediately press
renderInlineCode function, and make sure its output is
character of length one.
menu in MS Word.
Example and more info about bookmarks in MS Word can be seen in file
To render a table on a given place, just insert a bookmark with name
t_XYZ where XYZ will be a name of a flextable
To render a plot on a given place, just insert a bookmark with name
p_XYZ where XYZ will be a name of plot function.
Functions for rendering the MS Word file are the main content of the WordR package. Typical rendering R script contains following steps:
renderInlineCode on the prepared template
body_add_flextables on the file resulting from
addPlots on the file resulting from previous
Alternatively, from v0.3.3
renderAll function is
available, which is a wrapper around those three rendering
Examples can be seen in