Create Word documents from R with R2DOCX

June 28, 2013

(This article was first published on Revolutions, and kindly contributed to R-bloggers)

If you work in a corporate environment, then the Word document format is still pretty ubiquitous. Personally, I've switched to Google Docs for most of my own documents, but I still receive a couple of Words docs from others via email every day. And it's common request from clients of our Services team to generate documents in the Word format in a particular corporate template, but also including output (tables and figures) from R

You can generate Word documents from R using knitr and pandoc, but the Word output it generates is pretty vanilla. If you need to generate a Word document based on template with fonts, logos, etc. you might want to take a look at the R2DOCX package by David Gohel. It lets you start with a Word template and then with a few lines of R you can:

  • Replace keywords in the document programmatically (think: "AUTHOR" or "DATE")
  • Insert formatted tables based on R data (including support for coloring table cells, controlling fonts etc.)
  • Insert R graphics as charts
  • Programmatically insert paragraphs, line breaks, etc.

Take a look at the Word document embedded below as a sample of the output you can create. The base template file was created ahead of time in Word, with the author, chart, tables and charts inserted with this R script.

To get started wth R2DOCX, you can install it from GitHub with these R commands:

devtools::install_github('R2DOC', 'davidgohel')
devtools::install_github('R2DOCX', 'davidgohel')

You can learn more about R2DOCX and download the source code at the link below.

GitHub (David Gohel): R2DOCX

To leave a comment for the author, please follow the link and comment on their blog: Revolutions. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


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)