0% found this document useful (0 votes)
126 views36 pages

R2doc Tutorial

The R2DOC TUTORIAL document provides information on how to use the R2DOC and R2DOCX packages to generate Microsoft Word documents (.docx files) directly from R. It describes features for adding tables, plots, text, and tables of contents from R into Word documents. It also provides details on installation, dependencies, customizing formatting and styles, and using templates.

Uploaded by

Trina Miller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views36 pages

R2doc Tutorial

The R2DOC TUTORIAL document provides information on how to use the R2DOC and R2DOCX packages to generate Microsoft Word documents (.docx files) directly from R. It describes features for adding tables, plots, text, and tables of contents from R into Word documents. It also provides details on installation, dependencies, customizing formatting and styles, and using templates.

Uploaded by

Trina Miller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

R2DOC TUTORIAL

Features

Create docx files with only a few lines of R code.


Add tables, plots, texts or tables of contents into a word document.
R functions are available for customizing formatting properties of R output.

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

# 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:

for adding plots.


addParagraph for adding paragraphs of text.
addPlot

Page 2

R2DOC TUTORIAL

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.
addTable

To write the docx document on to the disk:


writeDoc( doc, "my_example.docx" )

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"

# [16] "En-ttedetabledesmatires" "Sansinterligne"

"Paragraphedeliste"

# [19] "PlotReference"

"En-tte"

"Pieddepage"

# [22] "Titre10"

"BulletList"

"Titre20"

# [25] "TitleDoc"

Paragraphs
Function

addParagraph

require parameter

stylename

whose value must be one of the names returned by the function

doc = addParagraph( doc, value = "Normal text", stylename = "Normal" )

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.

You have to use function

setHeaderStyle

to indicate which available styles are meant to be used as header styles.

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 )

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

grouped header row p

Page 6

R2DOC TUTORIAL

column header 1

column header 2

column header ...

column header ...

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:

Headers are columns labels displayed


Grouped headers is a row preceding headers where columns labels can be gathered under a label. This row is optional.
Each column has one of the following reporting types : double, integer, character, percent, date, datetime.
Data will be formated conditionnaly to the reporting types of each columns, for example, all numeric columns will be formated the same

Elements that can be formated are cells, paragraphs of text, texts


Options are available to define:

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 )

should produces the following result


Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1000

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

to specify formating text properties.

Argument name

Details

Expected Value

color

font color

a string value (e.g. "#000000" or "black")

font-size

font size in px

an integer value : 0>= value

font-weight

font weight

normal or "bold"

font-style

font style

normal or "italic"

font-family

font family name (Arial, Times, etc.)

a string value : must be an available font name, e.g. Arial, Times, etc.

textProperties(font.size = 10, font.weight = "bold" )


textProperties(font.size = 10, font.weight = "bold", color = "red" )

parProperties
Use function

parProperties

to specify formating paragraph properties.

Page 9

R2DOC TUTORIAL

Argument name

Details

Expected Value

text-align

text alignment

a string value : "left" or "right" or "center" or "justify"

padding-bottom

space after paragraph in px

an integer value : 0>= value

padding-top

space before paragraph in px

an integer value : 0>= value

padding-left

space on the left of paragraph in px

an integer value : 0>= value

padding-right

space on the right of paragraph in px

an integer value : 0>= value

An additional argument is available as shortcut:


Argument name

Details

Expected Value

padding

cell bottom, top, left and right padding

integer value : 0>= value. It sets all paddings to the same value.

parProperties(text.align = "center", padding = 3 )


parProperties(text.align = "right", padding.left = 4, padding.right = 4)

Page 10

R2DOC TUTORIAL

cellProperties
Use function

cellProperties

to specify formating cell properties.

Argument name

Details

Expected Value

border.bottom.color

border bottom color

a string value (e.g. "#000000" or "black")

border.bottom.style

border bottom style

a string value : "single" or "none" or "hidden" or "double" or "dotted" or "dashed" or


"inset" or "outset"

border.bottom.width

border bottom width

an integer value : 0>= value

border.left.color

border left color

a string value (e.g. "#000000" or "black")

border.left.style

border left style

a string value : "single" or "none" or "hidden" or "double" or "dotted" or "dashed" or


"inset" or "outset"

border.left.width

border left width

an integer value : 0>= value

Page 11

R2DOC TUTORIAL

Argument name

Details

Expected Value

border.top.color

border top color

a string value (e.g. "#000000" or "black")

border.top.style

border top style

a string value : "single" or "none" or "hidden" or "double" or "dotted" or "dashed" or


"inset" or "outset"

border.top.width

border top width

an integer value : 0>= value

border.right.color

border right color

a string value (e.g. "#000000" or "black")

border.right.style

border right style

a string value : "single" or "none" or "hidden" or "double" or "dotted" or "dashed" or


"inset" or "outset"

border.right.width

border right width

integer value : 0>= value

vertical.align

cell content vertical


alignment

a string value : "center" or "top" or "bottom"

Page 12

R2DOC TUTORIAL

Argument name

Details

Expected Value

padding.bottom

