The tab_stub_indent() function

Using a subset of the photolysis dataset within a gt table, we can provide some indentation to all of the row labels in the stub via tab_stub_indent(). Here we provide an indent value of 3 for a very prominent indentation that clearly shows that the row labels are subordinate to the two row group labels in this table ("inorganic reactions" and "carbonyls").

photolysis |>
  dplyr::select(cmpd_name, products, type, l, m, n) |>
  dplyr::slice_head(n = 10) |>
  gt(groupname_col = "type", rowname_col = "cmpd_name") |>
  fmt_chem(columns = products) |>
  fmt_scientific(columns = l) |>
  tab_stub_indent(
    rows = everything(),
    indent = 3
  )
products l m n
inorganic reactions
ozone O(1
 
D) + O2
6.07 × 10−5 1.743 0.474
ozone O(3
 
P) + O2
4.78 × 10−4 0.298 0.080
hydrogen peroxide OH + OH 1.04 × 10−5 0.723 0.279
nitrogen dioxide NO + O(3
 
P)
1.17 × 10−2 0.244 0.267
nitrate radical NO + O2 2.49 × 10−2 0.168 0.108
nitrate radical NO2 + O(3
 
P)
1.75 × 10−1 0.155 0.125
nitrous acid OH + NO 2.64 × 10−3 0.261 0.288
nitric acid OH + NO2 9.31 × 10−7 1.230 0.307
carbonyls
formaldeyde HCO + H 4.64 × 10−5 0.762 0.353
formaldeyde CO + H2 6.85 × 10−5 0.477 0.323

Let’s use a summarized version of the pizzaplace dataset to create a another gt table with row groups and row labels. With summary_rows(), we’ll generate summary rows at the top of each row group. Using tab_stub_indent() we can add indentation to the row labels in the stub.

pizzaplace |>
  dplyr::group_by(type, size) |>
  dplyr::summarize(
    sold = dplyr::n(),
    income = sum(price),
    .groups = "drop"
  ) |>
  gt(rowname_col = "size", groupname_col = "type") |>
  tab_header(title = "Pizzas Sold in 2015") |>
  fmt_integer(columns = sold) |>
  fmt_currency(columns = income) |>
  summary_rows(
    fns = list(label = "All Sizes", fn = "sum"),
    side = "top",
    fmt = list(
      ~ fmt_integer(., columns = sold),
      ~ fmt_currency(., columns = income)
    )
  ) |>
  tab_options(
    summary_row.background.color = "gray95",
    row_group.background.color = "#FFEFDB",
    row_group.as_column = TRUE
  ) |>
  tab_stub_indent(
    rows = everything(),
    indent = 2
  )
Pizzas Sold in 2015
sold income
chickenAll Sizes 11,050 $195,919.50
L 4,932 $102,339.00
M 3,894 $65,224.50
S 2,224 $28,356.00
classicAll Sizes 14,888 $220,053.10
L 4,057 $74,518.50
M 4,112 $60,581.75
S 6,139 $69,870.25
XL 552 $14,076.00
XXL 28 $1,006.60
supremeAll Sizes 11,987 $208,197.00
L 4,564 $94,258.50
M 4,046 $66,475.00
S 3,377 $47,463.50
veggieAll Sizes 11,649 $193,690.45
L 5,403 $104,202.70
M 3,583 $57,101.00
S 2,663 $32,386.75

Indentation of entries in the stub can be controlled by values within a column. Here’s an example of that using the constants dataset, where variations of a row label are mutated to eliminate the common leading text (replacing it with "..."). At the same time, the indentation for those rows is set to 4 in the indent column (value is 0 otherwise). The tab_stub_indent() statement uses from_column(), which passes values from the indent column to the namesake argument. We hide the indent column from view by use of cols_hide().

constants |>
  dplyr::select(name, value, uncert, units) |>
  dplyr::filter(
    grepl("^atomic mass constant", name) |
      grepl("^Rydberg constant", name) |
      grepl("^Bohr magneton", name)
  ) |>
  dplyr::mutate(
    indent = ifelse(grepl("constant |magneton ", name), 4, 0),
    name = gsub(".*constant |.*magneton ", "...", name)
  ) |>
  gt(rowname_col = "name") |>
  tab_stubhead(label = "Physical Constant") |>
  tab_stub_indent(
    rows = everything(),
    indent = from_column(column = "indent")
  ) |>
  fmt_scientific(columns = c(value, uncert)) |>
  fmt_units(columns = units) |>
  cols_hide(columns = indent) |>
  cols_label(
    value = "Value",
    uncert = "Uncertainty",
    units = "Units"
  ) |>
  cols_width(
    stub() ~ px(250),
    c(value, uncert) ~ px(150),
    units ~ px(80)
  ) |>
  tab_style(
    style = cell_text(indent = px(10)),
    locations = list(
      cells_column_labels(columns = units),
      cells_body(columns = units)
    )
  )
Physical Constant Value Uncertainty Units
atomic mass constant 1.66 × 10−27 5.00 × 10−37 kg
...energy equivalent 1.49 × 10−10 4.50 × 10−20 J
...energy equivalent in MeV 9.31 × 102 2.80 × 10−7 MeV
Bohr magneton 9.27 × 10−24 2.80 × 10−33 J T−1
...in eV/T 5.79 × 10−5 1.70 × 10−14 eV T−1
...in Hz/T 1.40 × 1010 4.20 Hz T−1
...in inverse meter per tesla 4.67 × 101 1.40 × 10−8 m−1 T−1
...in K/T 6.72 × 10−1 2.00 × 10−10 K T−1
Rydberg constant 1.10 × 107 2.10 × 10−5 m−1
...times c in Hz 3.29 × 1015 6.40 × 103 Hz
...times hc in eV 1.36 × 101 2.60 × 10−11 eV
...times hc in J 2.18 × 10−18 4.20 × 10−30 J