Examples for 'htmlTable::tidyHtmlTable'


Generate an htmlTable using tidy data as input

Aliases: tidyHtmlTable

Keywords:

### ** Examples

library(tibble)
library(dplyr)
Attaching package: 'dplyr'
The following object is masked from 'package:rcloud.support':

    select
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(tidyr)
Attaching package: 'tidyr'
The following object is masked from 'package:RCurl':

    complete
# Prep and select basic data
data("mtcars")
base_data <- mtcars %>%
  rownames_to_column() %>%
  mutate(gear = paste(gear, "Gears"),
         cyl = paste(cyl, "Cylinders")) %>%
  select(rowname, cyl, gear, wt, mpg, qsec)

base_data %>%
  pivot_longer(names_to = "per_metric",
               cols = c(wt, mpg, qsec)) %>%
  group_by(cyl, gear, per_metric) %>%
  summarise(value_Mean = round(mean(value), 1),
            value_Min = round(min(value), 1),
            value_Max = round(max(value), 1),
            .groups = "drop") %>%
  pivot_wider(names_from = per_metric,
              values_from = starts_with("value_")) %>%
  # Round the values into a nicer format where we want the weights to have two decimals
  txtRound(ends_with("_wt"), digits = 2) %>%
  txtRound(starts_with("value") & !ends_with("_wt"), digits = 1) %>%
  # Convert into long format
  pivot_longer(cols = starts_with("value_"), names_prefix = "value_") %>%
  separate(name, into = c("summary_stat", "per_metric")) %>%
  # Without sorting the row groups wont appear right
  # If the columns end up in the wrong order you may want to change the columns
  # into factors
  arrange(per_metric) %>%
  addHtmlTableStyle(align = "r") %>%
  tidyHtmlTable(
    header = gear,
    cgroup = cyl,
    rnames = summary_stat,
    rgroup = per_metric,
    skip_removal_warning = TRUE)
4 Cylinders  6 Cylinders  8 Cylinders
3 Gears 4 Gears 5 Gears   3 Gears 4 Gears 5 Gears   3 Gears 5 Gears
mpg
  Mean 21.5 26.9 28.2   19.8 19.8 19.7   15.1 15.4
  Min 21.5 21.4 26.0   18.1 17.8 19.7   10.4 15.0
  Max 21.5 33.9 30.4   21.4 21.0 19.7   19.2 15.8
qsec
  Mean 20.0 19.6 16.8   19.8 17.7 15.5   17.1 14.6
  Min 20.0 18.5 16.7   19.4 16.5 15.5   15.4 14.5
  Max 20.0 22.9 16.9   20.2 18.9 15.5   18.0 14.6
wt
  Mean 2.50 2.40 1.80   3.30 3.10 2.80   4.10 3.40
  Min 2.50 1.60 1.50   3.20 2.60 2.80   3.40 3.20
  Max 2.50 3.20 2.10   3.50 3.40 2.80   5.40 3.60

[Package htmlTable version 2.4.3 Index]