jthomasmock / gtExtras

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

gt_img_rows on group column #26

Closed martindut closed 2 years ago

martindut commented 2 years ago

Hi. Great package. I'm trying to get an image on a group column, but cannot seem to get in working. From the example in the package, I tried this: ` library(gt) library(gtExtras) teams <- "https://github.com/nflverse/nflfastR-data/raw/master/teams_colors_logos.rds" team_df <- readRDS(url(teams))

logo_table <- team_df %>% dplyr::select(team_wordmark, team_abbr, logo = team_logo_espn, team_name:team_conf) %>% head() %>% dplyr::group_by(team_wordmark) %>% gt() %>% gt_img_rows(columns = team_wordmark, height = 25) %>% gt_img_rows(columns = logo, img_source = "web", height = 30) %>% tab_options(data_row.padding = px(1)) ` but it just ends up with the url in the group column. I'm I doing something wrong. Thanks

jthomasmock commented 2 years ago

Howdy @martindut - gt only provides targeting of the cells_stub() with text_transform(), which I was using for the addition of images.

To accomplish row_name graphics, you could do the below:

team_df %>%
  dplyr::select(team_wordmark, team_abbr, logo = team_logo_espn, team_name:team_conf) %>%
  head() %>%
  gt(rowname_col = "team_wordmark") %>% 
  text_transform(
    locations = gt::cells_stub(rows = everything()),
    fn = function(x){
      web_image(x)
    }
  )

or if you really want them as the group:

top_team_df <- team_df %>%
  dplyr::select(team_wordmark, team_abbr, logo = team_logo_espn, team_name:team_conf) %>%
  head() 

top_team_df %>% 
  gt() %>% 
  gt::tab_row_group(
    label = gt::html(glue::glue("<img src = '{top_team_df[1,1]}' height = '35'>")),
    rows = 1:6
  )