The fmt_scientific() function

Let’s define a data frame that contains two columns of values (one small and one large). After creating a simple gt table from small_large_tbl we’ll call fmt_scientific() on both columns.

small_large_tbl <-
  dplyr::tibble(
    small = 10^(-12:-1),
    large = 10^(1:12)
  )

small_large_tbl |>
  gt() |>
  fmt_scientific()
small large
1.00 × 10−12 1.00 × 101
1.00 × 10−11 1.00 × 102
1.00 × 10−10 1.00 × 103
1.00 × 10−9 1.00 × 104
1.00 × 10−8 1.00 × 105
1.00 × 10−7 1.00 × 106
1.00 × 10−6 1.00 × 107
1.00 × 10−5 1.00 × 108
1.00 × 10−4 1.00 × 109
1.00 × 10−3 1.00 × 1010
1.00 × 10−2 1.00 × 1011
1.00 × 10−1 1.00 × 1012

The default method of styling the notation uses the ‘m x 10^n’ construction but this can be changed to a ‘mEn’ style via the exp_style argument. We can supply any single letter here and optionally affix a "1" to indicate there should not be any zero-padding of the n value. Two calls of fmt_scientific() are used here to show different options for styling in scientific notation.

small_large_tbl |>
  gt() |>
  fmt_scientific(
    columns = small,
    exp_style = "E"
  ) |>
  fmt_scientific(
    columns = large,
    exp_style = "e1",
    force_sign_n = TRUE
  )
small large
1.00E−12 1.00e+1
1.00E−11 1.00e+2
1.00E−10 1.00e+3
1.00E−09 1.00e+4
1.00E−08 1.00e+5
1.00E−07 1.00e+6
1.00E−06 1.00e+7
1.00E−05 1.00e+8
1.00E−04 1.00e+9
1.00E−03 1.00e+10
1.00E−02 1.00e+11
1.00E−01 1.00e+12

Taking a portion of the reactions dataset, we can create a gt table that contains reaction rate constants that should be expressed in scientific notation. All of the numeric values in the filtered table require that type of formatting so fmt_scientific() can be called without requiring any specification of column names in the columns argument. By default, the number of decimal places is fixed to 2, which is fine for this table.

reactions |>
  dplyr::filter(cmpd_type == "mercaptan") |>
  dplyr::select(cmpd_name, cmpd_formula, OH_k298, Cl_k298, NO3_k298) |>
  gt(rowname_col = "cmpd_name") |>
  tab_header(title = "Gas-phase reactions of selected mercaptan compounds") |>
  tab_spanner(
    label = md("Reaction Rate Constant (298 K),<br>{{cm^3 molecules^-1 s^-1}}"),
    columns = ends_with("k298")
  ) |>
  fmt_chem(columns = cmpd_formula) |>
  fmt_scientific() |>
  sub_missing() |>
  cols_label(
    cmpd_formula = "",
    OH_k298 = "OH",
    NO3_k298 = "{{%NO3%}}",
    Cl_k298 = "Cl"
  ) |>
  opt_stylize() |>
  opt_horizontal_padding(scale = 3) |>
  opt_table_font(font = google_font("IBM Plex Sans")) |>
  tab_options(stub.font.weight = "500")
Gas-phase reactions of selected mercaptan compounds
Reaction Rate Constant (298 K),
cm3 molecules−1 s−1
OH Cl NO3
methanethiol CH4S 3.50 × 10−11 2.00 × 10−10 9.20 × 10−13
ethanethiol C2H6S 4.50 × 10−11 1.75 × 10−10 1.21 × 10−12
propanethiol C3H8S 5.30 × 10−11 2.14 × 10−10
2-propanethiol C3H8S 3.90 × 10−11 2.70 × 10−10
1-butanethiol C4H10S 5.60 × 10−11
2-methyl-1-propanethiol C4H10S 4.60 × 10−11
2-butanethiol C4H10S 3.80 × 10−11 1.65 × 10−10
t-butylsulfide C4H10S 2.90 × 10−11
2-methylbutanethiol C5H12S 5.20 × 10−11
n-pentanethiol C5H12S 1.97 × 10−10
1,2-ethanedithiol C2H6S2 3.80 × 10−11

The constants table contains a plethora of data on the fundamental physical constants and values range from very small to very large, warranting the use of figures in scientific notation. Because the values differ in the degree of measurement precision, the dataset has columns (sf_value and sf_uncert) that include the number of significant figures for each measurement value and for the associated uncertainty. We can use the n_sigfig argument of fmt_scientific() in conjunction with the from_column() helper to format each value and its uncertainty to the proper number of significant digits.

constants |>
  dplyr::filter(grepl("Planck", name)) |>
  gt() |>
  fmt_scientific(
    columns = value,
    n_sigfig = from_column(column = "sf_value")
  ) |>
  fmt_scientific(
    columns = uncert,
    n_sigfig = from_column(column = "sf_uncert")
  ) |>
  cols_hide(columns = starts_with("sf")) |>
  fmt_units(columns = units) |>
  sub_missing(missing_text = "")
name value uncert units
molar Planck constant 3.990312712 × 10−10
J Hz−1 mol−1
Planck constant 6.62607015 × 10−34
J Hz−1
Planck constant in eV/Hz 4.135667696 × 10−15
eV Hz−1
Planck length 1.616255 × 10−35 1.8 × 10−40 m
Planck mass 2.176434 × 10−8 2.4 × 10−13 kg
Planck mass energy equivalent in GeV 1.220890 × 1019 1.4 × 1014 GeV
Planck temperature 1.416784 × 1032 1.6 × 1027 K
Planck time 5.391247 × 10−44 6.0 × 10−49 s
reduced Planck constant 1.054571817 × 10−34
J s
reduced Planck constant in eV s 6.582119569 × 10−16
eV s
reduced Planck constant times c in MeV fm 1.973269804 × 102
MeV fm