jthomasmock / gtExtras

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

`gt_two_column_layout` error due to missing image file #134

Open OKcomputer626 opened 3 months ago

OKcomputer626 commented 3 months ago

Description

I encountered an error while using the gt_two_column_layout function from the gtExtras package. The error occurs when the function tries to access an image file that does not exist. Here is the error message:

In addition: Warning message:
In file(con, "rb") :
  cannot open file 'Bayesian Leagues Cup/images/logos/FC Cincinnati.png': No such file or directory

Reproducible example

library(tidyverse)
library(gt)
library(gtExtras)

df <- read_csv("Bayesian Leagues Cup/data/leagues cup predictions.csv")

df <- df %>%
  select(-n) %>%
  filter(Rank %in% c(1,2)) %>%
  pivot_wider(names_from = Rank, values_from = freq) %>%
  rename(Rank_1 = `1`,
         Rank_2 = `2`)

# Iterate over team pairs and calculate probabilities
for (i in length(df$Team)) {
  Team <- df$Team[i]

  # Append the results row by row
  results <- df %>%
    tibble(
      logo = paste0("Bayesian Leagues Cup/images/logos/", Team, ".png"),
    )
}

table1 <- results %>%
  filter(Region %in% c("East 1", "East 2", "East 3", "East 4", "East 5", "East 6", "East 7")) %>%
  arrange(Region, desc(Rank_1)) %>%
  gt(groupname_col = "Region") %>%
  tab_header(
    title = add_text_img(
      "2024 LEAGUES CUP PROBABILITES",
      url = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/61/Leagues_Cup_logo_white-on-black.svg/1024px-Leagues_Cup_logo_white-on-black.svg.png"
    ),
    subtitle = "Projections as of July 26, 2024 - Data provided by fbref via Opta"
  ) %>%
  cols_label(
    Team = "TEAM",
    Rank_1 = "1ST",
    Rank_2 = "2ND",
    prob_advance = "MAKE L32",
    xP = "Points",
    logo = ""
  ) %>%
  cols_move_to_start(
    columns = logo
  ) %>%
  cols_move(
    columns = c(Rank_1, Rank_2),
    after = Team
  ) %>%
  gt_img_rows(columns = logo, img_source = "local", height = 25) %>%
  tab_style(
    style = cell_borders(
      sides = "b",
      color = "black",
      weight = px(3)
    ),
    locations = list(cells_column_labels(), cells_stubhead())
    ) %>%
  cols_align(align = "center") %>%
  opt_table_font(
    font = list(
      google_font(name = "Roboto")
    )
  ) %>%
  tab_style(
    style = list(
      cell_text(weight = "bold", color = "#26282A")
    ),
    locations = cells_title(groups = "title")
  ) %>%
  tab_style(
    style = list(
      cell_text(color = "#26282A")
    ),
    locations = cells_body(
      columns = Team
    )
  ) %>%
  tab_style(
    style = cell_text(color = "#26282A"),
    locations = cells_row_groups()
  ) %>%
  tab_style(
    style = cell_text(color = "#26282A"),
    locations = cells_column_labels()
  ) %>%
  fmt_percent(
    columns = c(Rank_1, Rank_2, prob_advance),
    decimals = 0
  ) %>%
  fmt_number(
    columns = xP,
    decimals = 2
  ) %>%
  gt_color_rows(columns = c(prob_advance, Rank_1, Rank_2), palette = "RColorBrewer::Blues",
                domain = c(0,1)) %>%
  gt_color_rows(columns = xP, palette = "RColorBrewer::Blues",
                domain = c(min(results$xP), max(results$xP))
                ) %>%
  tab_options(
    heading.title.font.size = px(25),
    heading.align = "left",
    table.border.top.style = "hidden",
    column_labels.border.top.style = "hidden"
    ) %>%
  cols_width(
    Rank_1 ~ px(100),
    Rank_2 ~ px(100),
    prob_advance ~ px(100),
    xP ~ px(100)
  ) %>%
  tab_style(
    style = list(cell_borders(sides = "top", style = "hidden")),
    locations = cells_row_groups(groups = c("East 2", "East 3"))
  )

