igroffman commited on
Commit
7444ef4
·
verified ·
1 Parent(s): aa61eb4

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +43 -45
app.R CHANGED
@@ -159,6 +159,10 @@ app_css <- "
159
  }
160
  "
161
 
 
 
 
 
162
  process_dataset <- function(df) {
163
  if ("Batter" %in% names(df)) {
164
  df <- df %>% mutate(Batter = stringr::str_replace(Batter, "^\\s*(\\w+)\\s*,\\s*(\\w+)\\s*$", "\\2 \\1"))
@@ -875,38 +879,36 @@ game_date <- if ("Date" %in% names(df)) {
875
  )
876
  }
877
  create_simple_header <- function(player_name, game_date, bio_data = NULL) {
878
- if (!is.null(bio_data) && nrow(bio_data) > 0) {
879
- player_bio <- bio_data %>% filter(Batter == player_name)
880
- if (nrow(player_bio) > 0 && "Headshot" %in% names(player_bio)) {
881
- headshot_url <- player_bio$Headshot[1]
882
- if (!is.na(headshot_url) && nzchar(headshot_url)) {
883
- tryCatch({
884
- if (requireNamespace("magick", quietly = TRUE)) {
885
- img <- magick::image_read(headshot_url)
886
- img_grob <- rasterGrob(as.raster(img), interpolate = TRUE)
887
- text_grob <- textGrob(
888
- paste(player_name, "-", format(game_date, "%m/%d/%y"), "- Hitter Report"),
889
- gp = gpar(fontface = "bold", cex = 1.6, col = "#006F71")
890
- )
891
- return(arrangeGrob(
892
- img_grob,
893
- textGrob(
894
- paste(player_name, "-", format(game_date, "%m/%d/%y"), "- Hitter Report"),
895
- gp = gpar(fontface = "bold", cex = 1.6, col = "#006F71"),
896
- x = 0.40, just = "centre"
897
- ),
898
- ncol = 2,
899
- widths = unit.c(unit(120, "pt"), unit(1, "npc") - unit(120, "pt"))
900
- ))
901
- }
902
- }, error = function(e) { })
903
- }
904
- }
905
- }
906
- grid::textGrob(
907
- paste(player_name, "-", format(game_date, "%m/%d/%y"), "- Hitter Report"),
908
- gp = grid::gpar(fontface = "bold", cex = 1.8, col = "#006F71")
909
- )
910
  }
911
 
912
  # Helper function to check if bat tracking data is available
@@ -1028,7 +1030,7 @@ create_postgame_pdf <- function(game_df, player_name, output_file, bio_data = NU
1028
  # Updated draw function with bat tracking support
1029
  draw_pitch_table <- function(df, y_top, row_height = 0.0135, cex = 0.58, include_bat_tracking = FALSE) {
1030
  if (include_bat_tracking) {
1031
- # Headers with bat tracking: #, Inn, Pitcher, Count, Pitch, Velo, IVB, HB, VAA, Event, EV, LA, Dist, BatSpd, VBA, HBA
1032
  headers <- c("#", "Inn", "Pitcher", "Count", "Pitch", "Velo", "IVB", "HB", "VAA", "Event", "EV", "LA", "Dist", "BatSpd", "AA", "HAA")
1033
  widths <- c(0.025, 0.03, 0.12, 0.04, 0.065, 0.04, 0.04, 0.04, 0.04, 0.065, 0.035, 0.035, 0.04, 0.045, 0.04, 0.04)
1034
  } else {
@@ -1107,24 +1109,24 @@ create_postgame_pdf <- function(game_df, player_name, output_file, bio_data = NU
1107
  grid.draw(create_simple_header(player_name, game_day, bio_data))
1108
  popViewport()
1109
 
1110
- grid.text("Game Line", x = 0.5, y = 0.89, gp = gpar(fontface = "bold", cex = 1.0))
1111
  headers <- c("PA","H","XBH","BB","K","Chase","Whiffs","IZ Whiffs","BIP","Avg EV","Avg LA","HH")
1112
  values <- c(game_stats$PA, game_stats$H, game_stats$XBH, game_stats$BB, game_stats$K,
1113
  game_stats$Chase, game_stats$Whiffs, game_stats$`IZ Whiffs`, game_stats$BIP,
1114
  game_stats$`Avg EV`, game_stats$`Avg LA`, game_stats$HH)
1115
- col_w <- 0.06; x0 <- 0.5 - (length(headers)*col_w)/2; yh <- 0.868; yv <- 0.846
1116
  for (i in seq_along(headers)) {
1117
  xi <- x0 + (i-1)*col_w
1118
- grid.rect(x = xi, y = yh, width = col_w*0.985, height = 0.018, just = c("left","top"),
1119
  gp = gpar(fill = "#006F71", col = "black", lwd = 0.5))
1120
- grid.text(headers[i], x = xi + col_w*0.49, y = yh - 0.009,
1121
- gp = gpar(col = "white", cex = 0.62, fontface = "bold"))
1122
- grid.rect(x = xi, y = yv, width = col_w*0.985, height = 0.018, just = c("left","top"),
1123
  gp = gpar(fill = "white", col = "black", lwd = 0.4))
1124
- grid.text(as.character(values[i]), x = xi + col_w*0.49, y = yv - 0.009, gp = gpar(cex = 0.62))
1125
  }
1126
 
1127
- pushViewport(viewport(x = 0.5, y = 0.805, width = 0.96, height = 0.44, just = c("center","top")))
1128
  print(at_bats_plot, newpage = FALSE)
1129
  popViewport()
1130
 
@@ -1153,10 +1155,6 @@ create_postgame_pdf <- function(game_df, player_name, output_file, bio_data = NU
1153
  cex = 0.56, include_bat_tracking = bat_tracking_available)
1154
  }
1155
  }
1156
-
1157
- # ============================================================
1158
- # PITCHER POST-GAME PDF (Tableau-style)
1159
- # Fixes:
1160
  # - Header stat tiles: bigger + more readable + moved slightly DOWN
1161
  # - Tables: bigger numbers
1162
  # - Pitch columns always ordered by pitch usage (Pitch Count desc)
 
159
  }
160
  "
161
 
162
+
163
+
164
+
165
+
166
  process_dataset <- function(df) {
167
  if ("Batter" %in% names(df)) {
168
  df <- df %>% mutate(Batter = stringr::str_replace(Batter, "^\\s*(\\w+)\\s*,\\s*(\\w+)\\s*$", "\\2 \\1"))
 
879
  )
880
  }
881
  create_simple_header <- function(player_name, game_date, bio_data = NULL) {
882
+ library(grid)
883
+ library(gridExtra)
884
+
885
+ # Load logos
886
+ left_logo <- tryCatch({
887
+ rasterGrob(as.raster(magick::image_resize(
888
+ magick::image_read("https://i.ibb.co/gLfTW4Fz/t-GPe-TPu.png"), "x120"
889
+ )), interpolate = TRUE)
890
+ }, error = function(e) nullGrob())
891
+
892
+ right_logo <- tryCatch({
893
+ rasterGrob(as.raster(magick::image_resize(
894
+ magick::image_read("https://i.imgur.com/zjTu3JS.png"), "x120"
895
+ )), interpolate = TRUE)
896
+ }, error = function(e) nullGrob())
897
+
898
+ # Title text
899
+ title_text <- paste(player_name, "-", format(game_date, "%m/%d/%y"), "- Hitter Report")
900
+
901
+ title_grob <- textGrob(
902
+ title_text,
903
+ gp = gpar(fontsize = 18, fontface = "bold", col = "#006F71")
904
+ )
905
+
906
+ # Layout with logos
907
+ arrangeGrob(
908
+ arrangeGrob(left_logo, title_grob, right_logo, ncol = 3,
909
+ widths = c(.15, .7, .15)),
910
+ ncol = 1
911
+ )
 
 
912
  }
913
 
914
  # Helper function to check if bat tracking data is available
 
1030
  # Updated draw function with bat tracking support
1031
  draw_pitch_table <- function(df, y_top, row_height = 0.0135, cex = 0.58, include_bat_tracking = FALSE) {
1032
  if (include_bat_tracking) {
1033
+ # Headers with bat tracking: #, Inn, Pitcher, Count, Pitch, Velo, IVB, HB, VAA, Event, EV, LA, Dist, BatSpd, AA, HAA
1034
  headers <- c("#", "Inn", "Pitcher", "Count", "Pitch", "Velo", "IVB", "HB", "VAA", "Event", "EV", "LA", "Dist", "BatSpd", "AA", "HAA")
1035
  widths <- c(0.025, 0.03, 0.12, 0.04, 0.065, 0.04, 0.04, 0.04, 0.04, 0.065, 0.035, 0.035, 0.04, 0.045, 0.04, 0.04)
1036
  } else {
 
1109
  grid.draw(create_simple_header(player_name, game_day, bio_data))
1110
  popViewport()
1111
 
1112
+ grid.text("Game Line", x = 0.5, y = 0.89, gp = gpar(fontface = "bold", cex = 1.2))
1113
  headers <- c("PA","H","XBH","BB","K","Chase","Whiffs","IZ Whiffs","BIP","Avg EV","Avg LA","HH")
1114
  values <- c(game_stats$PA, game_stats$H, game_stats$XBH, game_stats$BB, game_stats$K,
1115
  game_stats$Chase, game_stats$Whiffs, game_stats$`IZ Whiffs`, game_stats$BIP,
1116
  game_stats$`Avg EV`, game_stats$`Avg LA`, game_stats$HH)
1117
+ col_w <- 0.065; x0 <- 0.5 - (length(headers)*col_w)/2; yh <- 0.865; yv <- 0.840
1118
  for (i in seq_along(headers)) {
1119
  xi <- x0 + (i-1)*col_w
1120
+ grid.rect(x = xi, y = yh, width = col_w*0.985, height = 0.022, just = c("left","top"),
1121
  gp = gpar(fill = "#006F71", col = "black", lwd = 0.5))
1122
+ grid.text(headers[i], x = xi + col_w*0.49, y = yh - 0.011,
1123
+ gp = gpar(col = "white", cex = 0.72, fontface = "bold"))
1124
+ grid.rect(x = xi, y = yv, width = col_w*0.985, height = 0.022, just = c("left","top"),
1125
  gp = gpar(fill = "white", col = "black", lwd = 0.4))
1126
+ grid.text(as.character(values[i]), x = xi + col_w*0.49, y = yv - 0.011, gp = gpar(cex = 0.72))
1127
  }
1128
 
1129
+ pushViewport(viewport(x = 0.5, y = 0.795, width = 0.96, height = 0.44, just = c("center","top")))
1130
  print(at_bats_plot, newpage = FALSE)
1131
  popViewport()
1132
 
 
1155
  cex = 0.56, include_bat_tracking = bat_tracking_available)
1156
  }
1157
  }
 
 
 
 
1158
  # - Header stat tiles: bigger + more readable + moved slightly DOWN
1159
  # - Tables: bigger numbers
1160
  # - Pitch columns always ordered by pitch usage (Pitch Count desc)