The text_transform() function

Use a subset of the sp500 dataset to create a gt table. Transform the text in the date column using a function supplied to text_transform() (via the fn argument). Note that the x in the fn = function (x) part consists entirely of ISO 8601 date strings (which are acceptable as input to vec_fmt_date() and vec_fmt_datetime()).

sp500 |>
  dplyr::slice_head(n = 10) |>
  dplyr::select(date, open, close) |>
  dplyr::arrange(-dplyr::row_number()) |>
  gt() |>
  fmt_currency() |>
  text_transform(
    fn = function(x) {
      paste0(
        "<strong>",
        vec_fmt_date(x, date_style = "m_day_year"),
        "</strong>",
        "&mdash;W",
        vec_fmt_datetime(x, format = "w")
      )
    },
    locations = cells_body(columns = date)
  ) |>
  cols_label(
    date = "Date and Week",
    open = "Opening Price",
    close = "Closing Price"
  )
Date and Week Opening Price Closing Price
Dec 17, 2015—W51 $2,073.76 $2,041.89
Dec 18, 2015—W51 $2,040.81 $2,005.55
Dec 21, 2015—W52 $2,010.27 $2,021.15
Dec 22, 2015—W52 $2,023.15 $2,038.97
Dec 23, 2015—W52 $2,042.20 $2,064.29
Dec 24, 2015—W52 $2,063.52 $2,060.99
Dec 28, 2015—W53 $2,057.77 $2,056.50
Dec 29, 2015—W53 $2,060.54 $2,078.36
Dec 30, 2015—W53 $2,077.34 $2,063.36
Dec 31, 2015—W53 $2,060.59 $2,043.94

Let’s use a summarized version of the gtcars dataset to create a gt table. First, the numeric values in the n column are formatted as spelled-out numbers with fmt_spelled_num(). The output values are indeed spelled out but exclusively with lowercase letters. We actually want these words to begin with a capital letter and end with a period. To make this possible, text_transform() will be used since it can modify already-formatted text. Through the fn argument, we provide a custom function that uses R’s toTitleCase() operating on x (the numbers-as-text strings) within paste0() so that a period can be properly placed.

gtcars |>
  dplyr::filter(ctry_origin %in% c("Germany", "Italy", "Japan")) |>
  dplyr::count(mfr, ctry_origin, sort = TRUE) |>
  dplyr::arrange(ctry_origin) |>
  gt(rowname_col = "mfr", groupname_col = "ctry_origin") |>
  cols_label(n = "No. of Entries") |>
  tab_stub_indent(rows = everything(), indent = 2) |>
  cols_align(align = "center", columns = n) |>
  fmt_spelled_num() |>
  text_transform(
    fn = function(x) {
      paste0(tools::toTitleCase(x), ".")
    },
    locations = cells_body(columns = n)
  )
No. of Entries
Germany
Audi Five.
BMW Five.
Porsche Four.
Mercedes-Benz Two.
Italy
Ferrari Nine.
Lamborghini Three.
Maserati Three.
Japan
Acura One.
Nissan One.

There may be occasions where you’d want to remove all text. Here in this example based on the pizzaplace dataset, we generate a gt table that summarizes an entire year of data by colorizing the daily sales revenue. Individual cell values are not needed here (since the encoding by color suffices), so, text_transform() is used to turn every value to an empty string: "".

pizzaplace |>
  dplyr::group_by(date) |>
  dplyr::summarize(rev = sum(price)) |>
  dplyr::ungroup() |>
  dplyr::mutate(
    month = lubridate::month(date, label = TRUE),
    day_num = lubridate::mday(date)
  ) |>
  dplyr::select(-date) |>
  tidyr::pivot_wider(names_from = month, values_from = rev) |>
  gt(rowname_col = "day_num") |>
  data_color(
    method = "numeric",
    palette = "wesanderson::Zissou1",
    na_color = "white"
  ) |>
  text_transform(
    fn = function(x) "",
    locations = cells_body()
  ) |>
  opt_table_lines(extent = "none") |>
  opt_all_caps() |>
  cols_width(everything() ~ px(35)) |>
  cols_align(align = "center")
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31