Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

tidyHtmlTable

Generate an htmlTable using tidy data as input


Description

Builds an htmlTable by mapping columns from the input data, x, to elements of an output htmlTable (e.g. rnames, header, etc.). This provides a ggplot2-like interface you can pivot rows/columns as required. The typical use case is when you are using dplyr together with the tidyverse data processing functions, see vignette("tidyHtmlTable").

Usage

tidyHtmlTable(
  x,
  value,
  header,
  rnames,
  rgroup,
  hidden_rgroup,
  cgroup,
  tspanner,
  hidden_tspanner,
  skip_removal_warning = getOption("htmlTable.skip_removal_warning", FALSE),
  table_fn = htmlTable,
  ...
)

Arguments

x

Tidy data used to build the htmlTable

value

The column containing values filling individual cells of the output htmlTable. Defaults to "value" as used by tidyr::pivot_longer().

header

The column in x specifying column headings

rnames

The column in x specifying row names. Defaults to "name" as used by tidyr::pivot_longer().

rgroup

The column in x specifying row groups

hidden_rgroup

strings with rgroup values that will be hidden (the values will still be there but the spanner will be set to "" and thus ignored by htmlTable()).

cgroup

The column or columns in x specifying the column groups

tspanner

The column in x specifying tspanner groups

hidden_tspanner

strings with tspanner values that will be hidden (the values will still be there but the spanner will be set to "" and thus ignored by htmlTable()).

skip_removal_warning

boolean suppress warning message when removing NA columns.

table_fn

The table function that should receive the input, defaults to htmlTable() but you can provide any function that uses the same input formatting. This package was inspired by the Hmisc::latex() function.

...

Additional arguments that will be passed to the inner htmlTable() function

Value

Returns html code that will build a pretty table

Column-mapping parameters

The tidyHtmlTable function is designed to work like ggplot2 in that columns from x are mapped to specific parameters from the htmlTable function. At minimum, x must contain the names of columns mapping to rnames, header, and rnames. header and rnames retain the same meaning as in the htmlTable function. value contains the individual values that will be used to fill each cell within the output htmlTable.

A full list of parameters from htmlTable which may be mapped to columns within x include:

  • value

  • header

  • rnames

  • rgroup

  • cgroup

  • tspanner

Also note that the coordinates of each value within x must be unambiguously mapped to a position within the output htmlTable. Therefore, the each row-wise combination the variables specified above contained in x must be unique.

Sorting

Sorting of rows is as of version 2.0 skipped as we may have situations with repeating inputs and this can easily be performed pre-function by calling dplyr::arrange() prior to tidyHtmlTable.

Columns are sorted by arrange(cgroup, header) where cgroup will be expanded to the columns of the cgroup argument, e.g. cgroup = c(a, b), header = c will become arrange(a, b, c). If you want to sort in non-alphabetic order you can provide a factor variable and that information will be retained.

Hidden values

htmlTable Allows for some values within rgroup, cgroup, etc. to be specified as "". The following parameters allow for specific values to be treated as if they were a string of length zero in the htmlTable function.

  • hidden_rgroup

  • hidden_tspanner

Simple tibble output

The tibble discourages the use of row names. There is therefore a convenience option for tidyHtmlTable where you can use the function just as you would with htmlTable() where rnames is populated with the rnames argument provided using tidyselect syntax (defaults to the "names" column if present int the input data).

Additional dependencies

In order to run this function you also must have dplyr, tidyr, tidyselect and purrr packages installed. These have been removed due to the additional 20 Mb that these dependencies added (issue #47). Note: if you use tidyverse it will already have all of these and you do not need to worry.

See Also

Examples

library(tibble)
library(dplyr)
library(tidyr)

mtcars %>%
  rownames_to_column() %>%
  select(rowname, cyl, gear, hp, mpg, qsec) %>%
  pivot_longer(names_to = "per_metric",
               cols = c(hp, mpg, qsec)) %>%
  group_by(cyl, gear, per_metric) %>%
  summarise(
    Mean = round(mean(value), 1),
    SD = round(sd(value), 1),
    Min = round(min(value), 1),
    Max = round(max(value), 1)
  ) %>%
  pivot_longer(names_to = "summary_stat",
               cols = c(Mean, SD, Min, Max)) %>%
  ungroup() %>%
  mutate(
    gear = paste(gear, "Gears"),
    cyl = paste(cyl, "Cylinders")
  ) %>%
  addHtmlTableStyle(align = "r") %>%
  tidyHtmlTable(
    header = gear,
    cgroup = cyl,
    rnames = summary_stat,
    rgroup = per_metric,
    skip_removal_warning = TRUE)

htmlTable

Advanced Tables for Markdown/HTML

v2.1.0
GPL (>= 3)
Authors
Max Gordon [aut, cre], Stephen Gragg [aut], Peter Konings [aut]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.