The fmt_country() function

The peeps dataset will be used to generate a small gt table containing only the people born in the 1980s. The country column contains 3-letter country codes and those will be transformed to country names with fmt_country().

peeps |>
  dplyr::filter(grepl("198", dob)) |>
  dplyr::select(name_given, name_family, country, dob) |>
  dplyr::arrange(country, name_family) |>
  gt() |>
  fmt_country(columns = country) |>
  cols_merge(columns = c(name_given, name_family)) |>
  opt_vertical_padding(scale = 0.5) |>
  tab_options(column_labels.hidden = TRUE)
Lucinda Goldhar Australia 1984-08-07
Christopher Hansen Australia 1985-02-25
Brandon Pugliesi Australia 1987-08-23
Alannah Stead Australia 1986-02-19
Sezer Limburg Belgium 1987-04-26
Amélie Perillard Belgium 1987-11-06
Matilda Bates Canada 1988-05-13
Jennifer Dale Canada 1983-05-05
Hjalte Nielsen Denmark 1981-08-01
Rolla Skov Denmark 1986-04-03
Askalu Fikru United Kingdom 1984-06-22
Pava Štritof United Kingdom 1984-08-23
Declan Saunders New Zealand 1983-01-30
Faith Steele New Zealand 1989-05-25
Daniel Wood New Zealand 1982-06-06
Konstancja Sobczak Poland 1984-04-26
Vejsil Crevar Slovenia 1988-01-02
Pankracij Konovšek Slovenia 1980-02-13
Marša Možgon Slovenia 1985-01-17
Zorka Paić Slovenia 1985-03-09
Feride Šijan Slovenia 1988-07-12
Martin Bartůněk United States 1988-07-24
Leonard Jacobs United States 1985-10-01

Use the countrypops dataset to create a gt table. We will only include a few columns and rows from that table. The country_code_3 column has 3-letter country codes in the format required for fmt_country() and using that function transforms the codes to country names.

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

The country names derived from country codes can be localized. Let’s translate some of those country names into three different languages using different locale values in separate calls of fmt_country().

countrypops |>
  dplyr::filter(year == 2021) |>
  dplyr::arrange(desc(population)) |>
  dplyr::filter(
    dplyr::row_number() > max(dplyr::row_number()) - 5 |
    dplyr::row_number() <= 5
  ) |>
  dplyr::select(
    country_code_fl = country_code_2,
    country_code_2a = country_code_2,
    country_code_2b = country_code_2,
    country_code_2c = country_code_2,
    population
  ) |>
  gt(rowname_col = "country_code_fl") |>
  fmt_integer() |>
  fmt_flag(columns = stub()) |>
  fmt_country(columns = ends_with("a")) |>
  fmt_country(columns = ends_with("b"), locale = "ja") |>
  fmt_country(columns = ends_with("c"), locale = "ar") |>
  cols_label(
    ends_with("a") ~ "`en`",
    ends_with("b") ~ "`ja`",
    ends_with("c") ~ "`ar`",
    population = "Population",
    .fn = md
  ) |>
  tab_spanner(
    label = "Country name in specified locale",
    columns = matches("2a|2b|2c")
  ) |>
  cols_align(align = "center", columns = matches("2a|2b|2c")) |>
  opt_horizontal_padding(scale = 2)
Country name in specified locale
Population
en ja ar
China 中国

الصين

1,412,360,000
India インド

الهند

1,407,563,842
United States アメリカ合衆国

الولايات المتحدة

332,031,554
Indonesia インドネシア

إندونيسيا

273,753,191
Pakistan パキスタン

باكستان

231,402,117
St. Martin サン・マルタン

سان مارتن

31,948
British Virgin Islands 英領ヴァージン諸島

جزر فيرجن البريطانية

31,122
Palau パラオ

بالاو

18,024
Nauru ナウル

ناورو

12,511
Tuvalu ツバル

توفالو

11,204

Let’s make another gt table, this time using the films dataset. The countries_of_origin column contains 2-letter country codes and some cells contain multiple countries (separated by commas). We’ll use fmt_country() on that column and also specify that the rendered country names should be separated by a comma and a space character. Also note that historical country codes like "SU" (‘USSR’), "CS" (‘Czechoslovakia’), and "YU" (‘Yugoslavia’) are permitted as inputs for fmt_country().

films |>
  dplyr::filter(year == 1959) |>
  dplyr::select(
    contains("title"), run_time, director, countries_of_origin, imdb_url
  ) |>
  gt() |>
  tab_header(title = "Feature Films in Competition at the 1959 Festival") |>
  fmt_country(columns = countries_of_origin, sep = ", ") |>
  fmt_url(
    columns = imdb_url,
    label = fontawesome::fa("imdb", fill = "black")
  ) |>
  cols_merge(
    columns = c(title, original_title, imdb_url),
    pattern = "{1}<< ({2})>> {3}"
  ) |>
  cols_label(
    title = "Film",
    run_time = "Length",
    director = "Director",
    countries_of_origin = "Country"
  ) |>
  opt_vertical_padding(scale = 0.5) |>
  opt_table_font(stack = "classical-humanist", weight = "bold") |>
  opt_stylize(style = 1, color = "gray") |>
  tab_options(heading.title.font.size = px(26))
