R2DOCX : Create a word document with R



Two ways to generate a word document. Either with Knitr and pandoc or with R2DOCX.

Pandoc : Convert Markdown documents to other formats



For more information click here

The pandoc() function is included in the knitr package (version 1.2) and it could be used to convert Markdown documents to other formats such as Latex/PDF, HTML and Word (. Doc,. Docx).

To install pandoc click-here

How to use pandoc()

Code R :
 
library(knitr)
pandoc('foo.md', format='html')  # HTML
pandoc('foo.md', format='latex') # LaTeX/PDF
pandoc('foo.md', format='docx')  # MS Word
pandoc('foo.md', format='odt')   # OpenDocument
 



Create a Word document with R2DOCX



Using R2DOCX package you can, create easly a Word document from a template or not and add a plot or table to the document.

A comprehensive documentation is available here
An example is shown here


Installation




Compatibility R >= 3.0

Code R :
 
install.packages("devtools")
devtools::install_github('R2DOC', 'davidgohel')
devtools::install_github('R2DOCX', 'davidgohel')
 


For Mac users :

Code R :
 
Sys.setenv(NOAWT=1) #prevents usage of awt.[http://cran.r-project.org/doc/manuals/r-devel/R-admin.html#Java-_0028OS-X_0029]
install.packages("devtools")
devtools::install_github('R2DOC', 'davidgohel')
devtools::install_github('R2DOCX', 'davidgohel')
 




The available functions






Your first document




Code R :
 
Sys.setenv(NOAWT=1) #prevents usage of awt - required on Mac
library(R2DOCX)
 
# The file to create
docx.file <- "document.docx"
# Docx object to manipulate
doc <- new ("Docx", title = "My example" )
#Add a table
doc = addTable( doc, iris[1:10,] )
# Add atext
doc <- addParagraph( doc, value = "Hello World!", stylename = "Normal" )
# Add a plot
doc = addPlot( doc, function() plot( rnorm(10), rnorm(10) )
               , width = 10, height = 8
)
# Write the file
writeDoc( doc, docx.file )
 



Create a Word document from template



Code R :
 
# Document to create
docx.file = "document.docx"
# Word document used as template
template.file = file.path( find.package("R2DOCX"), "templates/TEMPLATE_01.docx", fsep = "/" )
# Create an object
doc = new("Docx"
	, title = "My example" 
	, basefile = template.file
)
# 2 keywords exist in the base file- Let's replace it
doc = replaceText( doc, pattern = "AUTHOR", replacement = "John Doe" )
doc = replaceText( doc, pattern = "DATE", replacement = date() )
 




Main title




Code R :
 
#Main title
#**************************************
doc = addParagraph( doc, value = "Exemple", stylename = "TitleDoc" )
doc = addPageBreak( doc ) #saut de page
 





Table of contents




Code R :
 
#Table of content : TOC
#**************************************
#(user will be asked to update this TOC  when document will be opened the first time)
doc = addHeader(doc, "Table des matières", 1);
doc = addTOC(doc)
doc = addPageBreak( doc )#saut de page
 





Add texts (Normal et list)




Code R :
 
# Add texts (Normal et list)
#**************************************
doc = addHeader(doc, "Dataset presentation", 1);
#Normal style
doc = addParagraph( doc, value = "Dataset 'measured.weights' is set of weights measurements collected on few people before savate training.", stylename = "Normal" )
#Unordered list
texts = c( "Column 'gender' is the subject gender." 
           , "Column 'id' is the unique subject identifier." 
           , "Column 'cat' is the subject weight category." 
           , "Column 'competitor' tells wether or not subject practice competition." 
           , "Column 'day' is the day of measurement." 
           , "Column 'weight' is the measured weight." 
)
doc = addParagraph( doc, value = texts, stylename = "BulletList" )
 





Conditionnal formating of the text




The addParagraph function is used to simply add text as shown above. It can also be used to replace text and to make conditional formatting in the document.

Code R :
 
# template text
x = "[animal] eats [food]."
# define formatting properties for replacement text – see chapter "styles"
textProp = textProperties( color = "blue" )
replacement.styles = list( animal= textProp, food= textProp )
# define replacement text
replacements = list( animal = "donkey", food = "grass" )
 
