Description Usage Arguments Details Examples
Insert a table into a Docx object
.
1 2 3 4 5 6 7 8 9 10 11 12 |
x |
the |
data |
|
formats |
a |
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 |
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]|
---------+----------+----------+----------+
...| ...| ...| ...|
---------+----------+----------+----------+
|
header.labels
: a name list, names are data
column names and values are labels to use in HEADER
cells(character vector of lenght 1)
Example : list("col1"="header 1","col2"="header 2")
If missing, headers will be filled with data
column names
grouped.cols
a name list, names are data
grouped headers labels and value are data
column names gathered under the grouped headers labels
Example : list("grouped header 1"=c( "col1", "col2"),"grouped header 2"=c( "col3", "col4"))
If missing, grouped headers won't be added to the table.
If used, columns order must be respected and all data
column names must belong to an element of the list:
Example (WRONG) : list("grouped header 1"=c( "col3", "col2"),"grouped header 2"=c( "col1", "col4"))
Example (WRONG) : list("grouped header 1"=c( "col1", "col2"),"grouped header 2"=c("col4"))
span.columns
: a character vector
specifying columns names where row merging should
be done (only if data[p,j]==data[p-1,j] )
Example : data = iris, span.columns = "Species"
mean to merge
identical values for the column "Species" of the iris dataset.
Default is character(0)
col.types
: a name list, names are data
column names and values are specifying how to format data in the table
Possible values are : "character", "integer", "double", "percent", "date", "datetime"
Example : list("col1"="character","col2"="double")
If missing, factor and character will be formated as character, integer as integer and numeric as double.
col.colors
: a name list, names are data
column names and values are character vectors specifying cells background colors
Each element of the list is a vector of length nrow(data)
.
Example : list("col1"=c("#AAEE00", "#AAEE00", "#000000", ...) , "col2"=c("#000000", "#AAEE00", "#AAEE00", ...) )
This argument is optional.
col.fontcolors
: a name list, names are data
column names and values are character vectors specifying cells font colors
Each element of the list is a vector of length nrow(data)
.
Example : list("col1"=c("#AAEE00", "#AAEE00", "#000000", ...) , "col2"=c("#000000", "#AAEE00", "#AAEE00", ...) )
This argument is optional.
bookmark
: a character vector specifying bookmark id to replace by the table.
If provided, table will replace the paragraph that contains the bookmark.
If not provided, table will be added at the end of the document.
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 )
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.