cell bottom padding

integer value : 0>= value

padding.top

cell top padding

integer value : 0>= value

padding.left

cell left padding

integer value : 0>= value

padding.right

cell right padding

integer value : 0>= value

background.color

cell background color

a string value (e.g. "#000000" or "black")

Additional arguments are available as shortcuts:


Argument
name

padding

Details

Expected Value

cell bottom, top, left and


right padding

integer value : 0>= value. It sets all paddings to the same value.

Page 13

R2DOC TUTORIAL

Argument
name

Details

Expected Value

border.width

cell bottom, top, left and


right border width

integer value : 0>= value. It sets all border widths to the same value.

border.style

cell bottom, top, left and


right border style

a string value : "single" or "none" or "hidden" or "double" or "dotted" or "dashed" or


"inset" or "outset". It sets all border styles to the same value.

cellProperties( border.color = "gray" )


cellProperties(border.left.width = 0, border.right.width = 0
, border.bottom.width = 2, border.top.width = 0
, padding.bottom = 2, padding.top = 2
, padding.left = 2, padding.right = 2 )

tableProperties
Use function

tableProperties

Argument name

to specify formating table properties.

Details

Expected Value

Page 14

R2DOC TUTORIAL

Argument name

Details

Expected Value

header.text

header text formatting properties

textProperties

header.par

header paragraph formatting properties

parProperties

header.cell

header cell formatting properties

cellProperties

groupedheader.text

grouped header text formatting properties

textProperties

groupedheader.par

grouped header paragraph formatting properties

parProperties

groupedheader.cell

grouped header cell formatting properties

cellProperties

double.text

"double" columns formatting text properties

textProperties

double.par

"double" columns formatting paragraph properties

parProperties

Page 15

R2DOC TUTORIAL

Argument name

Details

Expected Value

double.cell

"double" columns formatting cell properties

cellProperties

integer.text

"integer" columns formatting text properties

textProperties

integer.par

"integer" columns formatting paragraph properties

parProperties

integer.cell

"integer" columns formatting cell properties

cellProperties

percent.text

"percent" columns formatting text properties

textProperties

percent.par

"percent" columns formatting paragraph properties

parProperties

percent.cell

"percent" columns formatting cell properties

cellProperties

character.text

"character" columns formatting text properties

textProperties

Page 16

R2DOC TUTORIAL

Argument name

Details

Expected Value

character.par

"character" columns formatting paragraph properties

parProperties

character.cell

"character" columns formatting cell properties

cellProperties

date.text

"date" columns formatting text properties

textProperties

date.par

"date" columns formatting paragraph properties

parProperties

date.cell

"date" columns formatting cell properties

cellProperties

datetime.text

"datetime" columns formatting text properties

textProperties

datetime.par

"datetime" columns formatting paragraph properties

parProperties

datetime.cell

"datetime" columns formatting cell properties

cellProperties

Page 17

R2DOC TUTORIAL

Argument name

Details

Expected Value

percent.addsymbol

a string value to add after percent data (default to %)

character

integer.digit

minimum number of digits to display (to the left of the decimal point for data of type
percent and double)

an integer value : 0>=


value

fraction.double.digit

minimum number of digits to display to the right of the decimal point (for data of type
double)

an integer value : 0>=


value

fraction.percent.digit

minimum number of digits to display to the right of the decimal point (for data of type
percent)

an integer value : 0>=


value

Additional arguments are available as shortcuts:


Argument
name

data.cell

Details

Expected Value

cell formatting properties for all data


columns

A cellProperties object. It sets all cells to the same value (double.cell,


character.cell, integer.cell, etc.).

Page 18

R2DOC TUTORIAL

Argument
name

Details

Expected Value

data.par

paragraph formatting properties for all


data columns

A parProperties object. It sets all paragraphs to the same value (double.par,


character.par, integer.par, etc.).

data.text

text formatting properties for all data


columns

A textProperties object. It sets all texts to the same value (double.text,


character.text, integer.text, etc.).

header.cellProperties = cellProperties( border.left.width = 0, border.right.width = 0


, border.bottom.width = 1, border.top.width = 0, padding = 3 )
data.cellProperties = cellProperties( border.left.width = 0, border.right.width = 0
, border.bottom.width = 1, border.top.width = 0, padding = 2 )

header.textProperties = textProperties( font.size = 10, font.weight = "bold" )


data.textProperties = textProperties( font.size = 10 )

leftPar = parProperties( text.align = "left", padding.left = 4, padding.right = 4)


rightPar = parProperties( text.align = "right", padding.left = 4, padding.right = 4)
centerPar = parProperties( text.align = "center", padding.left = 4, padding.right = 4)

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
)

Replace Word bookmarks by tables, paragraphs and


plots
MS Work bookmarks can be used to define a named location in your document.

Page 20

R2DOC TUTORIAL

Functions

addTable , addPlot , addParagraph

Theses functions have an optional argument


containing the bookmark.

and

addImage

bookmark , if

can send respective outputs into theses bookmarks.

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

should be used if you want to replace multiple

