MyNixOS website logo
Description

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 common word processors. The package also contains a few text formatting functions that help outputting text compatible with HTML/LaTeX.

Downloads

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(magrittr)
library(htmlTable)
# A simple output
output <- matrix(1:4,
                 ncol=2,
                 dimnames = list(list("Row 1", "Row 2"),
                                 list("Column 1", "Column 2")))
htmlTable(output)
Column 1Column 2
Row 113
Row 224

If you are using dplyr and tidyverse a convenient wrapper is the tidyHtmlTable function (check out vignette("tidyHtmlTable")). A simple example of the tidyHtmlTable would look something like this:

library(tidyverse)
library(glue)
mtcars |>
  as_tibble(rownames = "rnames") |>
  filter(cyl == 6 & qsec < 18) |>
  pivot_longer(names_to = "per_metric",
               cols = c(hp, mpg, qsec)) |>
  arrange(gear, rnames) |>
  mutate(gear = glue("{gear} gears")) |>
  addHtmlTableStyle(align = "r") |>
  tidyHtmlTable(header = per_metric, rnames = rnames, rgroup = gear,
                caption = "A simple <code>tidyHtmlTable</code> example using <code>mtcars</code>")
A simple tidyHtmlTable example using mtcars
hpmpgqsec
4 gears
  Mazda RX41102116.46
  Mazda RX4 Wag1102117.02
5 gears
  Ferrari Dino17519.715.5

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) |>
  set_rownames(paste(c("1st", "2nd", "3rd",
                       paste0(4:8, "th")),
                     "row")) |>
  set_colnames(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)
  }
}

Row groups

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 hdr2nd hdr3rd hdr4th hdr5th hdr6th hdr
Group A
  1st row1:11:21:31:41:51:6
  2nd row2:12:22:32:42:52:6
Group B
  3rd row3:13:23:33:43:53:6
  4th row4:14:24:34:44:54:6
  5th row5:15:25:35:45:55:6
  6th row6:16:26:36:46:56:6
Group C
  7th row7:17:27:37:47:57:6
  8th row8:18:28:38:48:58: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 hdr2nd hdr3rd hdr4th hdr5th hdr6th hdr
Group A
  1st row1:11:21:31:41:51:6
  2nd row2:12:22:32:42:52:6
Group B
  3rd row3:13:23:33:43:53:6
  4th row4:14:24:34:44:54:6
  5th row5:15:25:35:45:55:6
  6th row6:16:26:36:46:56:6
7th row7:17:27:37:47:57:6
8th row8:18:28:38:48:58:6

When mixing row groups with variables without row groups we may want to omit the bold formatting of the row group label. As of htmlTable version 2.0 you can separate the css styling using addHtmlTableStyle:

mx |>
  addHtmlTableStyle(css.rgroup = "") |>
  htmlTable(rgroup = c(paste("Group", LETTERS[1:2]), ""),
            n.rgroup = c(2,4,nrow(mx) - 6))
1st hdr2nd hdr3rd hdr4th hdr5th hdr6th hdr
Group A
  1st row1:11:21:31:41:51:6
  2nd row2:12:22:32:42:52:6
Group B
  3rd row3:13:23:33:43:53:6
  4th row4:14:24:34:44:54:6
  5th row5:15:25:35:45:55:6
  6th row6:16:26:36:46:56:6
7th row7:17:27:37:47:57:6
8th row8:18:28:38:48:58:6

Column spanners

A column spanner spans 2 or more columns:

htmlTable(mx,
          cgroup = c("Cgroup 1", "Cgroup 2"),
          n.cgroup = c(2,4))
Cgroup 1Cgroup 2
1st hdr2nd hdr3rd hdr4th hdr5th hdr6th hdr
1st row1:11:21:31:41:51:6
2nd row2:12:22:32:42:52:6
3rd row3:13:23:33:43:53:6
4th row4:14:24:34:44:54:6
5th row5:15:25:35:45:55:6
6th row6:16:26:36:46:56:6
7th row7:17:27:37:47:57:6
8th row8:18:28:38:48:58: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 1Cgroup 2
1st hdr2nd hdr3rd hdr4th hdr5th hdr6th hdr
1st row1:11:21:31:41:51:6
2nd row2:12:22:32:42:52:6
3rd row3:13:23:33:43:53:6
4th row4:14:24:34:44:54:6
5th row5:15:25:35:45:55:6
6th row6:16:26:36:46:56:6
7th row7:17:27:37:47:57:6
8th row8:18:28:38:48:58: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 1Cgroup 2
1st hdr2nd hdr3rd hdr4th hdr5th hdr6th hdr
1st row1:11:21:31:41:51:6
2nd row2:12:22:32:42:52:6
3rd row3:13:23:33:43:53:6
4th row4:14:24:34:44:54:6
5th row5:15:25:35:45:55:6
6th row6:16:26:36:46:56:6
7th row7:17:27:37:47:57:6
8th row8:18:28:38:48:58:6

Table spanners

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 hdr2nd hdr3rd hdr4th hdr5th hdr6th hdr
Spanner A
1st row1:11:21:31:41:51:6
2nd row2:12:22:32:42:52:6
Spanner B
3rd row3:13:23:33:43:53:6
4th row4:14:24:34:44:54:6
5th row5:15:25:35:45:55:6
6th row6:16:26:36:46:56:6
Spanner C
7th row7:17:27:37:47:57:6
8th row8:18:28:38:48:58:6

Table caption

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 hdr2nd hdr
1st row1:11:2
2nd row2:12:2
mx[1:2,1:2] |>
  addHtmlTableStyle(pos.caption = "bottom") |>
  htmlTable(caption="A table caption below")
1st hdr2nd hdr
1st row1:11:2
2nd row2:12: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 hdr2nd hdr
1st row1:11:2
2nd row2:12:2

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

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

Table footer

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 hdr2nd hdr
1st row1:11:2
2nd row2:12:2
A table footer

Putting it all together

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

mx |>
  addHtmlTableStyle(col.columns = c(rep("none", 2), rep("#F5FBFF", 4)),
                    col.rgroup = c("none", "#F7F7F7"),
                    css.cell = "padding-left: .5em; padding-right: .2em;",
                    align="r") |>
  htmlTable(rgroup = paste("Group", LETTERS[1:3]),
            n.rgroup = c(2, 4),
            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)
Table 2: A table with column spanners, row groups, and zebra striping
Column spanners
Cgroup 1Cgroup 2†
1st hdr2nd hdr3rd hdr4th hdr5th hdr6th hdr
Group A
  1st row1:11:21:31:41:51:6
  2nd row2:12:22:32:42:52:6
Group B
  3rd row3:13:23:33:43:53:6
  4th row4:14:24:34:44:54:6
  5th row5:15:25:35:45:55:6
  6th row6:16:26:36:46:56:6
Group C
  7th row7:17:27:37:47:57:6
  8th row8:18:28:38:48:58:6
† A table footer comment.
Metadata

Version

2.4.3

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows