addTable-methods: Insert a table into the docx document

Description Usage Arguments Details Examples

Description

Insert a table into a Docx object.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
  ## S4 method for signature 
## 'Docx,
##    data.frame,
##    tableProperties,
##    list,
##    list,
##    integer,
##    character,
##    list,
##    list,
##    character'
addTable(x, data, formats, header.labels, grouped.cols, span.columns=character(0), col.types, col.colors, col.fontcolors, bookmark)

Arguments

x

the Docx to use

data

data.frame to add

formats

a tableProperties object that specify styles to use to format the table. optional

header.labels

a named list whose elements define labels to display in table headers instead of colnames. optional

grouped.cols

a named list whose elements define which columns are to be grouped in an upper header row (grouped header). optional

span.columns

a character vector of columns names where row merging is to be done. optional

col.types

Defines the formating style of columns. optional

col.colors

Defines the background color of cells. optional

col.fontcolors

Defines the font color of cells. optional

bookmark

a character value ; id of the Word bookmark to replace by the table. optional

Details

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
--------------------+---------------------+
  GROUPED HEADER 1  |   GROUPED HEADER 1  |
---------+----------+----------+----------+
HEADER 1 | HEADER 2 | HEADER 3 | HEADER 4 |
---------+----------+----------+----------+
data[1,1]| data[1,2]| data[1,3]| data[1,2]|
---------+----------+----------+----------+
data[2,1]| data[2,2]| data[2,3]| data[2,2]|
---------+----------+----------+----------+
      ...|       ...|       ...|       ...|
---------+----------+----------+----------+

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
data( measured.weights )
data( weights.summary )

# Word document to write
docx.file <- "document.docx"

# create document
doc <- new("Docx", title = "My example" )

# add a title
doc <- addHeader( doc, "Table example", 1 )

# add the first 5 lines of measured.weights in the docx
doc <- addTable( doc, head( measured.weights, n = 5 ) )

# demo span.columns
doc <- addHeader( doc, "Another table example", 1 )
doc <- addTable( doc, iris[ 46:55,], span.columns = "Species" )


# add another title
doc <- addHeader( doc, "And a last one", 1 )

# add and format weights.summary in the docx
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")
		, grouped.cols=list( "id" = "id"
				, "Summary" = c( "avg.weight",  "regularity", "visit.n", "last.day" ) )
		, col.types = list( "id" = "character", "avg.weight" = "double"
				, "regularity" = "percent", "visit.n" = "integer"
				, "last.day" = "date" ) 
		, col.fontcolors = list( "regularity" = ifelse( weights.summary$regularity < 0.5 , "gray", "blue") )
)

# write the docx onto the disk
writeDoc( doc, docx.file )

davidgohel/R2DOCX documentation built on May 14, 2019, 11:13 p.m.