Advanced Tables for Markdown/HTML

Tables with state-of-the-art layout elements such as row spanners, column spanners, table spanners, zebra striping, and more. While allowing advanced layout, the underlying css-structure is simple in order to maximize compatibility with word processors such as 'MS Word' or 'LibreOffice'. The package also contains a few text formatting functions that help outputting text compatible with HTML/'LaTeX'.


Basics

The htmlTable package is intended for generating tables using HTML formatting. This format is compatible with Markdown when used for HTML-output. The most basic table can easily be created by just passing a matrix or a data.frame to the htmlTable-function:

library(htmlTable) output <- matrix(1:4,
                 ncol=2,
                 dimnames = list(list("Row 1", "Row 2"),
                                 list("Column 1", "Column 2")))
htmlTable(output)
Column 1 Column 2
Row 1 1 3
Row 2 2 4

As of version 1.0.2 you no longer need to specify results='asis' for each knitr chunk.

Advanced

While it may be sufficient for basic tables a more advanced layout is often needed in medical publications with elements such as:

  • row groups
  • column spanners
  • table spanners
  • caption
  • table footer
  • zebra coloring (also know as banding):
    • rows
    • columns

As many journals require that a MS Word-document is submitted it is furthermore also important that the table imports correctly to a word processor, i.e. that the table doesn't only look nice in a web browser but also in the final document. The htmlTable-function is written for all these purposes.

Note: Due to GitHub CSS-styles the rows get automatically zebra-striped (in a bad way), borders get overridden and I haven't been able to figure out how to change this. See the vignette for a correct example: vignette("general", package = "htmlTable")

For demonstration purposes we will setup a basic matrix:

mx <-
  matrix(ncol=6, nrow=8)
rownames(mx) <- paste(c("1st", "2nd",
                        "3rd",
                        paste0(4:8, "th")),
                      "row")
colnames(mx) <- paste(c("1st", "2nd",
                        "3rd", 
                        paste0(4:6, "th")),
                      "hdr")
 
for (nr in 1:nrow(mx)){
  for (nc in 1:ncol(mx)){
    mx[nr, nc] <-
      paste0(nr, ":", nc)
  }
}

The purpose of the row groups is to group variables that belong to the same group, e.g. a factored variable with more than two levels often benefit from grouping variables together.

htmlTable(mx, 
          rgroup = paste("Group", LETTERS[1:3]),
          n.rgroup = c(2,4,nrow(mx) - 6))
1st hdr 2nd hdr 3rd hdr 4th hdr 5th hdr 6th hdr
Group A
  1st row 1:1 1:2 1:3 1:4 1:5 1:6
  2nd row 2:1 2:2 2:3 2:4 2:5 2:6
Group B
  3rd row 3:1 3:2 3:3 3:4 3:5 3:6
  4th row 4:1 4:2 4:3 4:4 4:5 4:6
  5th row 5:1 5:2 5:3 5:4 5:5 5:6
  6th row 6:1 6:2 6:3 6:4 6:5 6:6
Group C
  7th row 7:1 7:2 7:3 7:4 7:5 7:6
  8th row 8:1 8:2 8:3 8:4 8:5 8:6

We can easily mix row groups with regular variables by having an empty row group name "":

htmlTable(mx, 
          rgroup = c(paste("Group", LETTERS[1:2]), ""),
          n.rgroup = c(2,4,nrow(mx) - 6))
1st hdr 2nd hdr 3rd hdr 4th hdr 5th hdr 6th hdr
Group A
  1st row 1:1 1:2 1:3 1:4 1:5 1:6
  2nd row 2:1 2:2 2:3 2:4 2:5 2:6
Group B
  3rd row 3:1 3:2 3:3 3:4 3:5 3:6
  4th row 4:1 4:2 4:3 4:4 4:5 4:6
  5th row 5:1 5:2 5:3 5:4 5:5 5:6
  6th row 6:1 6:2 6:3 6:4 6:5 6:6
