Create a Word document from a template file using R software and ReporteRs package

This tutorial describes how to create a Word document based on an existing one using R software and Reporters package. In this case, your final Word document is generated using the layout and the styles from the template file.

This approach is useful in many situations :

  • If you work in a corporate environment, you may need sometimes to generate Word documents based on a template with specific fonts, color, logos, etc.
  • If you want to insert R outputs in an existing Word document.
  • If you want to use text formatting styles from a given template file.

Read and write a Word document from a template using R software and ReporteRs package

Before reading this article you should take a look at my first post : Create and format Word documents using R software and Reporters package.

Quick introduction to ReporteRs package

ReporteRs package provides simple functions to quickly generate and format a word document from R software. It can be used as follow :

# Create a Word document
doc <- docx()
# Add a title
doc <- addTitle(doc, "Example of a Word document from R software", level=1)
# Add paragraph
doc <- addParagraph(doc, "This Word document has been generated from R software using ReporteRs package.")
# Add plots
doc <- addTitle(doc, "Plots", level=1)
doc <- addPlot(doc, function() hist(iris$Sepal.Width, col=4) )
doc <- addPageBreak(doc) # go to the next page
# Add table
doc <- addTitle(doc, "Table", level=1)
doc <- addFlexTable(doc, vanilla.table(iris[1:10,]))
# Write the word document to a file
writeDoc(doc, file="r-reporters-word-example.docx")

The Word document created by the R code above is available here : R software and ReporteRs package - Example of a Word document

Create a Word document using a template file

A template file can be specified to the docx() function as follow :

# Create a word document
doc <- docx(template="path/to/your/word/template/file.docx")
# ...............
# Add contents
# ...............
# Write the Word document to a file 
writeDoc(doc, file = "output-file.docx")

In the R code below, a Word document template is downloaded from STHDA website and used to write a report :

# Download a Word document template from STHDA website
    destfile="r-reporters-word-document-template.docx", quiet=TRUE)
# Create a Word document using the downloaded template
doc <- docx(title="R software and ReporteRs package",
# Add titles
doc <- addTitle(doc, "Word document created from a template",
# Add an introduction
doc <- addTitle(doc, "Introduction", level=2) # Add a sub title
doc <- addParagraph(doc, "This Word document is created from a template using R software and ReporteRs package.")
# Add  a table
doc <- addTitle(doc, "Iris data sets", level=2)
doc <- addFlexTable(doc, FlexTable(iris[1:10,]))
doc <- addTitle(doc, "Description of iris data sets", level=2)
doc <- addParagraph(doc, "iris data set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica.")
# Add a page break : go to next page
doc <- addPageBreak(doc)
# Add a plot into the Word document
doc <- addTitle(doc, "Nice bar plot")
doc <- addPlot(doc, function() barplot(1:5, col=1:5))
# Write the Word document to a file 
writeDoc(doc, file = "r-reporters-word-document-from-template.docx")
# Remove the downloaded template file
ok <- file.remove("r-reporters-word-document-template.docx")

The Word document created by the R code above is available here : R software and ReporteRs package - Word document created from a template

Note that, the function docx() can take two arguments : a title argument (title of the document, appearing only in the Word document properties) and a template argument (to specify template file).


This analysis has been performed using R (ver. 3.1.0).

You can read more about ReporteRs and download the source code at the following link :

GitHub (David Gohel): ReporteRs

Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.

Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!

This page has been seen 39045 times