The fmt_chem() function

Let’s use the reactions dataset and create a new gt table. The table will be filtered down to only a few rows and columns. The column cmpd_formula contains chemical formulas and the formatting of those will be performed by fmt_chem(). Certain column labels with chemical names (o3_k298 and no3_k298) can be handled within cols_label() by using surrounding the text with "{{%"/"%}“}.

reactions |>
  dplyr::filter(cmpd_type == "terminal monoalkene") |>
  dplyr::filter(grepl("^1-", cmpd_name)) |>
  dplyr::select(cmpd_name, cmpd_formula, ends_with("k298")) |>
  gt() |>
  tab_header(title = "Gas-phase reactions of selected terminal alkenes") |>
  tab_spanner(
    label = "Reaction Rate Constant at 298 K",
    columns = ends_with("k298")
  ) |>
  fmt_chem(columns = cmpd_formula) |>
  fmt_scientific() |>
  sub_missing() |>
  cols_label(
    cmpd_name = "Alkene",
    cmpd_formula = "Formula",
    OH_k298 = "OH",
    O3_k298 = "{{%O3%}}",
    NO3_k298 = "{{%NO3%}}",
    Cl_k298 = "Cl"
  ) |>
  opt_align_table_header(align = "left")
Gas-phase reactions of selected terminal alkenes
Alkene Formula
Reaction Rate Constant at 298 K
OH O3 NO3 Cl
1-butene C4H8 3.10 × 10−11 1.00 × 10−17 1.30 × 10−14 3.00 × 10−10
1-pentene C5H10 3.22 × 10−11 1.06 × 10−17 1.50 × 10−14 4.20 × 10−10
1-hexene C6H12 3.70 × 10−11 1.15 × 10−17 1.80 × 10−14 4.00 × 10−10
1-heptene C7H14 3.88 × 10−11 1.16 × 10−17 2.00 × 10−14 4.40 × 10−10
1-octene C8H16 3.44 × 10−11 1.01 × 10−17 2.50 × 10−14 5.50 × 10−10
1-nonene C9H18 4.32 × 10−11 9.90 × 10−18 5.90 × 10−10
1-decene C10H20 4.61 × 10−11 1.11 × 10−17 2.60 × 10−14
1-undecene C11H22 4.79 × 10−11 1.03 × 10−17
1-dodecene C12H24 5.03 × 10−11 1.03 × 10−17 2.80 × 10−14
1-tridecene C13H26 5.09 × 10−11 9.60 × 10−18
1-tetradecene C14H28 4.96 × 10−11 9.70 × 10−18 2.80 × 10−14

Taking just a few rows from the photolysis dataset, let’s create a new gt table. The cmpd_formula and products columns both contain text in chemistry notation (the first has compounds, and the second column has the products of photolysis reactions). These columns will be formatted by fmt_chem(). The compound formulas will be merged with the compound names with cols_merge().

photolysis |>
  dplyr::filter(cmpd_name %in% c(
    "hydrogen peroxide", "nitrous acid",
    "nitric acid", "acetaldehyde",
    "methyl peroxide", "methyl nitrate",
    "ethyl nitrate", "isopropyl nitrate"
  )) |>
  dplyr::select(-c(l, m, n, quantum_yield, type)) |>
  gt() |>
  tab_header(title = "Photolysis pathways of selected VOCs") |>
  fmt_chem(columns = c(cmpd_formula, products)) |>
  cols_nanoplot(
    columns = sigma_298_cm2,
    columns_x_vals = wavelength_nm,
    expand_x = c(200, 400),
    new_col_name = "cross_section",
    new_col_label = "Absorption Cross Section",
    options = nanoplot_options(
      show_data_points = FALSE,
      data_line_stroke_width = 4,
      data_line_stroke_color = "black",
      show_data_area = FALSE
    )
  ) |>
  cols_merge(
    columns = c(cmpd_name, cmpd_formula),
    pattern = "{1}, {2}"
  ) |>
  cols_label(
    cmpd_name = "Compound",
    products = "Products"
  ) |>
  opt_align_table_header(align = "left")
