The fmt_email() function

Let’s take ten rows from the peeps dataset and create a table of contact information with mailing addresses and email addresses. With the column that contains email addresses (email_addr), we can use fmt_email() to generate ‘mailto:’ links. Clicking any of these formatted email addresses should result in new message creation (depending on the OS integration with an email client).

peeps |>
  dplyr::filter(country == "AUS") |>
  dplyr::select(
    starts_with("name"),
    address, city, state_prov, postcode, country, email_addr
  ) |>
  dplyr::mutate(city = toupper(city)) |>
  gt(rowname_col = "name_family") |>
  tab_header(title = "Our Contacts in Australia") |>
  tab_stubhead(label = "Name") |>
  fmt_email(columns = email_addr) |>
  fmt_country(columns = country) |>
  cols_merge(
    columns = c(address, city, state_prov, postcode, country),
    pattern = "{1}<br>{2} {3} {4}<br>{5}"
  ) |>
  cols_merge(
    columns = c(name_family, name_given),
    pattern = "{1},<br>{2}"
  ) |>
  cols_label(
    address = "Mailing Address",
    email_addr = "Email"
  ) |>
  tab_style(
    style = cell_text(size = "x-small"),
    locations = cells_body(columns = address)
  ) |>
  opt_align_table_header(align = "left")
Our Contacts in Australia
Name Mailing Address Email
Christison,
Milla
34 McGregor Street
KINALUNG NSW 2880
Australia
milla_c@example.com
Stead,
Alannah
44 Mt Berryman Road
ROPELEY QLD 4343
Australia
alannahstead@example.com
Fitzhardinge,
Lucas
88 Dossiter Street
WATERLOO TAS 7109
Australia
lucas_fitz@example.com
Goldhar,
Lucinda
24 Settlement Road
DARGO VIC 3862
Australia
lucinda_g@example.com
Dearth,
Alexis
60 Sunnyside Road
TAYLORVILLE SA 5330
Australia
alexisdearth@example.com
Hansen,
Christopher
99 Weemala Avenue
GOOLOOGONG NSW 2805
Australia
chrishansen85@example.com
Kaczmarek,
Scott
94 Peninsula Drive
ILLAWONG NSW 2234
Australia
scott_kaczmarek@example.com
Pugliesi,
Brandon
83 McDowall Street
BALMORAL RIDGE QLD 4552
Australia
brandon_pugliesi@example.com
Bremer,
Rachel
80 Argyle Street
STRATFORD NSW 2422
Australia
rachel_bremer@example.com
Kerferd,
Kaitlyn
15 Souttar Terrace
KINGSLEY WA 6026
Australia
kaitlyn_kerferd@example.com

We can further condense the table by reducing the email link to an icon. The approach we take here is the use of a fontawesome icon within the display_name argument. The icon used is "envelope" and each icon produced serves as a clickable ‘mailto:’ link. By adjusting one of the cols_merge() calls, we can place the icon/link next to the name of the person.

peeps |>
  dplyr::filter(country == "AUS") |>
  dplyr::select(
    starts_with("name"),
    address, city, state_prov, postcode, country, email_addr
  ) |>
  dplyr::mutate(city = toupper(city)) |>
  gt(rowname_col = "name_family") |>
  tab_header(title = "Our Contacts in Australia") |>
  fmt_email(
    columns = email_addr,
    display_name = fontawesome::fa(
      name = "envelope",
      height = "0.75em",
      fill = "gray"
    )
  ) |>
  fmt_country(columns = country) |>
  cols_merge(
    columns = c(address, city, state_prov, postcode, country),
    pattern = "{1}<br>{2} {3} {4}<br>{5}"
  ) |>
  cols_merge(
    columns = c(name_family, name_given, email_addr),
    pattern = "{1}, {2} {3}"
  ) |>
  cols_width(everything() ~ px(200)) |>
  tab_style(
    style = cell_text(size = px(11)),
    locations = cells_body(columns = address)
  ) |>
  tab_options(column_labels.hidden = TRUE) |>
  opt_align_table_header(align = "left")
Our Contacts in Australia
Christison, Milla 34 McGregor Street
KINALUNG NSW 2880
Australia
Stead, Alannah 44 Mt Berryman Road
ROPELEY QLD 4343
Australia
Fitzhardinge, Lucas 88 Dossiter Street
WATERLOO TAS 7109
Australia
Goldhar, Lucinda 24 Settlement Road
DARGO VIC 3862
Australia
Dearth, Alexis 60 Sunnyside Road
TAYLORVILLE SA 5330
Australia
Hansen, Christopher 99 Weemala Avenue
GOOLOOGONG NSW 2805
Australia
Kaczmarek, Scott 94 Peninsula Drive
ILLAWONG NSW 2234
Australia
Pugliesi, Brandon 83 McDowall Street
BALMORAL RIDGE QLD 4552
Australia
Bremer, Rachel 80 Argyle Street
STRATFORD NSW 2422
Australia
Kerferd, Kaitlyn 15 Souttar Terrace
KINGSLEY WA 6026
Australia

Another option is to display the names of the email recipients instead of the email addresses, making the display names serve as ‘mailto:’ links. We can do this by using from_column() in the display_name argument. The display names in this case are the combined given and family names, handled earlier through a dplyr::mutate() call. With some space conserved, we take the opportunity here to add in phone information for each person.

peeps |>
  dplyr::filter(country == "AUS") |>
  dplyr::mutate(name = paste(name_given, name_family)) |>
  dplyr::mutate(city = toupper(city)) |>
  dplyr::mutate(phone_number = gsub("^\\(0|\\)", "", phone_number)) |>
  dplyr::select(
    name, address, city, state_prov, postcode, country,
    email_addr, phone_number, country_code
  ) |>
  gt(rowname_col = "email_addr") |>
  tab_header(title = "Our Contacts in Australia") |>
  tab_stubhead(label = "Name") |>
  fmt_email(
    columns = email_addr,
    display_name = from_column("name"),
    color = "gray25"
  ) |>
  cols_hide(columns = name) |>
  fmt_country(columns = country) |>
  cols_merge(
    columns = c(address, city, state_prov, postcode, country),
    pattern = "{1}<br>{2} {3} {4}<br>{5}"
  ) |>
  cols_merge(
    columns = c(phone_number, country_code),
    pattern = "+{2} {1}"
  ) |>
  cols_label(
    address = "Mailing Address",
    email_addr = "Email",
    phone_number = "Phone"
  ) |>
  cols_move_to_start(columns = phone_number) |>
  cols_width(everything() ~ px(170)) |>
  tab_style(
    style = cell_text(size = px(11)),
    locations = cells_body(columns = address)
  ) |>
  cols_align(align = "left") |>
  opt_align_table_header(align = "left")
Our Contacts in Australia
Name Phone Mailing Address
Milla Christison +61 2 4002 5734 34 McGregor Street
KINALUNG NSW 2880
Australia
Alannah Stead +61 7 4598 9718 44 Mt Berryman Road
ROPELEY QLD 4343
Australia
Lucas Fitzhardinge +61 3 6214 2586 88 Dossiter Street
WATERLOO TAS 7109
Australia
Lucinda Goldhar +61 8 8764 0723 24 Settlement Road
DARGO VIC 3862
Australia
Alexis Dearth +61 8 8781 7784 60 Sunnyside Road
TAYLORVILLE SA 5330
Australia
Christopher Hansen +61 2 4001 7042 99 Weemala Avenue
GOOLOOGONG NSW 2805
Australia
Scott Kaczmarek +61 2 4251 1932 94 Peninsula Drive
ILLAWONG NSW 2234
Australia
Brandon Pugliesi +61 7 5376 0506 83 McDowall Street
BALMORAL RIDGE QLD 4552
Australia
Rachel Bremer +61 2 4904 0795 80 Argyle Street
STRATFORD NSW 2422
Australia
Kaitlyn Kerferd +61 8 9463 8262 15 Souttar Terrace
KINGSLEY WA 6026
Australia