jthomasmock / gtExtras

A Collection of Helper Functions for the gt Package.
https://jthomasmock.github.io/gtExtras/
Other
193 stars 26 forks source link

Initial gt_img_multi_rows() commit. #63

Closed ryantimpe closed 2 years ago

ryantimpe commented 2 years ago

Hi @jthomasmock

Following up on our Twitter exchange, I've added a version of the gt_img_rows() function that can take a nested column of image address and render them each within a cell.

Not sure how/if you want this to fit within the package, as some of the text/html parsing is a bit hacky and there might be an better way. For now, the function is nearly identical to gt_img_rows(), except it diverges at the text_transform function to first break the contents of the arrays inside the list into individuals divs.

Few notes:

library(gt)
teams <- "https://github.com/nflverse/nflfastR-data/raw/master/teams_colors_logos.rds"
team_df <- readRDS(url(teams))

conf_table <- team_df %>%
  dplyr::select(team_conf, team_division, logo = team_logo_espn) %>%
  dplyr::distinct() %>%
  tidyr::nest(data = logo) %>%
  dplyr::rename(team_logos = data) %>%
  dplyr::arrange(team_conf, team_division) %>%
  gt() %>%
  gt_img_multi_rows(columns = team_logos, height = 25)

conf_table
conf_table_missing <- team_df %>%
  dplyr::select(team_conf, team_division, logo = team_logo_espn) %>%
  dplyr::distinct() %>%
  dplyr::mutate(logo = dplyr::case_when(
    team_division == "AFC East" ~ "",
    team_division == "NFC South" & !stringr::str_detect(logo, "no.png") ~ "",
    TRUE ~ logo)) %>% 
  tidyr::nest(data = logo) %>%
  dplyr::rename(team_logos = data) %>%
  dplyr::arrange(team_conf, team_division) %>%
  gt() %>%
  gt_img_multi_rows(columns = team_logos, height = 25)

conf_table_missing 
jthomasmock commented 2 years ago

LGTM!

I'll revisit whether to combine this into gt_img_row() proper but I think that this being a stand-alone function is a great start, thanks for the PR!!!

jthomasmock commented 2 years ago

Checks are failing because of updated fontawesome and changed icons... will need to revisit separately, but going to merge. Thanks!