The cols_label_with() function

Use a subset of the sp500 dataset to create a gt table. We want all the column labels to be entirely capitalized versions of the default labels but, instead of using cols_label() and rewriting each label manually in capital letters we can use cols_label_with() and instruct it to apply the toupper() function to all column labels.

sp500 |>
  dplyr::filter(
    date >= "2015-12-01" &
      date <= "2015-12-15"
  ) |>
  dplyr::select(-c(adj_close, volume)) |>
  gt() |>
  cols_label_with(fn = toupper)
DATE OPEN HIGH LOW CLOSE
2015-12-15 2025.55 2053.87 2025.55 2043.41
2015-12-14 2013.37 2022.92 1993.26 2021.94
2015-12-11 2047.27 2047.27 2008.80 2012.37
2015-12-10 2047.93 2067.65 2045.67 2052.23
2015-12-09 2061.17 2080.33 2036.53 2047.62
2015-12-08 2073.39 2073.85 2052.32 2063.59
2015-12-07 2090.42 2090.42 2066.78 2077.07
2015-12-04 2051.24 2093.84 2051.24 2091.69
2015-12-03 2080.71 2085.00 2042.35 2049.62
2015-12-02 2101.71 2104.27 2077.11 2079.51
2015-12-01 2082.93 2103.37 2082.93 2102.63

Use the countrypops dataset to create a gt table. To improve the presentation of the table, we are again going to change the default column labels via function calls supplied within cols_label_with(). We can, if we prefer, apply multiple types of column label changes in sequence with multiple calls of cols_label_with(). Here, we use the make_clean_names() functions from the janitor package and follow up with the removal of a numeral with gsub().

countrypops |>
  dplyr::filter(year == 2021) |>
  dplyr::filter(grepl("^C", country_code_3)) |>
  dplyr::select(-country_code_2, -year) |>
  head(8) |>
  gt() |>
  cols_move_to_start(columns = country_code_3) |>
  fmt_integer(columns = population) |>
  cols_label_with(
    fn = ~ janitor::make_clean_names(., case = "title")
  ) |>
  cols_label_with(
    fn = ~ gsub("[0-9]", "", .)
  )
Country Code Country Name Population
CAF Central African Republic 5,457,154
CAN Canada 38,226,498
CHE Switzerland 8,703,405
CHL Chile 19,493,184
CHN China 1,412,360,000
CIV Cote d'Ivoire 27,478,249
CMR Cameroon 27,198,628
COD Congo (DRC) 95,894,118

We can make a svelte gt table with the pizzaplace dataset. There are ways to use one instance of cols_label_with() with multiple functions called on the column labels. In the example, we use an anonymous function call (with the function(x) { ... } construction) to perform multiple mutations of x (the vector of column labels). We can even use the md() helper function with that to signal to gt that the column label should be interpreted as Markdown text.

pizzaplace |>
  dplyr::mutate(month = substr(date, 6, 7)) |>
  dplyr::count(month, name = "pizze_vendute") |>
  dplyr::mutate(frazione_della_quota = pizze_vendute / 4000) |>
  dplyr::mutate(date = paste0("2015/", month, "/01")) |>
  dplyr::select(-month) |>
  gt(rowname_col = "date") |>
  fmt_date(date, date_style = "month", locale = "it") |>
  fmt_percent(columns = frazione_della_quota) |>
  fmt_integer(columns = pizze_vendute) |>
  cols_width(everything() ~ px(100)) |>
  cols_label_with(
    fn = function(x) {
      janitor::make_clean_names(x, case = "title") |>
        toupper() |>
        stringr::str_replace_all("^|$", "**") |>
        md()
    }
  )
PIZZE VENDUTE FRAZIONE DELLA QUOTA
gennaio 4,232 105.80%
febbraio 3,961 99.02%
marzo 4,261 106.53%
aprile 4,151 103.77%
maggio 4,328 108.20%
giugno 4,107 102.68%
luglio 4,392 109.80%
agosto 4,168 104.20%
settembre 3,890 97.25%
ottobre 3,883 97.08%
novembre 4,266 106.65%
dicembre 3,935 98.38%