doc <- addParagraph( doc, value = x, stylename = "Normal"
                     , replacements = replacements
                     , replacement.styles = replacement.styles
)
 

..


textProperties function
Code R :
textProperties(color = "black", font.size = 10
		, font.weight = "normal", font.style = "normal"
		, font.family = "Arial" )


color : font color; font.size: size in pixel; font.weight : "normal" ou "bold"; font.style: "normal" ou "italic"; font.family: font family.


Add a table




Simple table:

Code R :
 
# Add a table
#**************************************
# Used data
data( measured.weights )
doc = addHeader(doc, "Dataset", 1);
doc = addHeader(doc, "Dataset first lines", 2);
#Simple table
doc = addTable( doc
                , data = head( measured.weights, n = 10 )
                , formats = get.light.formats()
)
doc = addLineBreak( doc )#Line break
 




Rename table headers:

Before you rename:

Code R :
 
doc = addTable( doc, data = weights.summary)	
 




After you rename :

Code R :
 
doc = addTable( doc
		, data = weights.summary
		, header.labels = list("id" = "Subject Identifier", "avg.weight" = "Average Weight"
				, "regularity" = "Regularity", "visit.n" = "Number of visits", "last.day" = "Last visit"
				) # columns labels to display
		)
 




Simple table customized with merged rows :

In the table below, the rows are merged on the 'day' column. The column 'gender' is colored by gender.

Code R :
 
# simple addTable with row merging on 'day' column
doc = addHeader(doc, "Dataset last lines", 2);
sampledata = tail( measured.weights[ order(measured.weights$day), ], n = 20 )
doc = addTable( doc
		, data = sampledata
		, span.columns = c("day")
		, col.colors = list( "gender" = ifelse( sampledata$gender == "male" , "#00c2ff", "#ffcdd1") )
)
 




Customized table with grouped headers:

In this table, headers have been renamed, grouped headers are created, the type of columns is specified (%, integre, character, decimal, date). The values ​​of the "Regularity" column were colored according to the value (gray if <0.5 and blue if> = 0.5).

Code R :
 
# customized table
doc = addHeader(doc, "Dataset summary", 1);
doc = addTable( doc
		, data = weights.summary
		, header.labels = list("id" = "Subject Identifier", "avg.weight" = "Average Weight"
				, "regularity" = "Regularity", "visit.n" = "Number of visits", "last.day" = "Last visit"
				) # columns labels to display
		, grouped.cols=list( "id" = "id"
				, "Summary" = c( "avg.weight",  "regularity", "visit.n", "last.day" ) 
				) # grouped headers line to add before headers line
		, col.types = list( "id" = "character", "avg.weight" = "double"
				, "regularity" = "percent", "visit.n" = "integer"
				, "last.day" = "date" ) # reporting types of each columns 
		, col.fontcolors = list( "regularity" = ifelse( weights.summary$regularity < 0.5 , "gray", "blue") ) # customized font colors for column "regularity"
)
 
 





Add a plot




Code R :
 
# Add a plot
#**************************************
doc = addPageBreak( doc )
doc = addHeader(doc, "Graphics", 1);
doc = addPlot( doc,
               function() boxplot(measured.weights$weight ~ measured.weights$id,
                                  xlab = "Subjects", ylab = "Weights" , col = "#0088cc",
                                  legend = "My boxplot"),
               width=7, height=7
              )
 





Text replacement




To replace the keyword "AUTHOR" ,in the word document, with "Pearson":

Code R :
 
doc = replaceText( doc, "AUTHOR", "Pearson")
 



Heading styles available in your document




Code R :
 
doc <- new("Docx", title = "My example" )
styles( doc )
# [1] "Normal"                  "Titre1"                  "Titre2"                 
# [4] "Titre3"                  "Titre4"                  "Titre5"                 
# [7] "Titre6"                  "Titre7"                  "Titre8"                 
#[10] "Titre9"                  "Policepardfaut"          "TableauNormal"          
# ...
doc <- setHeaderStyle(doc, stylenames = c("Titre1", "Titre2", "Titre3", "Titre4", "Titre5", "Titre6", "Titre7", "Titre8", "Titre9" ) ) 
doc = addHeader( doc, "title 1", 1 )
 




A full word document




The full script used to create this word document can be downloaded here: R2DOCX.r

A full word document