The fmt_url() function

Use the towny dataset to create a gt table. After some major dplyring to get a nicely formatted data table, we use the fmt_url() function on the website column to generate navigable links to websites. By default the links are underlined and the color will be chosen for you (it’s dark cyan).

towny |>
  dplyr::filter(csd_type == "city") |>
  dplyr::arrange(desc(population_2021)) |>
  dplyr::select(name, website, population_2021) |>
  dplyr::slice_head(n = 10) |>
  gt() |>
  tab_header(
    title = md("The 10 Largest Municipalities in `towny`"),
    subtitle = "Population values taken from the 2021 census."
  ) |>
  fmt_integer() |>
  fmt_url(columns = website) |>
  cols_label(
    name = "Name",
    website = "Site",
    population_2021 = "Population"
  )
The 10 Largest Municipalities in towny
Population values taken from the 2021 census.
Name Site Population
Toronto https://www.toronto.ca 2,794,356
Ottawa https://ottawa.ca 1,017,449
Mississauga https://www.mississauga.ca 717,961
Brampton https://www.brampton.ca 656,480
Hamilton https://www.hamilton.ca 569,353
London https://london.ca 422,324
Markham https://www.markham.ca 338,503
Vaughan https://www.vaughan.ca 323,103
Kitchener https://www.kitchener.ca 256,885
Windsor https://www.citywindsor.ca 229,660

Let’s try something else. We can set a static text label for the link with the label argument (and we’ll use the word "site" for this). The link underline is removable with show_underline = FALSE. With this change, it seems sensible to merge the link to the "name" column and enclose the link text in parentheses (the cols_merge() function handles all that).

towny |>
  dplyr::filter(csd_type == "city") |>
  dplyr::arrange(desc(population_2021)) |>
  dplyr::select(name, website, population_2021) |>
  dplyr::slice_head(n = 10) |>
  gt() |>
  tab_header(
    title = md("The 10 Largest Municipalities in `towny`"),
    subtitle = "Population values taken from the 2021 census."
  ) |>
  fmt_integer() |>
  fmt_url(
    columns = website,
    label = "site",
    show_underline = FALSE
  ) |>
  cols_merge(
    columns = c(name, website),
    pattern = "{1} ({2})"
  ) |>
  cols_label(
    name = "Name",
    population_2021 = "Population"
  )
The 10 Largest Municipalities in towny
Population values taken from the 2021 census.
Name Population
Toronto (site) 2,794,356
Ottawa (site) 1,017,449
Mississauga (site) 717,961
Brampton (site) 656,480
Hamilton (site) 569,353
London (site) 422,324
Markham (site) 338,503
Vaughan (site) 323,103
Kitchener (site) 256,885
Windsor (site) 229,660

The fmt_url() function allows for the styling of links as ‘buttons’. This is as easy as setting as_button = TRUE. Doing that unlocks the ability to set a button_fill color. This color can automatically selected by gt (this is the default) but here we’re using "steelblue". The label argument also accepts a function! We can choose to adapt the label text from the URLs by eliminating any leading "https://" or "www." parts.

towny |>
  dplyr::filter(csd_type == "city") |>
  dplyr::arrange(desc(population_2021)) |>
  dplyr::select(name, website, population_2021) |>
  dplyr::slice_head(n = 10) |>
  dplyr::mutate(ranking = dplyr::row_number()) |>
  gt(rowname_col = "ranking") |>
  tab_header(
    title = md("The 10 Largest Municipalities in `towny`"),
    subtitle = "Population values taken from the 2021 census."
  ) |>
  fmt_integer() |>
  fmt_url(
    columns = website,
    label = function(x) gsub("https://|www.", "", x),
    as_button = TRUE,
    button_fill = "steelblue",
    button_width = px(150)
  ) |>
  cols_move_to_end(columns = website) |>
  cols_align(align = "center", columns = website) |>
  cols_width(
    ranking ~ px(40),
    website ~ px(200)
  ) |>
  tab_options(column_labels.hidden = TRUE) |>
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_stub()
  ) %>%
  opt_vertical_padding(scale = 0.75)
The 10 Largest Municipalities in towny
Population values taken from the 2021 census.
1 Toronto 2,794,356 toronto.ca
2 Ottawa 1,017,449 ottawa.ca
3 Mississauga 717,961 mississauga.ca
4 Brampton 656,480 brampton.ca
5 Hamilton 569,353 hamilton.ca
6 London 422,324 london.ca
7 Markham 338,503 markham.ca
8 Vaughan 323,103 vaughan.ca
9 Kitchener 256,885 kitchener.ca
10 Windsor 229,660 citywindsor.ca

It’s perhaps inevitable that you’ll come across missing values in your column of URLs. The fmt_url() function will preserve input NA values, allowing you to handle them with sub_missing(). Here’s an example of that.

towny |>
  dplyr::arrange(population_2021) |>
  dplyr::select(name, website, population_2021) |>
  dplyr::slice_head(n = 10) |>
  gt() |>
  tab_header(
    title = md("The 10 Smallest Municipalities in `towny`"),
    subtitle = "Population values taken from the 2021 census."
  ) |>
  fmt_integer() |>
  fmt_url(columns = website) |>
  cols_label(
    name = "Name",
    website = "Site",
    population_2021 = "Population"
  ) |>
  sub_missing()
The 10 Smallest Municipalities in towny
Population values taken from the 2021 census.
Name Site Population
Cockburn Island https://cockburnisland.ca 16
Thornloe 92
Brethour 105
Gauthier 151
Mattawan https://mattawan.ca 153
Hilton Beach https://hiltonbeach.com 198
Opasatika https://www.opasatika.net 200
Hilliard https://townshipofhilliard.ca 215
Pelee https://www.pelee.org 230
Head, Clara and Maria https://www.townshipsofheadclaramaria.ca 267