Feature Films in Competition at the 1959 Festival
Film Length Director Country
Araya 1h 30m Margot Benacerraf Venezuela, France
Compulsion 1h 43m Richard Fleischer United States
Eva (Die Halbzarte) 1h 32m Rolf Thiele Austria
Fanfare 1h 26m Bert Haanstra Netherlands
Miss April (Fröken April) 1h 38m Göran Gentele Sweden
Arms and the Man (Helden) 1h 40m Franz Peter Wirth Germany
Hiroshima mon amour 1h 30m Alain Resnais France, Japan
Court Martial (Kriegsgericht) 1h 24m Kurt Meisel Germany
The Soldiers of Pancho Villa (La cucaracha) 1h 37m Ismael Rodríguez Mexico
Lajwanti 2h Narendra Suri India
The 400 Blows (Les quatre cents coups) 1h 39m François Truffaut France
Honeymoon (Luna de miel) 1h 49m Michael Powell United Kingdom, Spain
Bloody Twilight (Matomeno iliovasilemma) 1h 28m Andreas Labrinos Greece
Middle of the Night 1h 58m Delbert Mann United States
Nazarín 1h 34m Luis Buñuel Mexico
Black Orpheus (Orfeu Negro) 1h 40m Marcel Camus Brazil, France, Italy
A Home for Tanya (Otchiy dom) 1h 40m Lev Kulidzhanov USSR
Policarpo (Policarpo 'ufficiale di scrittura') 1h 44m Mario Soldati Italy, France, Spain
Portuguese Rhapsody (Rapsódia Portuguesa) 1h 26m João Mendes Portugal
Room at the Top 1h 57m Jack Clayton United Kingdom
A Midsummer Night's Dream (Sen noci svatojánské) 1h 16m Jirí Trnka Czechoslovakia
The Snowy Heron (Shirasagi) 1h 37m Teinosuke Kinugasa Japan
Stars (Sterne) 1h 31m Konrad Wolf East Germany, Bulgaria
The Sinner (Tang fu yu sheng nu) 1h 30m Shen Tien Taiwan
The Diary of Anne Frank 3h George Stevens United States
Desire (Touha) 1h 35m Vojtech Jasný Czechoslovakia
Train Without a Timetable (Vlak bez voznog reda) 2h 1m Veljko Bulajic Yugoslavia
Sugar Harvest (Zafra) 1h 17m Lucas Demare Argentina
Édes Anna 1h 24m Zoltán Fábri Hungary

Country names can sometimes pair nicely with flag-based graphics. In this example (using a different portion of the films dataset) we use fmt_country() along with fmt_flag(). The formatted country names are then merged into the same cells as the icons via cols_merge().

films |>
  dplyr::filter(director == "Jean-Pierre Dardenne, Luc Dardenne") |>
  dplyr::select(title, year, run_time, countries_of_origin) |>
  gt() |>
  tab_header(title = "In Competition Films by the Dardenne Bros.") |>
  cols_add(country_flag = countries_of_origin) |>
  fmt_flag(columns = country_flag) |>
  fmt_country(columns = countries_of_origin, sep = ", ") |>
  cols_merge(
    columns = c(countries_of_origin, country_flag),
    pattern = "{2}<br>{1}"
  ) |>
  tab_style(
    style = cell_text(size = px(9)),
    locations = cells_body(columns = countries_of_origin)
  ) |>
  cols_merge(columns = c(title, year), pattern = "{1} ({2})") |>
  opt_vertical_padding(scale = 0.5) |>
  opt_horizontal_padding(scale = 3) |>
  opt_table_font(font = google_font("PT Sans")) |>
  opt_stylize(style = 1, color = "blue") |>
  tab_options(
    heading.title.font.size = px(26),
    column_labels.hidden = TRUE
  )
In Competition Films by the Dardenne Bros.
Rosetta (1999) 1h 35m
France, Belgium
The Son (2002) 1h 43m
Belgium, France
The Child (2005) 1h 35m
Belgium, France
The Silence of Lorna (2008) 1h 45m
Belgium, France, Italy, Germany
The Kid with a Bike (2011) 1h 27m
Belgium, France, Italy
Two Days, One Night (2014) 1h 35m
Belgium, France, Italy
The Unknown Girl (2016) 1h 53m
Belgium, France
Young Ahmed (2019) 1h 25m
Belgium, France
Tori and Lokita (2022) 1h 28m
Belgium, France