Generate an htmlTable using tidy data as input
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")
.
tidyHtmlTable( x, value, header, rnames, rgroup, hidden_rgroup, cgroup, tspanner, hidden_tspanner, skip_removal_warning = getOption("htmlTable.skip_removal_warning", FALSE), table_fn = htmlTable, ... )
x |
Tidy data used to build the |
value |
The column containing values filling individual cells of the
output |
header |
The column in |
rnames |
The column in |
rgroup |
The column in |
hidden_rgroup |
|
cgroup |
The column or columns in |
tspanner |
The column in |
hidden_tspanner |
|
skip_removal_warning |
|
table_fn |
The table function that should receive the input, defaults to |
... |
Additional arguments that will be passed to the inner
|
Returns html code that will build a pretty table
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 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.
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
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).
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.
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)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.