Photolysis pathways of selected VOCs
Compound Products Absorption Cross Section
hydrogen peroxide, H2O2 OH + OH
6.7E−19 4.0E−22 6.7E−19 5.6E−19 4.7E−19 4.1E−19 3.6E−19 3.1E−19 2.6E−19 2.2E−19 1.8E−19 1.5E−19 1.2E−19 1.0E−19 8.3E−20 6.7E−20 5.3E−20 4.2E−20 3.3E−20 2.6E−20 2.0E−20 1.5E−20 1.2E−20 9.0E−21 6.8E−21 5.1E−21 3.9E−21 2.9E−21 2.2E−21 1.6E−21 1.3E−21 1.0E−21 7.0E−22 5.0E−22 4.0E−22
nitrous acid, HONO OH + NO
5.1E−19 0 0 0 0 0 0 0 0 0 0 0 8.6E−23 8.5E−22 8.6E−23 4.3E−21 6.8E−21 1.3E−20 7.7E−21 3.4E−21 9.4E−21 1.6E−20 1.0E−20 1.5E−20 2.4E−20 2.0E−20 3.1E−20 2.5E−20 1.3E−20 2.7E−20 4.9E−20 3.0E−20 4.1E−20 6.8E−20 4.1E−20 4.4E−20 6.2E−20 4.1E−20 2.4E−20 5.5E−20 1.0E−19 5.1E−20 9.5E−20 1.6E−19 7.4E−20 6.5E−20 8.2E−20 8.4E−20 6.2E−20 4.4E−20 8.3E−20 1.6E−19 8.5E−20 1.4E−19 3.4E−19 1.3E−19 8.2E−20 9.3E−20 1.1E−19 8.7E−20 7.6E−20 6.7E−20 9.3E−20 1.8E−19 1.3E−19 1.6E−19 5.1E−19 3.1E−19 1.2E−19 9.9E−20 1.0E−19 8.8E−20 7.7E−20 6.9E−20 6.8E−20 8.2E−20 1.3E−19 1.5E−19 1.5E−19 2.6E−19 4.5E−19 3.3E−19 1.5E−19 9.6E−20 8.7E−20 6.5E−20 5.6E−20 4.5E−20 4.5E−20 4.3E−20 5.0E−20 6.8E−20 8.1E−20 9.7E−20 1.4E−19 1.8E−19 2.1E−19 1.7E−19 1.1E−19 7.7E−20 4.8E−20 2.9E−20 2.3E−20 1.7E−20 1.2E−20 9.4E−21 5.1E−21 8.5E−21 3.4E−21 4.3E−21 1.1E−20 8.5E−22 0
nitric acid, HNO3 OH + NO2
1.6E−17 4.1E−24 1.6E−17 1.5E−17 1.4E−17 1.2E−17 1.1E−17 9.4E−18 7.7E−18 5.9E−18 4.5E−18 3.3E−18 2.3E−18 1.6E−18 1.0E−18 6.7E−19 4.4E−19 2.9E−19 2.0E−19 1.5E−19 1.2E−19 9.6E−20 8.0E−20 6.8E−20 5.7E−20 4.9E−20 4.1E−20 3.4E−20 2.9E−20 2.6E−20 2.3E−20 2.2E−20 2.1E−20 2.0E−20 2.0E−20 2.0E−20 2.0E−20 2.0E−20 1.9E−20 1.9E−20 1.9E−20 1.8E−20 1.8E−20 1.7E−20 1.6E−20 1.5E−20 1.4E−20 1.3E−20 1.2E−20 1.1E−20 1.0E−20 9.1E−21 8.1E−21 7.1E−21 6.2E−21 5.3E−21 4.5E−21 3.8E−21 3.2E−21 2.6E−21 2.1E−21 1.7E−21 1.3E−21 1.1E−21 8.1E−22 6.3E−22 4.7E−22 3.6E−22 2.7E−22 2.0E−22 1.5E−22 1.1E−22 8.2E−23 6.1E−23 4.3E−23 3.2E−23 2.4E−23 2.0E−23 1.4E−23 1.0E−23 8.6E−24 6.9E−24 5.0E−24 4.1E−24 4.2E−24
acetaldehyde, CH3CHO HCO + CH3
4.9E−20 0 5.6E−22 5.3E−22 4.9E−22 4.8E−22 5.2E−22 6.5E−22 9.6E−22 1.5E−21 2.4E−21 3.8E−21 5.6E−21 8.2E−21 1.1E−20 1.5E−20 2.0E−20 2.4E−20 3.1E−20 3.4E−20 4.0E−20 4.2E−20 4.5E−20 4.7E−20 4.7E−20 4.8E−20 4.6E−20 4.5E−20 4.4E−20 4.6E−20 4.7E−20 4.8E−20 4.9E−20 4.8E−20 4.7E−20 4.5E−20 4.3E−20 4.3E−20 4.2E−20 4.4E−20 4.4E−20 4.3E−20 4.2E−20 4.0E−20 3.9E−20 3.7E−20 3.5E−20 3.4E−20 3.4E−20 3.4E−20 3.3E−20 3.1E−20 2.9E−20 2.8E−20 2.5E−20 2.5E−20 2.4E−20 2.2E−20 2.0E−20 2.1E−20 2.0E−20 1.9E−20 1.7E−20 1.5E−20 1.4E−20 1.2E−20 1.1E−20 1.1E−20 1.1E−20 8.6E−21 7.5E−21 7.1E−21 6.9E−21 5.9E−21 5.3E−21 4.0E−21 3.6E−21 3.5E−21 2.4E−21 2.2E−21 2.0E−21 2.2E−21 1.5E−21 7.4E−22 4.2E−22 3.1E−22 2.6E−22 2.1E−22 1.9E−22 1.5E−22 1.6E−22 1.0E−22 8.0E−23 7.0E−23 6.0E−23 5.0E−23 5.0E−23 4.0E−23 5.0E−23 3.0E−23 4.0E−23 2.0E−23 3.0E−23 2.0E−23 1.0E−23 0 0 0
methyl peroxide, CH3OOH CH3O + OH
3.1E−19 1.2E−22 3.1E−19 2.1E−19 1.5E−19 1.2E−19 9.6E−20 7.6E−20 6.0E−20 4.9E−20 4.0E−20 3.2E−20 2.6E−20 2.1E−20 1.7E−20 1.4E−20 1.1E−20 8.6E−21 6.9E−21 5.5E−21 4.1E−21 3.1E−21 2.4E−21 1.8E−21 1.4E−21 1.1E−21 7.9E−22 6.1E−22 4.7E−22 3.5E−22 2.7E−22 2.1E−22 1.6E−22 1.2E−22
methyl nitrate, CH3ONO2 CH3O + NO2
5.9E−20 1.2E−23 5.9E−20 4.2E−20 3.6E−20 3.3E−20 3.1E−20 2.8E−20 2.4E−20 2.0E−20 1.6E−20 1.2E−20 8.5E−21 5.7E−21 3.6E−21 2.1E−21 1.3E−21 6.3E−22 3.2E−22 1.4E−22 6.6E−23 2.7E−23 1.2E−23
ethyl nitrate, C2H5ONO2 C2H5O + NO2
1.1E−19 2.5E−23 1.1E−19 7.7E−20 5.4E−20 4.5E−20 4.1E−20 3.9E−20 3.6E−20 3.1E−20 2.7E−20 2.2E−20 1.7E−20 1.2E−20 8.5E−21 5.5E−21 3.3E−21 1.9E−21 1.0E−21 5.1E−22 2.6E−22 1.2E−22 4.9E−23 2.5E−23
isopropyl nitrate, i−C3H7ONO2 i−C3H7O + NO2
1.8E−17 1.8E−24 1.8E−17 1.8E−17 1.8E−17 1.6E−17 1.3E−17 8.7E−18 5.0E−18 2.5E−18 1.3E−18 6.2E−19 3.4E−19 1.8E−19 1.1E−19 7.0E−20 5.4E−20 4.9E−20 4.6E−20 4.3E−20 3.9E−20 3.4E−20 2.8E−20 2.2E−20 1.6E−20 1.2E−20 7.8E−21 5.0E−21 2.9E−21 1.7E−21 8.5E−22 4.4E−22 2.2E−22 1.1E−22 5.3E−23 1.8E−23 8.0E−24 2.9E−24 1.8E−24

