From an R console (R >= 3.0):
install.packages("devtools") devtools::install_github('R2DOC', 'davidgohel') devtools::install_github('R2DOCX', 'davidgohel')
Dependencies
R packages : rJava Java (it has been tested with java version >= 1.6)
Creating Microsoft Word documents is easy and can be done in few lines of R code. Below a commented R script:
library( R2DOCX ) # Here we define word document filename to write docx.file = "document.docx" # Creation of doc, a Docx object doc = new("Docx", title = "My example" ) # add into doc first 10 lines of iris doc = addTable( doc, iris[1:10,] ) # add text with stylename "Normal" into doc doc = addParagraph( doc, value = "Hello World!", stylename = "Normal" ) # add a plot into doc doc = addPlot( doc, function() plot( rnorm(10), rnorm(10) ) , width = 10, height = 8 ) # write the doc writeDoc( doc, docx.file )
When creating a Docx object, a base file is used. This file is the original docx document that will be completed with R ouputs. This file will be copied "in memory".
Available styles will be those available in the base file. Tables and paragraphs formats can be customize with dedicated functions.
To create a docx document from R, use:
doc = new("Docx", title = "My example" )
To add R outputs in a docx document, use one of these functions:
addPlot
for adding plots.addParagraph
for adding paragraphs of text.addTable
for adding tables.addHeader
for adding headers.addTOC
for adding tables of contents.addPageBreak
for adding a page break.addLineBreak
for adding a line break.To write the docx document on to the disk:
writeDoc( doc, "my_example.docx" )