Complete example
We are expecting the following output file document.docx
require( R2DOCX )

# Word document to write


docx.file = "document.docx"

# Remove file if it already exists


if(file.exists( docx.file ))
file.remove( docx.file )

# Word document to use as base document or a template


template.file = file.path( find.package("R2DOCX"), "templates/bookmark_example.docx", fsep = "/" )

Page 21

R2DOC TUTORIAL

# create document
doc = new("Docx", title = "My example", basefile = template.file )

# replace bookmarks 'AUTHOR' and 'REVIEWER' located in 'ttest_example.docx' by dummy values


doc = addParagraph( doc
, value = c( "James Sonny Crockett", "Ricardo Rico Tubbs" )
, stylename = "Normal"
, bookmark = "AUTHOR" )
doc = addParagraph( doc
, value = c( "Martin Marty Castillo" )
, stylename = "Normal"
, bookmark = "REVIEWER" )

# 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" )

## replace bookmark 'PLOT' by a plot


doc = addPlot( doc
, fun = plot
, x = rnorm( 100 )
, y = rnorm (100 )
, main = "base plot main title"
, bookmark = "PLOT")

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

writeDoc( doc, docx.file )

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

plot objects, use

print

on the object in the function (otherwise

doc = addPlot( doc , print


, x = qplot(Sepal.Length, Petal.Length, data = iris
, color = Species, size = Petal.Width )
, legend = "graph example", stylename="PlotReference"
)

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 .

doc = addParagraph( doc, "Hello World!", stylename = "Normal" )

Argument

stylename

is detailed in the chapter Styles.

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]."

# 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
)

Produces: donkey eats grass.


The function is vectorized
x = c("text 1", "text 2")
doc = addParagraph( doc, value = x, stylename = "Normal")

Page 26

R2DOC TUTORIAL

Table of contents
Insert a table of contents with function

addTOC .

doc = new("Docx", title = "My example" )


doc = addTOC(doc)
doc = addHeader(doc, "Title 1", 1);
doc = addHeader(doc, "Title 1.1", 2);
doc = addPageBreak( doc )
doc = addHeader(doc, "Title 1.2", 2);
doc = addPageBreak( doc )
doc = addHeader(doc, "Title 1.2.1", 3);

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

, legend="graph example 1", stylename = "PlotReference"


)
doc = addPageBreak( doc )
doc = addHeader(doc, "Title 1.2", 2);
doc = addTable( doc, iris )
doc = addParagraph( doc, value="table 1", stylename = "PlotReference" )
doc = addPageBreak( doc )
doc = addHeader(doc, "Title 1.2.1", 3);
doc = addPlot( doc
, fun = plot, x = rnorm( 100 ), y = rnorm (100 )
, main = "base plot main title"
, legend="graph example 2", stylename = "PlotReference"
)

Page breaks
Insert a page break with

addPageBreak .

doc = addPageBreak( doc)

External images
Insert an external image with

addImage .

Page 28

R2DOC TUTORIAL

doc = addImage( doc, "D:/img/image.png" )

The function is vectorized.

Header labels
Specify column labels to display with argument

header.labels

The following code:


require( R2DOCX )

# Create a new object


doc = new("Docx", title = "My example" )

desc( iris )
#'data.frame':

150 obs. of

5 variables:

# $ Sepal.Length: num

5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...

# $ 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

"Sepal.Length" = " Sepal Length", "Sepal.Width" = " Sepal Width"


, "Petal.Length" = " Petal Length", "Petal.Width" = " Petal Width"
, "Species" = "Species"
)
)

# write Word document


writeDoc( doc, "document.docx" )

should produce that table:


Sepal Length Sepal Width Petal Length Petal Width Species
5.10

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

Grouped header labels


The following code:
require( R2DOCX )

Page 30

R2DOC TUTORIAL

# Create a new object


doc = new("Docx", title = "My example" )

doc = addTable( doc


, iris[1:5,]
, grouped.cols=list( "Sepal" = c( "Sepal.Length", "Sepal.Width" )
, "Petal" = c("Petal.Length", "Petal.Width")
, "Species" = c("Species")
)
)

# write Word document


writeDoc( doc, "document.docx" )

should produce that table


Sepal

Petal

Sepal.Length Sepal.Width Petal.Length Petal.Width Species


5.10

3.50

1.40

0.20 setosa

4.90

3.00

1.40

0.20 setosa

Page 31

R2DOC TUTORIAL

Sepal

Petal

Sepal.Length Sepal.Width Petal.Length Petal.Width Species


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

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" ) )

Merging data cell


If argument

span.columns

is used, identical successive cells inside columns will be merged.

doc = addTable( doc


, iris[46:55, c(5,1:4) ]

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

Coloring data cell or font


For coloring fonts of columns use argument

col.fontcolors

For coloring cell backgrounds of columns use argument

col.colors

The following code:


doc = addTable( doc

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" ) )
)

should produce that table


Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.10

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",

"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"
)
Summary
Subj ect Identifier Av erage Weight Regularity Number of v isits Last v isit
ALD

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

You might also like