fmt_chem() can handle the typesetting of nuclide notation. Let’s take a subset of columns and rows from the nuclides dataset and make a new gt table. The contents of the nuclide column contains isotopes of hydrogen and carbon and this is placed in the table stub. Using fmt_chem() makes it so that the subscripted and superscripted values are properly formatted to the convention of formatting nuclides.

nuclides |>
  dplyr::filter(element %in% c("H", "C")) |>
  dplyr::mutate(nuclide = gsub("[0-9]+$", "", nuclide)) |>
  dplyr::select(nuclide, atomic_mass, half_life, decay_1, is_stable) |>
  gt(rowname_col = "nuclide") |>
  tab_header(title = "Isotopes of Hydrogen and Carbon") |>
  tab_stubhead(label = "Isotope") |>
  fmt_chem(columns = nuclide) |>
  fmt_scientific(columns = half_life) |>
  fmt_number(
    columns = atomic_mass,
    decimals = 4,
    scale_by = 1 / 1e6
  ) |>
  sub_missing(
    columns = half_life,
    rows = is_stable,
    missing_text = md("**STABLE**")
  ) |>
  sub_missing(columns = half_life, rows = !is_stable) |>
  sub_missing(columns = decay_1) |>
  data_color(
    columns = decay_1,
    target_columns = c(atomic_mass, half_life, decay_1),
    palette = "LaCroixColoR::PassionFruit",
    na_color = "white"
  ) |>
  cols_label_with(fn = function(x) tools::toTitleCase(gsub("_", " ", x))) |>
  cols_label(decay_1 = "Decay Mode") |>
  cols_width(
    stub() ~ px(70),
    c(atomic_mass, half_life, decay_1) ~ px(120)
  ) |>
  cols_hide(columns = c(is_stable)) |>
  cols_align(align = "center", columns = decay_1) |>
  opt_align_table_header(align = "left") |>
  opt_vertical_padding(scale = 0.5)
Isotopes of Hydrogen and Carbon
Isotope Atomic Mass Half Life Decay Mode
1
1
H
1.0078 STABLE
2
1
H
2.0141 STABLE
3
1
H
3.0160 3.89 × 108 B-
4
1
H
4.0264 N
5
1
H
5.0353 8.61 × 10−23 2N
6
1
H
6.0450 2.94 × 10−22
7
1
H
7.0527 5.07 × 10−21
8
6
C
8.0376 3.51 × 10−21 2P
9
6
C
9.0310 1.27 × 10−1 EC+B+
10
6
C
10.0169 1.93 × 101 EC+B+
11
6
C
11.0114 1.22 × 103 EC+B+
12
6
C
12.0000 STABLE
13
6
C
13.0034 STABLE
14
6
C
14.0032 1.80 × 1011 B-
15
6
C
15.0106 2.45 B-
16
6
C
16.0147 7.47 × 10−1 B-
17
6
C
17.0226 1.93 × 10−1 B-
18
6
C
18.0268 9.20 × 10−2 B-
19
6
C
19.0348 4.63 × 10−2 B-
20
6
C
20.0403 1.63 × 10−2 B-
22
6
C
22.0576 6.10 × 10−3 B-