table2 <- results %>%
  filter(Region %in% c("West 1", "West 2", "West 3", "West 4", "West 5", "West 6", "West 7", "West 8")) %>%
  arrange(Region, desc(Rank_1)) %>%
  gt(groupname_col = "Region") %>%
  cols_label(
    Team = "TEAM",
    Rank_1 = "1ST",
    Rank_2 = "2ND",
    prob_advance = "MAKE L32",
    xP = "Points",
    logo = ""
  ) %>%
  cols_move_to_start(
    columns = logo
  ) %>%
  cols_move(
    columns = c(Rank_1, Rank_2),
    after = Team
  ) %>%
  gt_img_rows(columns = logo, img_source = "local", height = 25) %>%
  tab_style(
    style = cell_borders(
      sides = "b",
      color = "black",
      weight = px(3)
    ),
    locations = list(cells_column_labels(), cells_stubhead())
  ) %>%
  cols_align(align = "center") %>%
  opt_table_font(
    font = list(
      google_font(name = "Roboto")
    )
  ) %>%
  tab_style(
    style = list(
      cell_text(color = "#26282A")
    ),
    locations = cells_body(
      columns = Team
    )
  ) %>%
  tab_style(
    style = cell_text(color = "#26282A"),
    locations = cells_row_groups()
  ) %>%
  tab_style(
    style = cell_text(color = "#26282A"),
    locations = cells_column_labels()
  ) %>%
  fmt_percent(
    columns = c(Rank_1, Rank_2, prob_advance),
    decimals = 0
  ) %>%
  fmt_number(
    columns = xP,
    decimals = 2
  ) %>%
  gt_color_rows(columns = c(prob_advance, Rank_1, Rank_2), palette = "RColorBrewer::Blues",
                domain = c(0,1)) %>%
  gt_color_rows(columns = xP, palette = "RColorBrewer::Blues",
                domain = c(min(results$xP), max(results$xP))
  ) %>%
  tab_options(
    table.border.top.style = "hidden",
    column_labels.border.top.style = "hidden"
  ) %>%
  cols_width(
    Rank_1 ~ px(100),
    Rank_2 ~ px(100),
    prob_advance ~ px(100),
    xP ~ px(100)
  ) 

tables <- list(table1, table2)
gt_two_column_layout(tables) 

Expected result

The gt_two_column_layout function should handle missing image files gracefully, possibly by providing a warning and skipping the missing files or by using a placeholder image.

Session info

 sessionInfo()
R version 4.4.1 (2024-06-14 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22631)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8    LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: America/Los_Angeles
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] gtExtras_0.5.0.9005 gt_0.11.0           lubridate_1.9.3     forcats_1.0.0       stringr_1.5.1       dplyr_1.1.4         purrr_1.0.2         readr_2.1.5         tidyr_1.3.1        
[10] tibble_3.2.1        ggplot2_3.5.1       tidyverse_2.0.0    

loaded via a namespace (and not attached):
 [1] sass_0.4.9        utf8_1.2.4        generics_0.1.3    prismatic_1.1.2   xml2_1.3.6        stringi_1.8.4     hms_1.1.3         digest_0.6.36     magrittr_2.0.3    grid_4.4.1       
[11] timechange_0.3.0  fastmap_1.2.0     jsonlite_1.8.8    processx_3.8.4    chromote_0.2.0    rematch2_2.1.2    ps_1.7.7          promises_1.3.0    fansi_1.0.6       scales_1.3.0     
[21] cli_3.6.3         rlang_1.1.4       crayon_1.5.3      bit64_4.0.5       munsell_0.5.1     base64enc_0.1-3   withr_3.0.0       tools_4.4.1       parallel_4.4.1    tzdb_0.4.0       
[31] colorspace_2.1-0  paletteer_1.6.0   vctrs_0.6.5       R6_2.5.1          lifecycle_1.0.4   fs_1.6.4          bit_4.0.5         vroom_1.6.5       fontawesome_0.5.2 pkgconfig_2.0.3  
[41] later_1.3.2       pillar_1.9.0      gtable_0.3.5      Rcpp_1.0.12       glue_1.7.0        xfun_0.45         tidyselect_1.2.1  rstudioapi_0.16.0 knitr_1.48        farver_2.1.2     
[51] websocket_1.4.2   htmltools_0.5.8.1 webshot2_0.1.1    compiler_4.4.1