FlexTable is a set of tools to customize tables, their formats and their contents. Formatting can be done on cells, paragraphs and text. It also lets you to insert headers and footers rows with eventually merged cells.
Below a representation of the table model implemented with FlexTable.
|
Grouped header 1.1 |
||||
|
Grouped header 2.1 |
Grouped header 2.2 |
|||
|
header_1 |
header_2 |
header_3 |
header_… |
header_p |
|
data[1,1] |
data[1,2] |
data[1,…] |
data[1,…] |
data[1, |
|
data[2,1] |
data[2,2] |
data[2,…] |
data[2,…] |
data[2, |
|
… |
… |
… |
… |
… |
|
data[ |
data[ |
data[ |
data[ |
data[ |
|
Grouped footer 1.1 |
Grouped footer 1.2 |
|||
|
Grouped footer 2.1 |
||||
Header rows
Header rows are the gray part of the table. A FlexTable can contain contain several header rows, it can also contain no header rows.
Body content
Body content is the blue part of the table.
Footer rows
Footer rows are the light-blue part of the table. A FlexTable can contain contain several footer rows, it can also contain no footer rows.
FlexTable require either argument data, a data.frame object or a matrix, either argument numrow and numcol.
When data is provided:
add.rownames = TRUE. Remember that it increments the number of columns of your table.header.columns = TRUE.Default format properties can also be specified:
body.cell.props: cellProperties object to apply to body cells.body.par.props: parProperties object to apply to body cells.body.text.props: textProperties object to apply to body cells.header.cell.props: cellProperties object to apply to header and footer cells.header.par.props: parProperties object to apply to header and footer cells.header.text.props: textProperties object to apply to header and footer cells.Below an example. The following dataset will be used:
# library( dplyr )
#
# data(esoph)
#
# data_full = esoph %>%
# group_by( agegp, alcgp ) %>%
# summarize( n = n(),
# ncases = sum( ncases, na.rm = T ),
# ncontrols = sum( ncases, na.rm = T ) )
data_full <- structure(list(agegp = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L,
6L), .Label = c("25-34", "35-44", "45-54", "55-64", "65-74",
"75+"), class = c("ordered", "factor")), alcgp = structure(c(1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("0-39g/day", "40-79",
"80-119", "120+"), class = c("ordered", "factor")), n = c(4L,
4L, 3L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
3L, 4L, 4L, 3L, 4L, 2L, 2L), ncases = c(0, 0, 0, 1, 1, 4, 0,
4, 1, 20, 12, 13, 12, 22, 24, 18, 11, 25, 13, 6, 4, 4, 2, 3),
ncontrols = c(0, 0, 0, 1, 1, 4, 0, 4, 1, 20, 12, 13, 12,
22, 24, 18, 11, 25, 13, 6, 4, 4, 2, 3)), .Names = c("agegp",
"alcgp", "n", "ncases", "ncontrols"), row.names = c(NA, -24L), class = c("data.frame"), drop = TRUE)
data = as.data.frame( head(data_full) )
knitr::kable( data )
| agegp | alcgp | n | ncases | ncontrols |
|---|---|---|---|---|
| 25-34 | 0-39g/day | 4 | 0 | 0 |
| 25-34 | 40-79 | 4 | 0 | 0 |
| 25-34 | 80-119 | 3 | 0 | 0 |
| 25-34 | 120+ | 4 | 1 | 1 |
| 35-44 | 0-39g/day | 4 | 1 | 1 |
| 35-44 | 40-79 | 4 | 4 | 4 |
Below a first FlexTable:
FlexTable(data)
|
agegp |
alcgp |
n |
ncases |
ncontrols |
|
25-34 |
0-39g/day |
4 |
0 |
0 |
|
25-34 |
40-79 |
4 |
0 |
0 |
|
25-34 |
80-119 |
3 |
0 |
0 |
|
25-34 |
120+ |
4 |
1 |
1 |
|
35-44 |
0-39g/day |
4 |
1 |
1 |
|
35-44 |
40-79 |
4 |
4 |
4 |
There are also two shortcut functions to quickly produce FlexTable objects: vanilla.tableand light.table.
vanilla.table(data)
|
agegp |
alcgp |
n |
ncases |
ncontrols |
|
25-34 |
0-39g/day |
4 |
0 |
0 |
|
25-34 |
40-79 |
4 |
0 |
0 |
|
25-34 |
80-119 |
3 |
0 |
0 |
|
25-34 |
120+ |
4 |
1 |
1 |
|
35-44 |
0-39g/day |
4 |
1 |
1 |
|
35-44 |
40-79 |
4 |
4 |
4 |
If R session is interactive, the FlexTable is rendered in an HTML page and loaded into a WWW browser. Within RStudio FlexTable is rendered in the viewer.
FlexTables have their columns autosized to the minimum width. Use setFlexTableWidths to specify each columns widths in inches.
MyFTable <- vanilla.table(data)
MyFTable
|
agegp |
alcgp |
n |
ncases |
ncontrols |
|
25-34 |
0-39g/day |
4 |
0 |
0 |
|
25-34 |
40-79 |
4 |
0 |
0 |
|
25-34 |
80-119 |
3 |
0 |
0 |
|
25-34 |
120+ |
4 |
1 |
1 |
|
35-44 |
0-39g/day |
4 |
1 |
1 |
|
35-44 |
40-79 |
4 |
4 |
4 |
Add header rows with function addHeaderRow. As we want to add specific header rows, header.columns needs to be set to FALSE.
# add first header row
value = c('Factors', 'Summary statistics'), colspan = c(2, 3) )
# add second header row
text.properties = textBold() )
MyFTable
|
Factors |
Summary statistics |
|||
|
agegp |
alcgp |
n |
ncases |
ncontrols |
|
25-34 |
0-39g/day |
4 |
0 |
0 |
|
25-34 |
40-79 |
4 |
0 |
0 |
|
25-34 |
80-119 |
3 |
0 |
0 |
|
25-34 |
120+ |
4 |
1 |
1 |
|
35-44 |
0-39g/day |
4 |
1 |
1 |
|
35-44 |
40-79 |
4 |
4 |
4 |
Functions spanFlexTableRows and spanFlexTableColumns are merging cells of a FlexTable object.
There is a special argument runs. It specifies to merge cells that have identical values along runs values.
The following code show usage of this parameter:
MyFTable = FlexTable(data)
MyFTable
|
agegp |
alcgp |
n |
ncases |
ncontrols |
|
25-34 |
0-39g/day |
4 |
0 |
0 |
|
40-79 |
4 |
0 |
0 |
|
|
80-119 |
3 |
0 |
0 |
|
|
120+ |
4 |
1 |
1 |
|
|
35-44 |
0-39g/day |
4 |
1 |
1 |
|
40-79 |
4 |
4 |
4 |
If argument runs is not what you need, there are arguments from and to that let you specify which cells are to be span.
MyFTable = FlexTable(data)
MyFTable
|
agegp |
alcgp |
n |
ncases |
ncontrols |
|
25-34 |
0-39g/day |
4 |
0 |
0 |
|
25-34 |
40-79 |
|||
|
25-34 |
80-119 |
3 |
0 |
0 |
|
25-34 |
4 |
1 |
1 |
|
|
35-44 |
4 |
1 |
1 |
|
|
35-44 |
4 |
4 |
4 |
|
When creating a table, you sometimes have a long table header like “Number of R users by country” and the data in the corresponding column is like “2”, “123”, … That’s an unpleasant mismatch of width. It’s not a problem for two columns, but if you had 10 columns, a better use of space would be to rotate the headers so that the column width can be much narrower.
FlexTable let you manage rotated headers text. It can be specified either with the FlexTable function either whit the addHeaderRow function.
It requires usage of argument text.direction within cellProperties associated with headers. This argument accept one of the following options:
# define a cell properties object with text rotation
header_cells_props <- cellProperties(
text.direction = "btlr", background.color = "#00557F", padding = 3 )
header_text_props <- textProperties(
color = "white", font.size = 11, font.weight = "bold" )
header.cell.props = header_cells_props, header.text.props = header_text_props,
MyFTable[] = parCenter()
addFlexTable( MyFTable )
filename <- "header_rot.pptx" # the document to produce
writeDoc( doc, file = filename )
Download file header_rot.pptx - view with office web viewer
par.properties = parprop ) %>%
par.properties = parprop, cell.properties = cellprop )
addFlexTable( MyFTable )
filename <- "header_rot.docx" # the document to produce
writeDoc( doc, file = filename )
Download file header_rot.docx - view with office web viewer
MyFTable <- FlexTable(data)
# change background colors of some cells in the table body
colors = ifelse(data$ncases < 5, '#DDDDDD', 'orange'))
# change background colors of a cell in the table header
colors = '#8888F0', to = 'header')
MyFTable
|
agegp |
alcgp |
n |
ncases |
ncontrols |
|
25-34 |
0-39g/day |
4 |
0 |
0 |
|
25-34 |
40-79 |
4 |
0 |
0 |
|
25-34 |
80-119 |
3 |
0 |
0 |
|
25-34 |
120+ |
4 |
1 |
1 |
|
35-44 |
0-39g/day |
4 |
1 |
1 |
|
35-44 |
40-79 |
4 |
4 |
4 |
Modify rows or columns backgroud colors with setRowsColors and setColumnsColors.
MyFTable
|
agegp |
alcgp |
n |
ncases |
ncontrols |
|
25-34 |
0-39g/day |
4 |
0 |
0 |
|
25-34 |
40-79 |
4 |
0 |
0 |
|
25-34 |
80-119 |
3 |
0 |
0 |
|
25-34 |
120+ |
4 |
1 |
1 |
|
35-44 |
0-39g/day |
4 |
1 |
1 |
|
35-44 |
40-79 |
4 |
4 |
4 |
Modify rows or columns backgroud colors with setRowsColors and setColumnsColors.
MyFTable
|
agegp |
alcgp |
n |
ncases |
ncontrols |
|
25-34 |
0-39g/day |
4 |
0 |
0 |
|
25-34 |
40-79 |
4 |
0 |
0 |
|
25-34 |
80-119 |
3 |
0 |
0 |
|
25-34 |
120+ |
4 |
1 |
1 |
|
35-44 |
0-39g/day |
4 |
1 |
1 |
|
35-44 |
40-79 |
4 |
4 |
4 |
MyFTable = FlexTable(data)
# format body content
# format header content
MyFTable
|
agegp |
alcgp |
n |
ncases |
ncontrols |
|
25-34 |
0-39g/day |
4 |
0 |
0 |
|
25-34 |
40-79 |
4 |
0 |
0 |
|
25-34 |
80-119 |
3 |
0 |
0 |
|
25-34 |
120+ |
4 |
1 |
1 |
|
35-44 |
0-39g/day |
4 |
1 |
1 |
|
35-44 |
40-79 |
4 |
4 |
4 |
MyFTable = FlexTable(data)
# format body content
# format header content
MyFTable
|
agegp |
alcgp |
n |
ncases |
ncontrols |
|
25-34 |
0-39g/day |
4 |
0 |
0 |
|
25-34 |
40-79 |
4 |
0 |
0 |
|
25-34 |
80-119 |
3 |
0 |
0 |
|
25-34 |
120+ |
4 |
1 |
1 |
|
35-44 |
0-39g/day |
4 |
1 |
1 |
|
35-44 |
40-79 |
4 |
4 |
4 |
MyFTable = FlexTable(data)
MyFTable
|
agegp |
alcgp |
n |
ncases |
ncontrols |
|
25-34 |
0-39g/day |
4 |
0 |
0 |
|
25-34 |
40-79 |
4 |
0 |
0 |
|
25-34 |
80-119 |
3 |
0 |
0 |
|
25-34 |
120+ |
4 |
1 |
1 |
|
35-44 |
0-39g/day |
4 |
1 |
1 |
|
35-44 |
40-79 |
4 |
4 |
4 |
MyFTable[,'alcgp', side = 'bottom'] <- border_
MyFTable
|
agegp |
alcgp |
n |
ncases |
ncontrols |
|
25-34 |
0-39g/day |
4 |
0 |
0 |
|
25-34 |
40-79 |
4 |
0 |
0 |
|
25-34 |
80-119 |
3 |
0 |
0 |
|
25-34 |
120+ |
4 |
1 |
1 |
|
35-44 |
0-39g/day |
4 |
1 |
1 |
|
35-44 |
40-79 |
4 |
4 |
4 |
# the default cellProperties
myCellProps = cellProperties( )
header.text.props = textBold(),
header.cell.props = myCellProps,
body.cell.props= myCellProps )
# modify the default cellProperties and apply
# it to cells of columns 4 to 8 where n < 20
MyFTable
|
agegp |
alcgp |
n |
ncases |
ncontrols |
|
25-34 |
0-39g/day |
4 |
0 |
0 |
|
25-34 |
40-79 |
4 |
0 |
0 |
|
25-34 |
80-119 |
3 |
0 |
0 |
|
25-34 |
120+ |
4 |
1 |
1 |
|
35-44 |
0-39g/day |
4 |
1 |
1 |
|
35-44 |
40-79 |
4 |
4 |
4 |
You can add content with the substract operator.
Function has an argument text.properties to specify text formatting properties:
MyFTable[data$ncases < 1, 'n',
MyFTable
|
agegp |
alcgp |
n |
ncases |
ncontrols |
|
25-34 |
0-39g/day |
4(1) |
0 |
0 |
|
25-34 |
40-79 |
4(1) |
0 |
0 |
|
25-34 |
80-119 |
3(1) |
0 |
0 |
|
25-34 |
120+ |
4 |
1 |
1 |
|
35-44 |
0-39g/day |
4 |
1 |
1 |
|
35-44 |
40-79 |
4 |
4 |
4 |
And an argument newpar to let create new paragraphs into cells.
MyFTable
|
agegp |
alcgp |
n |
ncases |
ncontrols |
|
25-34 newpar usage |
0-39g/day |
4(1) |
0 |
0 |
|
25-34 |
40-79 |
4(1) |
0 |
0 |
|
25-34 |
80-119 |
3(1) |
0 |
0 |
|
25-34 |
120+ |
4 |
1 |
1 |
|
35-44 |
0-39g/day |
4 |
1 |
1 |
|
35-44 |
40-79 |
4 |
4 |
4 |