The fmt_units() function

Let’s use the illness dataset and create a new gt table. The units column contains character values in gt’s specialized units notation (e.g., "x10^9 / L") so the fmt_units() function was used to better format those units.

illness |>
  gt() |>
  fmt_units(columns = units) |>
  sub_missing(columns = -starts_with("norm")) |>
  sub_missing(columns = c(starts_with("norm"), units), missing_text = "") |>
  sub_large_vals(rows = test == "MYO", threshold = 1200) |>
  fmt_number(
    decimals = 2,
    drop_trailing_zeros = TRUE
  ) |>
  tab_header(title = "Laboratory Findings for the YF Patient") |>
  tab_spanner(label = "Day", columns = starts_with("day")) |>
  cols_label_with(fn = ~ gsub("day_", "", .)) |>
  cols_merge_range(col_begin = norm_l, col_end = norm_u) |>
  cols_label(
    starts_with("norm") ~ "Normal Range",
    test ~ "Test",
    units ~ "Units"
  ) |>
  cols_width(
    starts_with("day") ~ px(80),
    everything() ~ px(120)
  ) |>
  tab_style(
    style = cell_text(align = "center"),
    locations = cells_column_labels(columns = starts_with("day"))
  ) |>
  tab_style(
    style = cell_fill(color = "aliceblue"),
    locations = cells_body(columns = c(test, units))
  ) |>
  opt_vertical_padding(scale = 0.4) |>
  opt_align_table_header(align = "left") |>
  tab_options(heading.padding = px(10))
Laboratory Findings for the YF Patient
Test Units
Day
Normal Range
3 4 5 6 7 8 9
Viral load copies per mL 12,000 4,200 1,600 830 760 520 250
WBC ×109/L 5.26 4.26 9.92 10.49 24.77 30.26 19.03 4–10
Neutrophils ×109/L 4.87 4.72 7.92 18.21 22.08 27.17 16.59 2–8
RBC ×1012/L 5.72 5.98 4.23 4.83 4.12 2.68 3.32 4–5.5
Hb g/L 153 135 126 115 75 87 95 120–160
PLT ×109/L 67 38.6 27.4 26.2 74.1 36.2 25.6 100–300
ALT U/L 12,835 12,632 6,426.7 4,263.1 1,623.7 672.6 512.4 9–50
AST U/L 23,672 21,368 14,730 8,691 2,189 1,145 782.5 15–40
TBIL µmol/L 117.2 143.8 137.2 158.1 127.3 105.1 163.2 0–18.8
DBIL µmol/L 71.4 104.6 94.6 143.9 117.8 83.6 126.3 0–6.8
NH3 mmol/L 115.2 135.2 131 176.7 84.2 72.4 91.9 10–47
PT s 24.6 42.4 53.7 54 22.6 16.8 29.5 9.4–12.5
APTT s 39.2 57.2 65.9 68.3 62.4 61.7 114.7 25.1–36.5
PTA % 41 25 19 14 51 55 31 70–130
DD mg/L 32.9 35.1 24.5 25.6 18.7 24.7 64.8 0–5
FDP µg/mL 84.7 92.5 77.2 157.2 291.7 0–5
Fibrinogen mg/dL 238.1 216.8 135 85.2 105.7 64.3 200–400
LDH U/L 5,727.3 2,622.8 2,418.7 546.3 637.2 80–285
HBDH
5,971.2 5,826.9 4,826.9 2,871.2 1,163.6 74–182
CK U/L 725 792.1 760.2 1,263.6 1,294.2 38–174
CKMB U/L 75 71 58 65 68 –25
BNP pg/mL 37 73 482 421 1,332 –100
MYO ng/mL 636.6 762.1 364.6 ≥1200 ≥1200 ≥1200 ≥1200 0–140
TnI ng/mL 0.03 0.04 0.05 0.16 0.14 2.84 8.94 0–0.03
CREA µmol/L 705.6 683.6 523.6 374 259.6 241.8 211.4 59–104
BUN mmol/L 20.13 25.33 13.33 7.84 4.23 3.92 3.41 1.7–8.3
AMY U/L 232.8 394.6 513.7 642.9 538.9 0–115
LPS U/L 227.6 526.9 487.9 437.8 414.5 5.6–51.3
K mmol/L 4.19 4.64 4.34 4.83 4.53 4.37 5.74 3.5–5.3
Na mmol/L 136.3 135.7 142.1 140.8 144.8 143.6 144.2 137–147
Cl mmol/L 91.2 92.9 96.6 99.2 102.1 99.5 105.2 99–110
Ca mmol/L 1.74 1.64 2.25 2.35 2.16 2.03 2.29 2.2–2.55
P mmol/L 2.96 3.23 1.47 1.15 0.97 1.57 1.63 0.81–1.45
Lac mmol/L 2.32 2.42 2.19 2.66 6.15 5.46 1.33–1.78
CRP mg/L 43.6 38.6 28.6 21.5 4.3 6.4 0–5
PCT ng/mL 0.57 1.35 2.26 1.79 3.48 5.92 –0.05
IL-6
165.9 58.3 74.6 737.2 –7
CD3+CD4+ T cells per µL 174 153 184 243 370 252 706–1,125
CD3+CD8+ T cells per µL 142 135 126 132 511 410 323–836

The constants dataset contains values for hundreds of fundamental physical constants. We’ll take a subset of values that have some molar basis and generate a gt table from that. Like the illness dataset, this one has a units column so, again, the fmt_units() function will be used to format those units. Here, the preference for typesetting measurement units is to have positive and negative exponents (e.g., not "<unit_1> / <unit_2>" but rather "<unit_1> <unit_2>^-1").

constants |>
  dplyr::filter(grepl("molar", name)) |>
  gt() |>
  cols_hide(columns = c(uncert, starts_with("sf"))) |>
  fmt_units(columns = units) |>
  fmt_scientific(columns = value, decimals = 3) |>
  tab_header(title = "Physical Constants Having a Molar Basis") |>
  tab_options(column_labels.hidden = TRUE)
Physical Constants Having a Molar Basis
alpha particle molar mass 4.002 × 10−3 kg mol−1
deuteron molar mass 2.014 × 10−3 kg mol−1
electron molar mass 5.486 × 10−7 kg mol−1
helion molar mass 3.015 × 10−3 kg mol−1
molar gas constant 8.314 J mol−1 K−1
molar mass constant 1.000 × 10−3 kg mol−1
molar mass of carbon-12 1.200 × 10−2 kg mol−1
molar Planck constant 3.990 × 10−10 J Hz−1 mol−1
molar volume of ideal gas (273.15 K, 100 kPa) 2.271 × 10−2 m3 mol−1
molar volume of ideal gas (273.15 K, 101.325 kPa) 2.241 × 10−2 m3 mol−1
molar volume of silicon 1.206 × 10−5 m3 mol−1
muon molar mass 1.134 × 10−4 kg mol−1
neutron molar mass 1.009 × 10−3 kg mol−1
proton molar mass 1.007 × 10−3 kg mol−1
tau molar mass 1.908 × 10−3 kg mol−1
triton molar mass 3.016 × 10−3 kg mol−1