7th row 7:1 7:2 7:3 7:4 7:5 7:6
8th row 8:1 8:2 8:3 8:4 8:5 8:6

When mixing row groups with variables without row groups we may want to omit the bold formatting of the row group label:

htmlTable(mx, 
          css.rgroup = "",
          rgroup = c(paste("Group", LETTERS[1:2]), ""),
          n.rgroup = c(2,4,nrow(mx) - 6))
1st hdr 2nd hdr 3rd hdr 4th hdr 5th hdr 6th hdr
Group A
  1st row 1:1 1:2 1:3 1:4 1:5 1:6
  2nd row 2:1 2:2 2:3 2:4 2:5 2:6
Group B
  3rd row 3:1 3:2 3:3 3:4 3:5 3:6
  4th row 4:1 4:2 4:3 4:4 4:5 4:6
  5th row 5:1 5:2 5:3 5:4 5:5 5:6
  6th row 6:1 6:2 6:3 6:4 6:5 6:6
7th row 7:1 7:2 7:3 7:4 7:5 7:6
8th row 8:1 8:2 8:3 8:4 8:5 8:6

A column spanner spans 2 or more columns:

htmlTable(mx,
          cgroup = c("Cgroup 1", "Cgroup 2"),
          n.cgroup = c(2,4))
Cgroup 1  Cgroup 2
1st hdr 2nd hdr   3rd hdr 4th hdr 5th hdr 6th hdr
1st row 1:1 1:2   1:3 1:4 1:5 1:6
2nd row 2:1 2:2   2:3 2:4 2:5 2:6
3rd row 3:1 3:2   3:3 3:4 3:5 3:6
4th row 4:1 4:2   4:3 4:4 4:5 4:6
5th row 5:1 5:2   5:3 5:4 5:5 5:6
6th row 6:1 6:2   6:3 6:4 6:5 6:6
7th row 7:1 7:2   7:3 7:4 7:5 7:6
8th row 8:1 8:2   8:3 8:4 8:5 8:6

It can sometimes be convenient to have column spanners in multiple levels:

htmlTable(mx,
          cgroup = rbind(c("", "Column spanners", NA),
                         c("", "Cgroup 1", "Cgroup 2")),
          n.cgroup = rbind(c(1,2,NA),
                           c(2,2,2)))
  Column spanners
  Cgroup 1  Cgroup 2
1st hdr 2nd hdr   3rd hdr 4th hdr   5th hdr 6th hdr
1st row 1:1 1:2   1:3 1:4   1:5 1:6
2nd row 2:1 2:2   2:3 2:4   2:5 2:6
3rd row 3:1 3:2   3:3 3:4   3:5 3:6
4th row 4:1 4:2   4:3 4:4   4:5 4:6
5th row 5:1 5:2   5:3 5:4   5:5 5:6
6th row 6:1 6:2   6:3 6:4   6:5 6:6
7th row 7:1 7:2   7:3 7:4   7:5 7:6
8th row 8:1 8:2   8:3 8:4   8:5 8:6

Above example allows the column spanner to be a sum of the underlying cgroups (see n.cgroup), this is not required by the function:

htmlTable(mx,
          cgroup = rbind(c("", "Column spanners", NA),
                         c("", "Cgroup 1", "Cgroup 2")),
          n.cgroup = rbind(c(1,5,NA),
                           c(2,1,3)))
  Column spanners
  Cgroup 1  Cgroup 2
1st hdr   2nd hdr   3rd hdr   4th hdr 5th hdr 6th hdr
1st row 1:1   1:2   1:3   1:4 1:5 1:6
2nd row 2:1   2:2   2:3   2:4 2:5 2:6
3rd row 3:1   3:2   3:3   3:4 3:5 3:6
4th row 4:1   4:2   4:3   4:4 4:5 4:6
5th row 5:1   5:2   5:3   5:4 5:5 5:6
6th row 6:1   6:2   6:3   6:4 6:5 6:6
7th row 7:1   7:2   7:3   7:4 7:5 7:6
8th row 8:1   8:2   8:3   8:4 8:5 8:6

