[Rmarkdown] Automate word document generation using Rmarkdown

[This article was first published on R on Zhenguo Zhang's Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Zhenguo Zhang’s Blog /2024/01/28/rmarkdown-automate-word-document-generation-using-rmarkdown/ –

I knew that Rmarkdown was very power to generate all kinds of document formats such as html, pdf, word, powerpoint, etc, but until recently, I have never used it to generate word documents (most time, I generated html and pdf files).

When I started to generate word documents using Rmarkdown, I found that it was not so straightforward. The main issue is how to specify the different text styles to different texts, such as “Heading 1”, “Body Text”, etc.

After some research, I found that there are two major ways to apply styles to texts.

  • Use a customized word template

  • Use officedown/officer packages

Use a customized word template

To use this approach, one need to follow the following steps:

  1. generate a word document using Rmarkdown, and try to include all the elements you will use in the template. The content isn’t important.

  2. then open the word document and change each style to the designed format. For example, you can change the color and font of “Heading 2” and then update the style to match the new setting. One can find more details here. Note that it is very important: don’t change the text style name, because this name will be reused by new documents generated from Rmarkdown. Then save this document as a template, say, “my_template.docx”.

  3. create a new Rmarkdown document, and use the elements you used in the step 1. and set parameter ‘reference_docx’ to the path of the template in the YAML header, like the following one:

title: "Fancy Word Report"
author: "my Name"
    reference_docx: path/to/my_template.docx
  1. Finally, knit the Rmarkdown and get the new output. In this new document, all the styles should follow the same one as set in the template.

To apply a specific style to certain text, one can use the so-called fenced_divs syntax, as one example:

::: {custom-style="Heading 3"}
This is my paragraph with new
custom style.

One can find more examples about this syntax at https://www.andreashandel.com/posts/2020-10-07-custom-word-format/.

Use officedown/officer packages

To set styles using the packages, one can use the following functions:

  • officer::ftext("text", prop=ft_prop), here we can use the parameter prop to control the color, font, boldness of text, which in turn is formatted with the function fp_text_lite().

  • officer::fpar(..., fp_p=fp_par()) to control the layout of paragraphs, such as centering texts. The formatting is controlled by the function fp_par().

These two functions allow one to specify any formats to any text chunks in a paragraph or for a whole paragraph. Below is an example:

ft_text<-fp_text_lite(color="red", bold=T, font.size=15, font.family="Calibri (Body)")
fp_center<-fp_par(line_spacing = 1.5, text.align="center")
fpar(ftext("Website: https://google.com/", ft_text), fp_p=fp_center)

Note that one need to specify the output format as officedown::rdocx_document.

In fact, one can combine the styles set in a word template with the styles set with the officer package, and the following YAML header gives an example:

title: "officedown output"
    toc: false
    reference_docx: my_template.docx

Any text not set by fpar() or ftext() will inherit the styles from the word template.

I will give some examples in the future posts.

Happy programming 😄


- /2024/01/28/rmarkdown-automate-word-document-generation-using-rmarkdown/ -
To leave a comment for the author, please follow the link and comment on their blog: R on Zhenguo Zhang's Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)