R2doc Tutorial
R2doc Tutorial
Features
Installation
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)
Getting Started
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"
Page 1
R2DOC TUTORIAL
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" )
Page 2
R2DOC TUTORIAL
Template
You may need to generate docx documents in a particular corporate template (with specific fonts, colour schemes, logos, etc.).
Prepare a docx file that will be used as template (or use an existing one).
require( R2DOCX )
# Word document to use as base document or a template
template.file = "D:/templates/My_Template.docx"
# Create a new object
doc = new("Docx", title = "example", basefile = template.file )
That's it. This file is then duplicated in memory. R outputs sent to the object 'doc' will go into that copy.
By default, an empty document is used (located into the package directory).
Page 3
R2DOC TUTORIAL
Styles
Available styles are the paragraph styles contained in the template.
require( R2DOCX )
# Create a new object with the default template
doc = new("Docx", title = "example" )
styles( doc )
# [1] "Normal"
"Titre1"
"Titre2"
# [4] "Titre3"
"Titre4"
"Titre5"
# [7] "Titre6"
"Titre7"
"Titre8"
# [10] "Titre9"
"Titre"
"Sous -titre"
# [13] "Citation"
"Citationintense"
"Lgende"
"Paragraphedeliste"
# [19] "PlotReference"
"En-tte"
"Pieddepage"
# [22] "Titre10"
"BulletList"
"Titre20"
# [25] "TitleDoc"
Paragraphs
Function
addParagraph
require parameter
stylename
Page 4
styles .
R2DOC TUTORIAL
Headers
Function
addHeader
need to know which styles are corresponding to which title level (1 ; 1.1 ; 1.1.1 ; etc.).
When template is read, R2DOCX try to guess what are theses styles. If he do not succeed, you will see that error when addHeader will be called:
Error in addHeader(...
You must defined header styles via setHeaderStyle first.
setHeaderStyle
"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 )
Page 5
R2DOC TUTORIAL
Tables of contents
Functions addTOC has an optional argument stylename , if used, a table of contents will be created with entries formated with style specified. An
example of usage is a table of contents of the produced graphics:
doc = addPlot( doc, function() plot( rnorm(10), rnorm(10) )
, width = 10, height = 8, legend="graph example 1"
, stylename="PlotReference" )
doc = addPlot( doc, function() plot( rnorm(100), rnorm(100) )
, width = 10, height = 8, legend="graph example 2"
, stylename="PlotReference" )
doc = addTOC( doc, stylename="PlotReference" )
Warning
Do not use styles names that contains special character. In France for example, the default style named "Legend" becomes "Lgende" and is
returned as "Lgende" by the styles R function. The workaround is to create a new style based on "Lgende" and to name it "Legende". It will be
then a valid name to use with R2DOCX.
Formating tables
You can define table formating properties. It is first necessary to understand the model implemented :
grouped header row 1
Page 6
R2DOC TUTORIAL
column header 1
column header 2
column header p
data[1,1]
data[1,2]
data[1,...]
data[1,...]
data[1,p]
data[...,1]
data[...,2]
data[...,...]
data[...,...]
data[...,p]
data[n,1]
data[n,2]
data[n,...]
data[n,...]
data[n,p]
In few words:
the number of digits to the left of the decimal point for numeric data and percent data.
the minimum number of digits to the right of the decimal point of double data or the minimum number of digits to display for integer data.
percent symbol to add after data of reporting type 'percent'
Page 7
R2DOC TUTORIAL
Simple example
This code
# let's format header labels with a bold font and a centered paragraph
# also, change the number of digits to the left of the decimal point for double data
myformat = tableProperties(
header.text = textProperties(font.weight="bold")
, header.par = parProperties( text.align = "center" )
, fraction.double.digit = 4
)
doc = addTable( doc, iris[1:5,], formats = myformat )
3.5000
1.4000
0.2000 setosa
4.9000
3.0000
1.4000
0.2000 setosa
4.7000
3.2000
1.3000
0.2000 setosa
4.6000
3.1000
1.5000
0.2000 setosa
5.0000
3.6000
1.4000
0.2000 setosa
textProperties
Page 8
R2DOC TUTORIAL
Use function
textProperties
Argument name
Details
Expected Value
color
font color
font-size
font size in px
font-weight
font weight
normal or "bold"
font-style
font style
normal or "italic"
font-family
a string value : must be an available font name, e.g. Arial, Times, etc.
parProperties
Use function
parProperties
Page 9
R2DOC TUTORIAL
Argument name
Details
Expected Value
text-align
text alignment
padding-bottom
padding-top
padding-left
padding-right
Details
Expected Value
padding
integer value : 0>= value. It sets all paddings to the same value.
Page 10
R2DOC TUTORIAL
cellProperties
Use function
cellProperties
Argument name
Details
Expected Value
border.bottom.color
border.bottom.style
border.bottom.width
border.left.color
border.left.style
border.left.width
Page 11
R2DOC TUTORIAL
Argument name
Details
Expected Value
border.top.color
border.top.style
border.top.width
border.right.color
border.right.style
border.right.width
vertical.align
Page 12
R2DOC TUTORIAL
Argument name
Details
Expected Value
padding.bottom
padding.top
padding.left
padding.right
background.color
padding
Details
Expected Value
integer value : 0>= value. It sets all paddings to the same value.
Page 13
R2DOC TUTORIAL
Argument
name
Details
Expected Value
border.width
integer value : 0>= value. It sets all border widths to the same value.
border.style
tableProperties
Use function
tableProperties
Argument name
Details
Expected Value
Page 14
R2DOC TUTORIAL
Argument name
Details
Expected Value
header.text
textProperties
header.par
parProperties
header.cell
cellProperties
groupedheader.text
textProperties
groupedheader.par
parProperties
groupedheader.cell
cellProperties
double.text
textProperties
double.par
parProperties
Page 15
R2DOC TUTORIAL
Argument name
Details
Expected Value
double.cell
cellProperties
integer.text
textProperties
integer.par
parProperties
integer.cell
cellProperties
percent.text
textProperties
percent.par
parProperties
percent.cell
cellProperties
character.text
textProperties
Page 16
R2DOC TUTORIAL
Argument name
Details
Expected Value
character.par
parProperties
character.cell
cellProperties
date.text
textProperties
date.par
parProperties
date.cell
cellProperties
datetime.text
textProperties
datetime.par
parProperties
datetime.cell
cellProperties
Page 17
R2DOC TUTORIAL
Argument name
Details
Expected Value
percent.addsymbol
character
integer.digit
minimum number of digits to display (to the left of the decimal point for data of type
percent and double)
fraction.double.digit
minimum number of digits to display to the right of the decimal point (for data of type
double)
fraction.percent.digit
minimum number of digits to display to the right of the decimal point (for data of type
percent)
data.cell
Details
Expected Value
Page 18
R2DOC TUTORIAL
Argument
name
Details
Expected Value
data.par
data.text
Page 19
R2DOC TUTORIAL
my.formats = tableProperties(
character.par = leftPar
, percent.par = rightPar
, double.par = rightPar
, integer.par = rightPar
, groupedheader.par = centerPar
, groupedheader.text = header.textProperties
, groupedheader.cell = header.cellProperties
, header.cell = header.cellProperties
, header.par = centerPar
, header.text = header.textProperties
, data.cell = data.cellProperties
, data.text = data.textProperties
)
Page 20
R2DOC TUTORIAL
Functions
and
addImage
bookmark , if
used, content (table, plots, paragraphs or images) will replace the whole paragraph
Warning
A paragraph in the template document can only contain one bookmark. Function
values in paragraphs.
addParagraph
Complete example
We are expecting the following output file document.docx
require( R2DOCX )
Page 21
R2DOC TUTORIAL
# create document
doc = new("Docx", title = "My example", basefile = template.file )
# replace bookmarks 'DATA' and 'CONFINT' located in 'ttest_example.docx' by data.frame objects 'data' and 'conf.int'
doc = addTable( doc
, iris[5:10,]
Page 22
R2DOC TUTORIAL
, bookmark = "DATA" )
doc = addParagraph( doc, value = c( "Header 1" ), stylename = "NAMESTYLE", bookmark = "COLNAME1" )
doc = addParagraph( doc, value = c( "Header 2" ), stylename = "NAMESTYLE", bookmark = "COLNAME2" )
doc = addParagraph( doc, value = c( "Header 3" ), stylename = "NAMESTYLE", bookmark = "COLNAME3" )
doc = addParagraph( doc, value = c( "Row name 1" ), stylename = "NAMESTYLE", bookmark = "ROWNAME1" )
doc = addParagraph( doc, value = c( "Row name 2" ), stylename = "NAMESTYLE", bookmark = "ROWNAME2" )
doc = addParagraph( doc, value = c( "Hello World!" ), stylename = "DATASTYLE", bookmark = "ANYDATA" )
Page 23
R2DOC TUTORIAL
if( interactive() ) {
out = readline( "Open the docx file (y/n)? " )
if( out == "y" ) browseURL( file.path(getwd(), docx.file ) )
}
Plots
Function
addPlot
will capture plots into png files and insert them into the output document with eventually a legend text below graphics.
Its main argument is a function. The plotting is done by this function. If this functio n requires parameters, add them as parameters to
the addPlot function (the ... argument).
doc = addPlot( doc , plot
, x = rnorm( 10 ), y = rnorm( 10 ), main = "main title"
, legend = "graph example", stylename="PlotReference"
)
Page 24
R2DOC TUTORIAL
If manipulating
written).
lattice
or
ggplot2
To combine multiple plots, simply create a function to execute all plotting instructions:
doc = addPlot( doc , function( ) {
print( qplot(Sepal.Length, Petal.Length
, data = iris, color = Species, size = Petal.Width ) )
plot( x = rnorm( 10 ), y = rnorm( 10 ), main = "main title" )
}
, legend = "graph example", stylename="PlotReference"
)
Text outputs
Simple texts output can be done with function
addParagraph .
Argument
stylename
Page 25
lattice
or
ggplot2
objects won't be
R2DOC TUTORIAL
The function can also be used to execute text replacement and conditional formatting. If replacements have to be done, keywords that identify
text to be replaced must be contains into square brackets [].
Replacement texts has to be formated with
textProperties
objects.
# template text
x = "[animal] eats [food]."
Page 26
R2DOC TUTORIAL
Table of contents
Insert a table of contents with function
addTOC .
Insert a customized table of contents with argument stylename . If used, a table of contents will be created containing entries formated with
specified styles. An example of usage is a table of contents of the produced graphics:
doc = new("Docx", title = "My example" )
doc <- addTOC(doc, stylename = "PlotReference")
doc = addHeader(doc, "Title 1", 1);
doc = addHeader(doc, "Title 1.1", 2);
doc = addPlot( doc
, fun = plot, x = rnorm( 100 ), y = rnorm (100 )
, main = "base plot main title"
Page 27
R2DOC TUTORIAL
Page breaks
Insert a page break with
addPageBreak .
External images
Insert an external image with
addImage .
Page 28
R2DOC TUTORIAL
Header labels
Specify column labels to display with argument
header.labels
desc( iris )
#'data.frame':
150 obs. of
5 variables:
# $ Sepal.Length: num
# $ Sepal.Width : num
3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
# $ Petal.Length: num
1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
# $ Petal.Width : num
0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
# $ Species
doc = addTable( doc , iris[1:5, ]
, header.labels = list(
Page 29
R2DOC TUTORIAL
3.50
1.40
0.20 setosa
4.90
3.00
1.40
0.20 setosa
4.70
3.20
1.30
0.20 setosa
4.60
3.10
1.50
0.20 setosa
5.00
3.60
1.40
0.20 setosa
Page 30
R2DOC TUTORIAL
Petal
3.50
1.40
0.20 setosa
4.90
3.00
1.40
0.20 setosa
Page 31
R2DOC TUTORIAL
Sepal
Petal
3.20
1.30
0.20 setosa
4.60
3.10
1.50
0.20 setosa
5.00
3.60
1.40
0.20 setosa
Argument
grouped.cols is a
named list:
Names are labels to display, list elements are character vector containing data column names to gather under one label.
All columns names must be in exactly one element.
Order of appearance in elements must be the order of columns names in the dataset.
E.g. if columns are 'col1', 'col2', 'col3', argument cannot be:
list ('colgroup1'='col2', 'colgroup2'=c('col1', 'col3'))
To leave blank headers labels, name the element the same that the column name:
doc = addTable( doc, iris, grouped.cols=list( ..., "Species" = "Species" ) )
span.columns
Page 32
R2DOC TUTORIAL
, span.columns = "Species"
)
Species Sepal.Length Sepal.Width Petal.Length Petal.Width
setosa
versicolor
4.80
3.00
1.40
0.30
5.10
3.80
1.60
0.20
4.60
3.20
1.40
0.20
5.30
3.70
1.50
0.20
5.00
3.30
1.40
0.20
7.00
3.20
4.70
1.40
6.40
3.20
4.50
1.50
6.90
3.10
4.90
1.50
5.50
2.30
4.00
1.30
6.50
2.80
4.60
1.50
col.fontcolors
col.colors
Page 33
R2DOC TUTORIAL
, iris[ 1:5, ]
, col.fontcolors = list( Species = c("red", "blue"
, "red", "blue", "red" ) )
, col.colors = list( Sepal.Length = c("red", "blue"
, "red", "blue", "red" ) )
)
3.50
1.40
0.20 setosa
4.90
3.00
1.40
0.20 setosa
4.70
3.20
1.30
0.20 setosa
4.60
3.10
1.50
0.20 setosa
5.00
3.60
1.40
0.20 setosa
Formating tables
details
Complete example
doc = addTable( doc
Page 34
R2DOC TUTORIAL
, 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",
77.17
0.53%
10 2013-02-05
CLJ
58.06
0.68%
13 2013-03-19
DAB
70.52
0.32%
6 2013-01-29
DAG
70.30
0.84%
16 2013-03-19
Page 35
R2DOC TUTORIAL
Summary
Subj ect Identifier Av erage Weight Regularity Number of v isits Last v isit
ETP
67.79
0.79%
15 2013-03-19
GUC
71.80
0.37%
7 2013-03-19
JBI
83.25
0.42%
8 2013-03-05
JCB
93.10
0.58%
11 2013-03-19
JLM
76.12
0.47%
9 2013-03-19
JUI
66.68
0.63%
12 2013-03-19
MAB
66.74
0.42%
8 2013-01-15
MIG
102.95
0.21%
4 2013-01-08
NIR
74.05
0.63%
12 2013-03-19
REN
69.90
0.58%
11 2013-02-26
SEH
79.88
0.63%
12 2013-02-26
TIB
73.84
0.53%
10 2013-03-05
Page 36