igroffman commited on
Commit
4987c02
·
verified ·
1 Parent(s): 3c70393

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +37 -51
app.R CHANGED
@@ -201,9 +201,7 @@ process_dataset <- function(df) {
201
  if ("PitchUID" %in% names(df)) df <- df %>% distinct(PitchUID, .keep_all = TRUE)
202
 
203
  if ("Date" %in% names(df)) {
204
- df$Date <- suppressWarnings(as.Date(df$Date, format = "%m/%d/%Y"))
205
- if (all(is.na(df$Date))) df$Date <- suppressWarnings(as.Date(df$Date, format = "%m/%d/%y"))
206
- if (all(is.na(df$Date))) df$Date <- suppressWarnings(as.Date(df$Date))
207
  }
208
 
209
  if ("PlateLocSide" %in% names(df)) df$PlateLocSide <- as.numeric(df$PlateLocSide)
@@ -259,11 +257,8 @@ process_bp_dataset <- function(df) {
259
  df <- df %>% distinct()
260
  if ("PitchUID" %in% names(df)) df <- df %>% distinct(PitchUID, .keep_all = TRUE)
261
 
262
- # Date processing
263
  if ("Date" %in% names(df)) {
264
- df$Date <- suppressWarnings(as.Date(df$Date, format = "%m/%d/%y"))
265
- if (all(is.na(df$Date))) df$Date <- suppressWarnings(as.Date(df$Date, format = "%m/%d/%Y"))
266
- if (all(is.na(df$Date))) df$Date <- suppressWarnings(as.Date(df$Date))
267
  }
268
 
269
  # Convert numeric columns
@@ -813,26 +808,10 @@ calculate_leaderboards <- function(df, team_meta_df = team_meta) {
813
 
814
  game_date <- if ("Date" %in% names(df)) {
815
  raw_date <- unique(na.omit(df$Date))[1]
816
- parsed <- tryCatch({
817
- # Handle if already a Date object
818
- if (inherits(raw_date, "Date")) {
819
- raw_date
820
- } else {
821
- raw_date <- as.character(raw_date)
822
- if (grepl("^\\d{4}-\\d{1,2}-\\d{1,2}", raw_date)) {
823
- as.Date(raw_date, format = "%Y-%m-%d")
824
- } else if (grepl("^\\d{1,2}/\\d{1,2}/\\d{4}", raw_date)) {
825
- as.Date(raw_date, format = "%m/%d/%Y")
826
- } else if (grepl("^\\d{1,2}/\\d{1,2}/\\d{2}$", raw_date)) {
827
- as.Date(raw_date, format = "%m/%d/%y")
828
- } else {
829
- as.Date(raw_date)
830
- }
831
- }
832
- }, error = function(e) NA)
833
-
834
  if (is.na(parsed)) "N/A" else format(parsed, "%m/%d/%Y")
835
  } else "N/A"
 
836
  # Calculate final score from RunsScored
837
  teams <- unique(c(df$BatterTeam, df$PitcherTeam))
838
  teams <- teams[!is.na(teams)]
@@ -1801,10 +1780,10 @@ create_tableau_pitcher_pdf <- function(game_df, pitcher_name, output_file) {
1801
  return(output_file)
1802
  }
1803
 
1804
- # Metadata
1805
  game_date <- tryCatch({
1806
  d <- unique(pitcher_df$Date)[1]
1807
- if (inherits(d, "Date")) format(d, "%m/%d/%Y") else as.character(d)
 
1808
  }, error = function(e) "NA")
1809
 
1810
  batter_teams <- unique(pitcher_df$BatterTeam)
@@ -1877,15 +1856,10 @@ grid.text("Pitcher Post-Game Report", x = 0.02, y = 0.977, just = "left",
1877
 
1878
  info_y <- 0.935
1879
 
1880
- draw_info_row_colored_team(
1881
- game_date = game_date,
1882
- pitcher_name = pitcher_name,
1883
- away_team_abbr = away_team,
1884
- TMB = TMB,
1885
- x = 0.02, y = info_y,
1886
- base_cex = 0.8,
1887
- base_col = "black",
1888
- fontface = "bold"
1889
  )
1890
 
1891
  # ============================================================
@@ -2032,9 +2006,7 @@ catcher_process_dataset <- function(df) {
2032
  df <- df %>% distinct()
2033
  if ("PitchUID" %in% names(df)) df <- df %>% distinct(PitchUID, .keep_all = TRUE)
2034
  if ("Date" %in% names(df)) {
2035
- df$Date <- suppressWarnings(as.Date(df$Date, format = "%m/%d/%Y"))
2036
- if (all(is.na(df$Date))) df$Date <- suppressWarnings(as.Date(df$Date, format = "%m/%d/%y"))
2037
- if (all(is.na(df$Date))) df$Date <- suppressWarnings(as.Date(df$Date))
2038
  }
2039
  if ("PlateLocSide" %in% names(df)) df$PlateLocSide <- as.numeric(df$PlateLocSide)
2040
  if ("PlateLocHeight" %in% names(df)) df$PlateLocHeight <- as.numeric(df$PlateLocHeight)
@@ -2943,8 +2915,7 @@ umpire_process_data <- function(df) {
2943
  filter(PitchCall %in% c("StrikeCalled", "BallCalled", "BallinDirt"))
2944
 
2945
  if ("Date" %in% names(df)) {
2946
- df$Date <- suppressWarnings(as.Date(df$Date, format = "%m/%d/%Y"))
2947
- if (all(is.na(df$Date))) df$Date <- suppressWarnings(as.Date(df$Date, format = "%m/%d/%y"))
2948
  }
2949
 
2950
  if ("PlateLocSide" %in% names(df)) df$PlateLocSide <- as.numeric(df$PlateLocSide)
@@ -3235,18 +3206,33 @@ draw_header <- function() {
3235
  pushViewport(viewport(x = 0.72, y = 0.60, width = 0.47, height = 0.27)); print(plot_strike_opp, newpage = FALSE); popViewport()
3236
 
3237
  if (nrow(MissedCalls) > 0) {
3238
- page_chunks <- split(MissedCalls, ceiling(seq_len(nrow(MissedCalls)) / rows_per_page))
3239
- pushViewport(viewport(x = 0.5, y = 0.30, width = 0.92, height = 0.50))
3240
- grid.table(page_chunks[[1]], rows = NULL, theme = ttheme_green_small)
3241
- popViewport()
3242
- if (length(page_chunks) > 1) {
3243
- for (i in 2:length(page_chunks)) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3244
  grid.newpage()
3245
- pushViewport(viewport(x = 0.5, y = 0.5, width = 0.92, height = 0.92))
3246
- grid.table(page_chunks[[i]], rows = NULL, theme = ttheme_green_small)
 
 
3247
  popViewport()
3248
-
3249
-
3250
  }
3251
  }
3252
  }
 
201
  if ("PitchUID" %in% names(df)) df <- df %>% distinct(PitchUID, .keep_all = TRUE)
202
 
203
  if ("Date" %in% names(df)) {
204
+ df$Date <- parse_flexible_date(df$Date)
 
 
205
  }
206
 
207
  if ("PlateLocSide" %in% names(df)) df$PlateLocSide <- as.numeric(df$PlateLocSide)
 
257
  df <- df %>% distinct()
258
  if ("PitchUID" %in% names(df)) df <- df %>% distinct(PitchUID, .keep_all = TRUE)
259
 
 
260
  if ("Date" %in% names(df)) {
261
+ df$Date <- parse_flexible_date(df$Date)
 
 
262
  }
263
 
264
  # Convert numeric columns
 
808
 
809
  game_date <- if ("Date" %in% names(df)) {
810
  raw_date <- unique(na.omit(df$Date))[1]
811
+ parsed <- tryCatch(parse_flexible_date(raw_date), error = function(e) NA)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
812
  if (is.na(parsed)) "N/A" else format(parsed, "%m/%d/%Y")
813
  } else "N/A"
814
+
815
  # Calculate final score from RunsScored
816
  teams <- unique(c(df$BatterTeam, df$PitcherTeam))
817
  teams <- teams[!is.na(teams)]
 
1780
  return(output_file)
1781
  }
1782
 
 
1783
  game_date <- tryCatch({
1784
  d <- unique(pitcher_df$Date)[1]
1785
+ parsed <- parse_flexible_date(d)
1786
+ if (!is.na(parsed)) format(parsed, "%m/%d/%Y") else "NA"
1787
  }, error = function(e) "NA")
1788
 
1789
  batter_teams <- unique(pitcher_df$BatterTeam)
 
1856
 
1857
  info_y <- 0.935
1858
 
1859
+ grid.text(
1860
+ paste0(game_date, " | ", pitcher_name, " vs ", away_team),
1861
+ x = 0.02, y = info_y, just = "left",
1862
+ gp = gpar(cex = 0.8, fontface = "bold", col = "black")
 
 
 
 
 
1863
  )
1864
 
1865
  # ============================================================
 
2006
  df <- df %>% distinct()
2007
  if ("PitchUID" %in% names(df)) df <- df %>% distinct(PitchUID, .keep_all = TRUE)
2008
  if ("Date" %in% names(df)) {
2009
+ df$Date <- parse_flexible_date(df$Date)
 
 
2010
  }
2011
  if ("PlateLocSide" %in% names(df)) df$PlateLocSide <- as.numeric(df$PlateLocSide)
2012
  if ("PlateLocHeight" %in% names(df)) df$PlateLocHeight <- as.numeric(df$PlateLocHeight)
 
2915
  filter(PitchCall %in% c("StrikeCalled", "BallCalled", "BallinDirt"))
2916
 
2917
  if ("Date" %in% names(df)) {
2918
+ df$Date <- parse_flexible_date(df$Date)
 
2919
  }
2920
 
2921
  if ("PlateLocSide" %in% names(df)) df$PlateLocSide <- as.numeric(df$PlateLocSide)
 
3206
  pushViewport(viewport(x = 0.72, y = 0.60, width = 0.47, height = 0.27)); print(plot_strike_opp, newpage = FALSE); popViewport()
3207
 
3208
  if (nrow(MissedCalls) > 0) {
3209
+ # First page can fit 10 rows in the bottom half
3210
+ first_page_rows <- 10
3211
+ remaining_page_rows <- rows_per_page # 30 rows per full page
3212
+
3213
+ if (nrow(MissedCalls) <= first_page_rows) {
3214
+ # Fits on current page
3215
+ pushViewport(viewport(x = 0.5, y = 0.30, width = 0.92, height = 0.50))
3216
+ grid.table(MissedCalls, rows = NULL, theme = ttheme_green_small)
3217
+ popViewport()
3218
+ } else {
3219
+ # First batch on current page
3220
+ first_chunk <- MissedCalls[1:first_page_rows, , drop = FALSE]
3221
+ pushViewport(viewport(x = 0.5, y = 0.30, width = 0.92, height = 0.50))
3222
+ grid.table(first_chunk, rows = NULL, theme = ttheme_green_small)
3223
+ popViewport()
3224
+
3225
+ # Remaining rows paginated onto new pages
3226
+ remaining <- MissedCalls[(first_page_rows + 1):nrow(MissedCalls), , drop = FALSE]
3227
+ remaining_chunks <- split(remaining, ceiling(seq_len(nrow(remaining)) / remaining_page_rows))
3228
+
3229
+ for (i in seq_along(remaining_chunks)) {
3230
  grid.newpage()
3231
+ grid.text("Missed Calls (continued)", x = 0.5, y = 0.96,
3232
+ gp = gpar(fontsize = 12, fontface = "bold", col = "#006F71"))
3233
+ pushViewport(viewport(x = 0.5, y = 0.48, width = 0.92, height = 0.88))
3234
+ grid.table(remaining_chunks[[i]], rows = NULL, theme = ttheme_green_small)
3235
  popViewport()
 
 
3236
  }
3237
  }
3238
  }