The fmt_flag() function

Use the countrypops dataset to create a gt table. We will only include a few columns and rows from that table. The country_code_2 column has 2-letter country codes in the format required for fmt_flag() and using that function transforms the codes to circular flag icons.

countrypops |>
  dplyr::filter(year == 2021) |>
  dplyr::filter(grepl("^S", country_name)) |>
  dplyr::arrange(country_name) |>
  dplyr::select(-country_name, -year) |>
  dplyr::slice_head(n = 10) |>
  gt() |>
  fmt_integer() |>
  fmt_flag(columns = country_code_2) |>
  fmt_country(columns = country_code_3) |>
  cols_label(
    country_code_2 = "",
    country_code_3 = "Country",
    population = "Population (2021)"
  )
Country Population (2021)
Samoa 218,764
San Marino 33,745
São Tomé & Príncipe 223,107
Saudi Arabia 35,950,396
Senegal 16,876,720
Serbia 6,834,326
Seychelles 99,258
Sierra Leone 8,420,641
Singapore 5,453,566
Sint Maarten 42,846

Using countrypops we can generate a table that provides populations every five years for the Benelux countries ("BE", "NL", and "LU"). This requires some manipulation with dplyr and tidyr before introducing the table to gt. With fmt_flag() we can obtain flag icons in the country_code_2 column. After that, we can merge the flag icons into the stub column, generating row labels that have a combination of icon and text.

countrypops |>
  dplyr::filter(country_code_2 %in% c("BE", "NL", "LU")) |>
  dplyr::filter(year %% 10 == 0) |>
  dplyr::select(country_name, country_code_2, year, population) |>
  tidyr::pivot_wider(names_from = year, values_from = population) |>
  dplyr::slice(1, 3, 2) |>
  gt(rowname_col = "country_name") |>
  tab_header(title = "Populations of the Benelux Countries") |>
  tab_spanner(columns = everything(), label = "Year") |>
  fmt_integer() |>
  fmt_flag(columns = country_code_2) |>
  cols_merge(
    columns = c(country_name, country_code_2),
    pattern = "{2} {1}"
  )
Populations of the Benelux Countries
Year
1960 1970 1980 1990 2000 2010 2020
Belgium 9,153,489 9,655,549 9,859,242 9,967,379 10,251,250 10,895,586 11,538,604
Netherlands 11,486,631 13,038,526 14,149,800 14,951,510 15,925,513 16,615,394 17,441,500
Luxembourg 313,970 339,171 364,150 381,850 436,300 506,953 630,419

fmt_flag() works well even when there are multiple country codes within the same cell. It can operate on comma-separated codes without issue. When rendered to HTML, hovering over each of the flag icons results in tooltip text showing the name of the country.

countrypops |>
  dplyr::filter(year == 2021, population < 100000) |>
  dplyr::select(country_code_2, population) |>
  dplyr::mutate(population_class = cut(
    population,
    breaks = scales::breaks_pretty(n = 5)(population)
    )
  ) |>
  dplyr::group_by(population_class) |>
  dplyr::summarize(
    countries = paste0(country_code_2, collapse = ",")
  ) |>
  dplyr::arrange(desc(population_class)) |>
  gt() |>
  tab_header(title = "Countries with Small Populations") |>
  fmt_flag(columns = countries) |>
  fmt_bins(
    columns = population_class,
    fmt = ~ fmt_integer(., suffixing = TRUE)
  ) |>
  cols_label(
    population_class = "Population Range",
    countries = "Countries"
  ) |>
  cols_width(population_class ~ px(150))
Countries with Small Populations
Population Range Countries
80K–100K
60K–80K
40K–60K
20K–40K
0–20K