A table spanner is similar to rgroup but has the primary purpose of combining 2 or more tables with the same columns into one:

htmlTable(mx, 
          tspanner = paste("Spanner", LETTERS[1:3]),
          n.tspanner = c(2,4,nrow(mx) - 6))
1st hdr 2nd hdr 3rd hdr 4th hdr 5th hdr 6th hdr
Spanner A
1st row 1:1 1:2 1:3 1:4 1:5 1:6
2nd row 2:1 2:2 2:3 2:4 2:5 2:6
Spanner B
3rd row 3:1 3:2 3:3 3:4 3:5 3:6
4th row 4:1 4:2 4:3 4:4 4:5 4:6
5th row 5:1 5:2 5:3 5:4 5:5 5:6
6th row 6:1 6:2 6:3 6:4 6:5 6:6
Spanner C
7th row 7:1 7:2 7:3 7:4 7:5 7:6
8th row 8:1 8:2 8:3 8:4 8:5 8:6

The table caption is simply the table description and can be either located above or below the table:

htmlTable(mx[1:2,1:2], 
          caption="A table caption above")
Table 5: A table caption above
1st hdr 2nd hdr
1st row 1:1 1:2
2nd row 2:1 2:2
htmlTable(mx[1:2,1:2], 
          pos.caption = "bottom",
          caption="A table caption below")
1st hdr 2nd hdr
1st row 1:1 1:2
2nd row 2:1 2:2
Table 6: A table caption below

A more interesting detail that the function allows for is table numbering, initialized by:

options(table_counter = TRUE)
htmlTable(mx[1:2,1:2], 
          caption="A table caption with a numbering")
Table 1: A table caption with a numbering
1st hdr 2nd hdr
1st row 1:1 1:2
2nd row 2:1 2:2

As we often want to reference the table number in the text there are two associated functions:

tblNoLast()
## [1] 1
tblNoNext()
## [1] 2

The footer usually contains specifics regarding variables and is always located at the foot of the table:

htmlTable(mx[1:2,1:2], 
          tfoot="A table footer")
1st hdr 2nd hdr
1st row 1:1 1:2
2nd row 2:1 2:2
A table footer

Now if we want to do everything in one table it may look like this:

htmlTable(mx, 
          align="r",
          rgroup = paste("Group", LETTERS[1:3]),
          n.rgroup = c(2,4,nrow(mx) - 6),
          cgroup = rbind(c("", "Column spanners", NA),
                         c("", "Cgroup 1", "Cgroup 2&dagger;")),
          n.cgroup = rbind(c(1,2,NA),
                           c(2,2,2)),
          caption="A table with column spanners, row groups, and zebra striping",
          tfoot="&dagger; A table footer commment",
          cspan.rgroup = 2,
          col.columns = c(rep("none", 2),
                          rep("#F5FBFF", 4)),
          col.rgroup = c("none", "#F7F7F7"),
          css.cell = "padding-left: .5em; padding-right: .2em;")
Table 2: A table with column spanners, row groups, and zebra striping
  Column spanners
  Cgroup 1  Cgroup 2†
1st hdr 2nd hdr   3rd hdr 4th hdr   5th hdr 6th hdr
Group A    
  1st row 1:1 1:2   1:3 1:4   1:5 1:6
  2nd row 2:1 2:2   2:3 2:4   2:5 2:6
Group B    
  3rd row 3:1 3:2   3:3 3:4   3:5 3:6
  4th row 4:1 4:2   4:3 4:4   4:5 4:6
  5th row 5:1 5:2   5:3 5:4   5:5 5:6
  6th row 6:1 6:2   6:3 6:4   6:5 6:6
Group C    
  7th row 7:1 7:2   7:3 7:4   7:5 7:6
  8th row 8:1 8:2   8:3 8:4   8:5 8:6
† A table footer comment

News

