WordR Package"

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 officer.

Motivation

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 or R2wd for creating a MS Word report from scratch. However, such techniques has some drawbacks:

On the other hand, WordR package, enables

To conclude, WordR package is useful, for example, when you need to

How to use the WordR package

All examples, and instructions given in this document applies to MS Word 2013. No major differences are expected to other versions (as of 2017).

Template preparation

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.

Creating inline R code

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:

As a result you should see something like this: Inline Code

Example can be seen in file examples/templates/template1.docx.

Troubleshooting the inline code

Inserting bookmarks for Plots or Tables

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 (flextable::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.

Template rendering

Functions for rendering the MS Word file are the main content of the WordR package. Typical rendering R script contains following steps:

Alternatively, from v0.3.3 renderAll function is available, which is a wrapper around those three rendering functions.

Examples can be seen in examples/examples.R.



Try the WordR package in your browser

Any scripts or data that you put into this service are public.

WordR documentation built on Jan. 13, 2023, 1:12 a.m.