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 :

#install.packages("ReporteRs")
library("ReporteRs")
# 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
download.file(url="http://www.sthda.com/sthda/RDoc/example-files/r-reporters-word-document-template.docx",
    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",
            template="r-reporters-word-document-template.docx")
# Add titles
doc <- addTitle(doc, "Word document created from a template",
                level=1) 
# 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).

Infos

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









Want to Learn More on R Programming and Data Science?

Follow us by Email

by FeedBurner

On Social Networks:


 Get involved :
  Click to follow us on and Google+ :   
  Comment this article by clicking on "Discussion" button (top-right position of this page)
  Sign up as a member and post news and articles on STHDA web site.
This page has been seen 8376 times