NEWS for the htmlTable package

  • Added ability to print matrix & data.frame without any rows, i.e. empty (thanks lemna)
  • Added table border flexibility via the ctable argument (Thanks raredd)
  • Added option of having row-group separators for no-named row groups (Thanks, prof. Harrell)
  • Fixed bug with outputting dates (issue #14)
  • The txtRound now properly handles vector digits argument
  • The txtRound is now a S3-function and handles data.frame objects in a cleaner way
  • Added better description for how to use the add attribute for rgroups
  • Extended the add attribute for rgroup to accept matrices
  • The n.rgroup/rgroup are automaticaly completed with the last rows if sum(n.rgroup) is less than the total number of rows
  • Similar applies to n.cgroup/cgroup
  • Fixed the line-merge so that all new lines get an
    -tag
  • Added an interactiveTable for allowing tables with cells that have resizeable content
  • Added css.table for table element css styling
  • Handles data.frames with factors - thanks Sergio Oller #4
  • Prepared for API-changes with stringr 1.0
  • The txtRound can now handle vectors and single values
  • Fixed table counter update
  • The htmlTable can now also accept vectors
  • Removed the format.df from Hmisc as it converted & to & with unexpected results. This functionality has also been superseeded by the txtRound function.
  • Added the option of having an attribute on the rgroup in case there is an interest of adding more data to that particular row
  • Added a fix for the pandoc tab bug
  • knit_print implemented removing the need for results='asis' except for within for-loops
  • Removed the capitalize tspanner css as this may cause confusion with limited word processor compatibility
  • Added htmlTable tests
  • txtRound now also rounds character matrices
  • Added a detailed vignette with the primary features of htmlTable
  • Added the option of having a total row
  • The pos.caption can now also be "below"
  • Fixed minor bug with numbering not beeing turned off with options(table_counter = FALSE)
  • Zebra striping now works for rgroups mixed with ""
  • txtRound returns "" by default if value missing. This can also be specified with the txt.NA option
  • The htmlTable and associated txt-functions are now separated from Gmisc
  • Argument name changes for htmlTable for better consistency and logic: rowname -> rnames headings -> header halign -> align.header cgroup.just -> align.cgroup rgroupCSSstyle -> css.rgroup rgroupCSSseparator -> css.rgroup.sep tspannerCSSstyle -> css.tspanner tspannerCSSseparator -> css.tspanner.sep tableCSSclass -> css.table.class rowlabel.pos -> pos.rowlabel caption.loc -> pos.caption altcol -> col.rgroup
  • htmlTable can now handle rnames = FALSE in order to surpress rownames
  • htmlTable now defaults to the layout of ctable as this is the more commonly found layout among medical papers
  • htmlTable rgroup has the additional padding.rgroup for those that want to change the no-breaking space padding
  • htmlTable tfoot is automatically run through txtMergeLines in order to retain wrapped text
  • Renamed splitLines4Table to txtMergeLines, outputInt to txtInt, pvalueFormatter to txtPval and these follow now the argument style of htmlTable
  • Added txtRound for rounding matrices. The problem with round() is that 1.01 rounds to 1 instead of "1.0" that is wanted for output.
  • Multiple bug-fixes

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("htmlTable")

1.9 by Max Gordon, 9 months ago


http://gforge.se/packages/


Report a bug at https://github.com/gforge/htmlTable/issues


Browse source code at https://github.com/cran/htmlTable


Authors: Max Gordon <max@gforge.se>


Documentation:   PDF Manual  


Task views: Reproducible Research


GPL (>= 3) license


Imports stringr, knitr, magrittr, methods, checkmate, htmlwidgets

Suggests testthat, XML, xtable, ztable, Hmisc, reshape, rmarkdown, pander, chron, lubridate


Imported by Greg, Hmisc, compareDF, condformat, epitable, neuropsychology, rms.

Depended on by Gmisc, SkyWatchr, expss.

Suggested by dataRetrieval, leaflet.esri, leaflet.extras, microplot.


See at CRAN