• Home
  • Header labels
  • Grouped header labels
  • Merging data cell
  • Coloring data cell or font
  • Formating tables
  • Complete example

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

# 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
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.colsis 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) ]
        , span.columns = "Species"
)
Species
Sepal.Length
Sepal.Width
Petal.Length
Petal.Width
setosa
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
versicolor
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
        , 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
        , 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
Subject Identifier
Average Weight
Regularity
Number of visits
Last visit
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
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