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.
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).
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
Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
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!
Recommended for You!
Recommended for you
This section contains best data science and self-development resources to help you on your path.
Coursera - Online Courses and Specialization
- Course: Machine Learning: Master the Fundamentals by Standford
- Specialization: Data Science by Johns Hopkins University
- Specialization: Python for Everybody by University of Michigan
- Courses: Build Skills for a Top Job in any Industry by Coursera
- Specialization: Master Machine Learning Fundamentals by University of Washington
- Specialization: Statistics with R by Duke University
- Specialization: Software Development in R by Johns Hopkins University
- Specialization: Genomic Data Science by Johns Hopkins University
Popular Courses Launched in 2020
- Google IT Automation with Python by Google
- AI for Medicine by deeplearning.ai
- Epidemiology in Public Health Practice by Johns Hopkins University
- AWS Fundamentals by Amazon Web Services
- The Science of Well-Being by Yale University
- Google IT Support Professional by Google
- Python for Everybody by University of Michigan
- IBM Data Science Professional Certificate by IBM
- Business Foundations by University of Pennsylvania
- Introduction to Psychology by Yale University
- Excel Skills for Business by Macquarie University
- Psychological First Aid by Johns Hopkins University
- Graphic Design by Cal Arts
Books - Data Science
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet
Want to Learn More on R Programming and Data Science?
Follow us by Email On Social Networks: