Author: Stephen Gragg
Date: 2024-07-20

# Using tidyHtmlTable

#### Stephen Gragg

#### 2024-07-20

# Introduction

`tidyHtmlTable` acts as a wrapper function for the `htmlTable` function allowing columns to be mapped from the input data to specific htmlTable parameters in a manner similar to ggplot2.

# Some Examples

## Prepare Data

We’ll begin by turning the `mtcars` data into a tidy dataset. The `pivot_longer` function is called to collect 3 performance metrics into a pair of key and value columns.

    library(magrittr)
    library(tidyr)
    library(dplyr)
    library(htmlTable)
    library(tibble)

    td <- mtcars %>%
      as_tibble(rownames = "rnames") %>%
      pivot_longer(names_to = "per_metric",
                   cols = c(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),
                .groups = 'drop') %>%
      pivot_longer(names_to = "summary_stat",
                   cols = c(Mean, SD, Min, Max)) %>%
      ungroup() %>%
      mutate(gear = paste(gear, "Gears"),
             cyl = paste(cyl, "Cylinders"))

At this point, we are ready to implement the `htmlTable` 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.

## Output html table

### Example 1

    tidy_summary  %>%
      arrange(per_metric, summary_stat) %>%
      addHtmlTableStyle(align = "r") %>%
      tidyHtmlTable(header = gear,
                    cgroup = cyl,
                    rnames = summary_stat,
                    rgroup = per_metric)

| 4 Cylinders |  |  | 6 Cylinders |  |  | 8 Cylinders
--- | --- | --- | --- | --- | --- | --- | ---
| 3 Gears | 4 Gears | 5 Gears |  |  | 3 Gears | 4 Gears | 5 Gears |  |  | 3 Gears | 5 Gears
hp
Max | 97 | 109 | 113 |  |  | 110 | 123 | 175 |  |  | 245 | 335
Mean | 97 | 76 | 102 |  |  | 107.5 | 116.5 | 175 |  |  | 194.2 | 299.5
Min | 97 | 52 | 91 |  |  | 105 | 110 | 175 |  |  | 150 | 264
SD |  | 20.1 | 15.6 |  |  | 3.5 | 7.5 |  |  |  | 33.4 | 50.2
mpg
Max | 21.5 | 33.9 | 30.4 |  |  | 21.4 | 21 | 19.7 |  |  | 19.2 | 15.8
Mean | 21.5 | 26.9 | 28.2 |  |  | 19.8 | 19.8 | 19.7 |  |  | 15.1 | 15.4
Min | 21.5 | 21.4 | 26 |  |  | 18.1 | 17.8 | 19.7 |  |  | 10.4 | 15
SD |  | 4.8 | 3.1 |  |  | 2.3 | 1.6 |  |  |  | 2.8 | 0.6
qsec
Max | 20 | 22.9 | 16.9 |  |  | 20.2 | 18.9 | 15.5 |  |  | 18 | 14.6
Mean | 20 | 19.6 | 16.8 |  |  | 19.8 | 17.7 | 15.5 |  |  | 17.1 | 14.6
Min | 20 | 18.5 | 16.7 |  |  | 19.4 | 16.5 | 15.5 |  |  | 15.4 | 14.5
SD |  | 1.5 | 0.1 |  |  | 0.6 | 1.1 |  |  |  | 0.8 | 0.1

### Example 2

    tidy_summary  %>%
      arrange(cyl, gear) %>%
      addHtmlTableStyle(align = "r") %>%
      tidyHtmlTable(header = summary_stat,
                    cgroup = per_metric,
                    rnames = gear,
                    rgroup = cyl)

| hp |  |  | mpg |  |  | qsec
--- | --- | --- | --- | --- | --- | --- | ---
| Max | Mean | Min | SD |  |  | Max | Mean | Min | SD |  |  | Max | Mean | Min | SD
4 Cylinders
3 Gears | 97 | 97 | 97 |  |  |  | 21.5 | 21.5 | 21.5 |  |  |  | 20 | 20 | 20 |
4 Gears | 109 | 76 | 52 | 20.1 |  |  | 33.9 | 26.9 | 21.4 | 4.8 |  |  | 22.9 | 19.6 | 18.5 | 1.5
5 Gears | 113 | 102 | 91 | 15.6 |  |  | 30.4 | 28.2 | 26 | 3.1 |  |  | 16.9 | 16.8 | 16.7 | 0.1
6 Cylinders
3 Gears | 110 | 107.5 | 105 | 3.5 |  |  | 21.4 | 19.8 | 18.1 | 2.3 |  |  | 20.2 | 19.8 | 19.4 | 0.6
4 Gears | 123 | 116.5 | 110 | 7.5 |  |  | 21 | 19.8 | 17.8 | 1.6 |  |  | 18.9 | 17.7 | 16.5 | 1.1
5 Gears | 175 | 175 | 175 |  |  |  | 19.7 | 19.7 | 19.7 |  |  |  | 15.5 | 15.5 | 15.5 |
8 Cylinders
3 Gears | 245 | 194.2 | 150 | 33.4 |  |  | 19.2 | 15.1 | 10.4 | 2.8 |  |  | 18 | 17.1 | 15.4 | 0.8
5 Gears | 335 | 299.5 | 264 | 50.2 |  |  | 15.8 | 15.4 | 15 | 0.6 |  |  | 14.6 | 14.6 | 14.5 | 0.1