The tidyhTbl
package is meant to act as a wrapper function for the htmlTable
function in the htmlTable
package so that data in a tidy format can be used
to fully specify complex html tables.
A strong understanding of the capabilities of the htmlTable package is a pre-requisite for appropriate use of this package. A knowledge of tidy data and the tidyverse are also key to implementing this package appropriately.
We'll begin by turning the mtcars
data into a tidy dataset. The
gather
function is called to collect 3 performance metrics into a pair
of key and value columns.
library(magrittr) library(tidyr) library(dplyr) library(tidyhTbl) library(tibble) td <- mtcars %>% rownames_to_column %>% select(rowname, cyl, gear, hp, mpg, qsec) %>% gather(per_metric, value, hp, mpg, qsec)
Now we will compute 4 summary statistics for each of the 3 performance metrics. This will be further grouped by number of cylinders and gears.
tidy_summary <- td %>% 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)) %>% gather(summary_stat, value, Mean, SD, Min, Max) %>% ungroup %>% mutate(gear = paste(gear, "Gears"), cyl = paste(cyl, "Cylinders"))
At this point, we are ready to implement the htmlTable_td
function.
Essentially, this constructs an html table using arguments similar to the
htmlTable
function. However, whereas htmlTable
required the user to manually
arrange the data and specify the column groups, headers, row names, row-groups,
etc., each of these components of the table is mapped to a column within the
input data.
tidy_summary %>% htmlTable_td(header_td = "gear", cgroup1_td = "cyl", cell_value = "value", rnames_td = "summary_stat", rgroup_td = "per_metric")
tidy_summary %>% htmlTable_td(header_td = "summary_stat", cgroup1_td = "per_metric", cell_value = "value", rnames_td = "gear", rgroup_td = "cyl")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.