Create Word documents from R with R2DOCX
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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:
install.packages("devtools") devtools::install_github('R2DOC', 'davidgohel') devtools::install_github('R2DOCX', 'davidgohel') require(R2DOCX)
You can learn more about R2DOCX and download the source code at the link below.
GitHub (David Gohel): R2DOCX
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.