The sub_large_vals() function

Let’s generate a simple, single-column tibble that contains an assortment of values that could potentially undergo some substitution.

tbl <- dplyr::tibble(num = c(0, NA, 10^(8:14)))

tbl
# A tibble: 9 × 1
    num
  <dbl>
1  0   
2 NA   
3  1e 8
4  1e 9
5  1e10
6  1e11
7  1e12
8  1e13
9  1e14

The tbl object contains a variety of larger numbers and some might be larger enough to reformat with a threshold value. With sub_large_vals() we can do just that:

tbl |>
  gt() |>
  fmt_number(columns = num) |>
  sub_large_vals()
num
0.00
NA
100,000,000.00
1,000,000,000.00
10,000,000,000.00
100,000,000,000.00
≥1e+12
≥1e+12
≥1e+12

Large negative values can also be handled but they are handled specially by the sign parameter. Setting that to "-" will format only the large values that are negative. Notice that with the default large_pattern value of ">={x“} the ">=" is automatically changed to "<=".

tbl |>
  dplyr::mutate(num = -num) |>
  gt() |>
  fmt_number(columns = num) |>
  sub_large_vals(sign = "-")
num
0.00
NA
−100,000,000.00
−1,000,000,000.00
−10,000,000,000.00
−100,000,000,000.00
≤-1e+12
≤-1e+12
≤-1e+12

You don’t have to settle with the default threshold value or the default replacement pattern (in large_pattern). This can be changed and the "{x“} in large_pattern (which uses the threshold value) can even be omitted.

tbl |>
  gt() |>
  fmt_number(columns = num) |>
  sub_large_vals(
    threshold = 5E10,
    large_pattern = "hugemongous"
  )
num
0.00
NA
100,000,000.00
1,000,000,000.00
10,000,000,000.00
hugemongous
hugemongous
hugemongous
hugemongous