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 two other great and powerful packages - officer
and ReporteRs
.
R language has many ways for producing state-of-the-art reports. For example rmarkdown
or Sweave
packages are very effective in preparing reports in PDF format. However, such techniques has some drawbacks:
rmarkdown
are limited and with Sweave
complicated (LateX knowledge required).On the other hand, WordR
package, enables
To conclude, 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:
This functionality enables including simple R expression(s) (almost) anywhere 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)). Because of limitation in officer
package, line breaks does not works. Because of used workflow, functions etc. the R 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 (in a new document):
Ctrl+Alt+Enter
(it is preferable to have formatting symbols visible by clicking the “new line” sign button on Home>Paragraph
panel in MS Word)#rXXXX expression @{style}r#
, where XXXX is a numeric unique code block identification (must be unique across whole document), expression
is R expression like 1+1
. Last part @{style}
is optional argument specifying the MS Word style in which the expression should be evaluated (for details see help for function renderInlineCode
).As a result you should see something like this:
As this is tedious, you can create a VBA macro for this:
ALT+F11
in MS Word)put the code below e.g. to a newly created module in Normal template (https://word.tips.net/T000109_Writing_a_Macro_from_Scratch.html):
Sub InsertInlineRCode()
'
' InsertInlineRCode Macro
'
'
Selection.TypeParagraph
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.InsertStyleSeparator
Selection.TypeBackspace
rn = Round(1000000 * Rnd())
Selection.TypeText Text:="#r" + Trim(Str(rn)) + " 1+1 r#"
Selection.TypeParagraph
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.InsertStyleSeparator
Selection.TypeBackspace
Selection.MoveLeft Unit:=wdCharacter, Count:=17, Extend:=wdExtend
Selection.Range.HighlightColorIndex = wdYellow
End Sub
assign a button to this macro (https://wordribbon.tips.net/T006011_Adding_a_Macro_to_the_Quick_Access_Toolbar.html), so you can easily create a new R code block with one click
Example can be seen in file examples/templates/template1.docx
.
R Inline code chunk ID: XXX not found. Pls reidentificate!(retype the \"#rXXXXX[space]\")
error: Because of the way how officer
reads the document (and is looking up strings/paragraphs) the characters identifying the code chunk needs to be in a single element of the XML in which the MS Word stores the document. If this error occur, please delete everything between the beginning of the code chunk (style separator) and the expression and then type #rXXXX
- where XXXX is a number specifying the code chunk; and mind the space after that. Do not change the style of any part this part of code chunk as that would split the text into separate elements in XML.renderInlineCode
function, and make sure its output is character of length one.Example and more info about bookmarks in MS Word can be seen in file examples/templates/templateFT.docx
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 (ReporteRs::FlexTable
) table.
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 (ReporteRs::addPlot.docx
).
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 fileaddFlexTables
on the file resulting from previous stepaddFlexPlots
on the file resulting from previous stepExamples can be seen in examples/examples.R
.