igroffman commited on
Commit
e8bbde3
·
verified ·
1 Parent(s): e0b4794

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +88 -13
app.R CHANGED
@@ -2045,28 +2045,103 @@ catcher_create_throwing_plot <- function(catcher_data, catcher_name) {
2045
  }
2046
 
2047
  catcher_create_simple_header <- function(catcher_name, game_date, bio_data = NULL) {
 
 
 
 
 
 
 
 
 
 
 
 
2048
  if (!is.null(bio_data) && nrow(bio_data) > 0) {
 
2049
  catcher_bio <- bio_data %>% filter(Catcher == catcher_name)
 
2050
  if (nrow(catcher_bio) > 0 && "Headshot" %in% names(catcher_bio)) {
 
2051
  headshot_url <- catcher_bio$Headshot[1]
 
2052
  if (!is.na(headshot_url) && nzchar(headshot_url)) {
2053
- tryCatch({
2054
- if (requireNamespace("magick", quietly = TRUE)) {
2055
- img <- magick::image_read(headshot_url)
2056
- img_grob <- rasterGrob(as.raster(img), interpolate = TRUE)
2057
- text_grob <- textGrob(paste(catcher_name, "- Catcher Report"),
2058
- gp = gpar(fontface = "bold", cex = 1.6, col = "#006F71"))
2059
- return(arrangeGrob(img_grob, text_grob, ncol = 2,
2060
- widths = unit.c(unit(100, "pt"), unit(1, "null"))))
2061
- }
2062
- }, error = function(e) { })
 
 
 
 
 
 
 
 
 
 
 
 
2063
  }
2064
  }
2065
  }
2066
- grid::textGrob(paste(catcher_name, "- Catcher Report"),
2067
- gp = grid::gpar(fontface = "bold", cex = 1.8, col = "#006F71"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2068
  }
2069
 
 
2070
  catcher_create_catcher_pdf <- function(game_df, catcher_name, output_file, bio_data = NULL) {
2071
  if (length(dev.list()) > 0) { try(dev.off(), silent = TRUE) }
2072
  catcher_df <- dplyr::filter(game_df, Catcher == catcher_name)
@@ -2080,7 +2155,7 @@ catcher_create_catcher_pdf <- function(game_df, catcher_name, output_file, bio_d
2080
  .groups = "drop")
2081
 
2082
  opp_receiving_stats <- catcher_df %>%
2083
- filter(PitcherTeam != "COA_CHA") %>%
2084
  summarise(`Opp Strikes Stolen` = sum(StolenStrike, na.rm = TRUE),
2085
  `Opp Strikes Lost` = sum(StrikeLost, na.rm = TRUE),
2086
  `Opp Game +/-` = sum(StolenStrike, na.rm = TRUE) - sum(StrikeLost, na.rm = TRUE),
 
2045
  }
2046
 
2047
  catcher_create_simple_header <- function(catcher_name, game_date, bio_data = NULL) {
2048
+
2049
+ suppressPackageStartupMessages({
2050
+ library(grid)
2051
+ library(gridExtra)
2052
+ library(magick)
2053
+ library(dplyr)
2054
+ })
2055
+
2056
+ title_text <- paste(catcher_name, "- Catcher Report")
2057
+ subhead_text <- if (!is.na(game_date)) paste("Game Date:", game_date) else NULL
2058
+
2059
+ ## ---------------- HEADSHOT HEADER (IF AVAILABLE) ----------------
2060
  if (!is.null(bio_data) && nrow(bio_data) > 0) {
2061
+
2062
  catcher_bio <- bio_data %>% filter(Catcher == catcher_name)
2063
+
2064
  if (nrow(catcher_bio) > 0 && "Headshot" %in% names(catcher_bio)) {
2065
+
2066
  headshot_url <- catcher_bio$Headshot[1]
2067
+
2068
  if (!is.na(headshot_url) && nzchar(headshot_url)) {
2069
+
2070
+ img <- try(magick::image_read(headshot_url), silent = TRUE)
2071
+
2072
+ if (!inherits(img, "try-error")) {
2073
+
2074
+ img_grob <- rasterGrob(as.raster(img), interpolate = TRUE)
2075
+
2076
+ text_grob <- textGrob(
2077
+ title_text,
2078
+ gp = gpar(fontface = "bold", fontsize = 18, col = "#006F71"),
2079
+ just = "left"
2080
+ )
2081
+
2082
+ return(
2083
+ arrangeGrob(
2084
+ img_grob,
2085
+ text_grob,
2086
+ ncol = 2,
2087
+ widths = unit.c(unit(100, "pt"), unit(1, "null"))
2088
+ )
2089
+ )
2090
+ }
2091
  }
2092
  }
2093
  }
2094
+
2095
+ ## ---------------- FALLBACK HEADER (NO HEADSHOT) ----------------
2096
+ grid.newpage()
2097
+
2098
+ draw_logo_url <- function(url, x, just) {
2099
+ img <- try(magick::image_read(url), silent = TRUE)
2100
+ if (inherits(img, "try-error")) return(NULL)
2101
+
2102
+ img <- magick::image_resize(img, "x130")
2103
+
2104
+ grid.draw(
2105
+ rasterGrob(
2106
+ as.raster(img),
2107
+ vp = viewport(
2108
+ x = x,
2109
+ y = 0.96,
2110
+ width = 0.13,
2111
+ height = 0.08,
2112
+ just = c(just, "center")
2113
+ ),
2114
+ interpolate = TRUE
2115
+ )
2116
+ )
2117
+ }
2118
+
2119
+ ## --- LOGOS ---
2120
+ left_logo_url <- "https://i.ibb.co/gLfTW4Fz/t-GPe-TPu.png"
2121
+ right_logo_url <- "https://i.imgur.com/zjTu3JS.png"
2122
+
2123
+ draw_logo_url(left_logo_url, 0.05, "left")
2124
+ draw_logo_url(right_logo_url, 0.95, "right")
2125
+
2126
+ ## --- TEXT ---
2127
+ grid.text(
2128
+ title_text,
2129
+ y = 0.975,
2130
+ gp = gpar(fontsize = 18, fontface = "bold", col = "#006F71")
2131
+ )
2132
+
2133
+ if (!is.null(subhead_text)) {
2134
+ grid.text(
2135
+ subhead_text,
2136
+ y = 0.945,
2137
+ gp = gpar(fontsize = 11)
2138
+ )
2139
+ }
2140
+
2141
+ invisible(NULL)
2142
  }
2143
 
2144
+
2145
  catcher_create_catcher_pdf <- function(game_df, catcher_name, output_file, bio_data = NULL) {
2146
  if (length(dev.list()) > 0) { try(dev.off(), silent = TRUE) }
2147
  catcher_df <- dplyr::filter(game_df, Catcher == catcher_name)
 
2155
  .groups = "drop")
2156
 
2157
  opp_receiving_stats <- catcher_df %>%
2158
+ filter(BatterTeam = "COA_CHA") %>%
2159
  summarise(`Opp Strikes Stolen` = sum(StolenStrike, na.rm = TRUE),
2160
  `Opp Strikes Lost` = sum(StrikeLost, na.rm = TRUE),
2161
  `Opp Game +/-` = sum(StolenStrike, na.rm = TRUE) - sum(StrikeLost, na.rm = TRUE),