igroffman commited on
Commit
a68f710
·
verified ·
1 Parent(s): f17ead6

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +49 -11
app.R CHANGED
@@ -1188,9 +1188,12 @@ create_bp_leaderboard_pdf <- function(lb, date_label, output_file) {
1188
  invisible(output_file)
1189
  }
1190
 
1191
- create_bp_pdf <- function(bp_data, batter_name, output_file) {
1192
- if (length(dev.list()) > 0) try(dev.off(), silent = TRUE)
1193
-
 
 
 
1194
  batter_df <- filter(bp_data, Batter == batter_name)
1195
 
1196
  # Calculate stats in the order: BBE, Avg EV, Avg LA, Max EV, 10-30%, HH%, Barrel%
@@ -1216,10 +1219,6 @@ create_bp_pdf <- function(bp_data, batter_name, output_file) {
1216
  contact_plot <- create_bp_contact_map(batter_name, bp_data)
1217
  side_plot <- create_bp_contact_side(batter_name, bp_data)
1218
 
1219
- # Create PDF
1220
- pdf(output_file, width = 11, height = 8.5)
1221
- on.exit(try(dev.off(), silent = TRUE), add = TRUE)
1222
-
1223
  grid::grid.newpage()
1224
 
1225
  # Title section
@@ -1229,7 +1228,7 @@ create_bp_pdf <- function(bp_data, batter_name, output_file) {
1229
  grid::popViewport()
1230
 
1231
  grid::pushViewport(grid::viewport(x = 0.5, y = 0.935, width = 1, height = 0.04, just = c("center", "top")))
1232
- grid::grid.text(batter_name,
1233
  gp = grid::gpar(fontface = "bold", cex = 1.6, col = "black"))
1234
  grid::popViewport()
1235
 
@@ -1296,6 +1295,34 @@ create_bp_pdf <- function(bp_data, batter_name, output_file) {
1296
  grid::grid.text("Coastal Carolina Baseball Analytics", x = 0.5, y = 0.018,
1297
  gp = grid::gpar(cex = 0.65, col = "grey55"))
1298
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1299
  invisible(output_file)
1300
  }
1301
 
@@ -7146,9 +7173,20 @@ server <- function(input, output, session) {
7146
  tmp <- tempdir(); pdfs <- character(0); total <- length(players)
7147
  for (i in seq_along(players)) {
7148
  ply <- players[i]; incProgress(1/total, detail=paste("Report for", ply))
7149
- out <- file.path(tmp, paste0(gsub(" ","_",ply), "_BP_Report.pdf"))
7150
- try(create_bp_pdf(df, ply, out), silent = TRUE)
7151
- if (file.exists(out)) pdfs <- c(pdfs, out)
 
 
 
 
 
 
 
 
 
 
 
7152
  }
7153
  if (!length(pdfs)) {
7154
  showNotification("Failed to generate BP reports", type="error", duration=5)
 
1188
  invisible(output_file)
1189
  }
1190
 
1191
+ bp_batter_sides <- function(bp_data, batter_name) {
1192
+ s <- bp_data %>% filter(Batter == batter_name) %>% pull(BatterSide)
1193
+ intersect(c("Left", "Right"), unique(na.omit(s)))
1194
+ }
1195
+
1196
+ draw_bp_report_page <- function(bp_data, batter_name, side_label = NULL) {
1197
  batter_df <- filter(bp_data, Batter == batter_name)
1198
 
1199
  # Calculate stats in the order: BBE, Avg EV, Avg LA, Max EV, 10-30%, HH%, Barrel%
 
1219
  contact_plot <- create_bp_contact_map(batter_name, bp_data)
1220
  side_plot <- create_bp_contact_side(batter_name, bp_data)
1221
 
 
 
 
 
1222
  grid::grid.newpage()
1223
 
1224
  # Title section
 
1228
  grid::popViewport()
1229
 
1230
  grid::pushViewport(grid::viewport(x = 0.5, y = 0.935, width = 1, height = 0.04, just = c("center", "top")))
1231
+ grid::grid.text(if (!is.null(side_label)) paste(batter_name, side_label) else batter_name,
1232
  gp = grid::gpar(fontface = "bold", cex = 1.6, col = "black"))
1233
  grid::popViewport()
1234
 
 
1295
  grid::grid.text("Coastal Carolina Baseball Analytics", x = 0.5, y = 0.018,
1296
  gp = grid::gpar(cex = 0.65, col = "grey55"))
1297
 
1298
+ invisible(NULL)
1299
+ }
1300
+
1301
+ create_bp_pdf <- function(bp_data, batter_name, output_file, side = NULL) {
1302
+ if (length(dev.list()) > 0) try(dev.off(), silent = TRUE)
1303
+
1304
+ side_tag <- c(Left = "(LHH)", Right = "(RHH)")
1305
+
1306
+ pdf(output_file, width = 11, height = 8.5)
1307
+ on.exit(try(dev.off(), silent = TRUE), add = TRUE)
1308
+
1309
+ if (!is.null(side)) {
1310
+ # Explicit single-side report
1311
+ draw_bp_report_page(filter(bp_data, BatterSide == side), batter_name,
1312
+ side_label = side_tag[[side]])
1313
+ } else {
1314
+ sides <- bp_batter_sides(bp_data, batter_name)
1315
+ if (length(sides) == 2) {
1316
+ # Switch hitter: one page per side (lefty first, then righty)
1317
+ for (sd in c("Left", "Right")) {
1318
+ draw_bp_report_page(filter(bp_data, BatterSide == sd), batter_name,
1319
+ side_label = side_tag[[sd]])
1320
+ }
1321
+ } else {
1322
+ draw_bp_report_page(bp_data, batter_name)
1323
+ }
1324
+ }
1325
+
1326
  invisible(output_file)
1327
  }
1328
 
 
7173
  tmp <- tempdir(); pdfs <- character(0); total <- length(players)
7174
  for (i in seq_along(players)) {
7175
  ply <- players[i]; incProgress(1/total, detail=paste("Report for", ply))
7176
+ sides <- bp_batter_sides(df, ply)
7177
+ if (length(sides) == 2) {
7178
+ # Switch hitter: separate righty and lefty reports
7179
+ for (sd in c("Left", "Right")) {
7180
+ tag <- if (sd == "Left") "LHH" else "RHH"
7181
+ out <- file.path(tmp, paste0(gsub(" ","_",ply), "_", tag, "_BP_Report.pdf"))
7182
+ try(create_bp_pdf(df, ply, out, side = sd), silent = TRUE)
7183
+ if (file.exists(out)) pdfs <- c(pdfs, out)
7184
+ }
7185
+ } else {
7186
+ out <- file.path(tmp, paste0(gsub(" ","_",ply), "_BP_Report.pdf"))
7187
+ try(create_bp_pdf(df, ply, out), silent = TRUE)
7188
+ if (file.exists(out)) pdfs <- c(pdfs, out)
7189
+ }
7190
  }
7191
  if (!length(pdfs)) {
7192
  showNotification("Failed to generate BP reports", type="error", duration=5)