igroffman commited on
Commit
0d8dc4b
·
verified ·
1 Parent(s): 3949eaf

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +66 -68
app.R CHANGED
@@ -281,74 +281,72 @@ server <- function(input, output, session){
281
  mutate(row_key = row_number())
282
  })
283
 
284
- # ---- Plotly Movement plot ----
285
- output$movement_plot <- renderPlotly({
286
- d <- pitcher_df()
287
- validate(need(nrow(d) > 0, "No data for selected pitcher"))
288
- pal <- pitch_colors
289
- cols <- pal[d$TaggedPitchType]; cols[is.na(cols)] <- "#D3D3D3"
290
-
291
- plot_ly(
292
- data = d, source="mv", type="scatter", mode="markers",
293
- x = ~HorzBreak, y = ~InducedVertBreak,
294
- text = ~paste0(
295
- "<b>", TaggedPitchType, "</b>",
296
- "<br>Velo: ", round(RelSpeed,1), " mph",
297
- "<br>HB: ", round(HorzBreak,1), " in",
298
- "<br>IVB: ", round(InducedVertBreak,1), " in",
299
- if ("SpinRate" %in% names(d)) paste0("<br>Spin: ", round(SpinRate), " rpm") else ""
300
- ),
301
- hoverinfo = "text",
302
- key = ~row_key,
303
- marker = list(size = 10, color = cols)
304
- ) %>%
305
- layout(
306
- title = paste("Pitch Movement Chart -", input$pitcher_select),
307
- xaxis = list(title="Horizontal Break (in)", range=c(-25,25), zeroline=TRUE),
308
- yaxis = list(title="Induced Vertical Break (in)", range=c(-25,25), zeroline=TRUE),
309
- dragmode = if (input$selection_mode == "drag") "select" else "zoom"
310
- ) %>%
311
- config(displaylogo = FALSE)
312
- })
313
-
314
- # ---- Plotly Location plot (under table) ----
315
- output$location_plot <- renderPlotly({
316
- d <- pitcher_df()
317
- validate(need(nrow(d) > 0, "No data for selected pitcher"))
318
- pal <- pitch_colors
319
- cols <- pal[d$TaggedPitchType]; cols[is.na(cols)] <- "#D3D3D3"
320
-
321
- p <- plot_ly(
322
- data = d, source="loc", type="scatter", mode="markers",
323
- x = ~PlateLocSide, y = ~PlateLocHeight,
324
- text = ~paste0(
325
- "<b>", TaggedPitchType, "</b>",
326
- "<br>Velo: ", round(RelSpeed,1), " mph",
327
- "<br>X: ", round(PlateLocSide,2),
328
- "<br>Z: ", round(PlateLocHeight,2)
329
- ),
330
- hoverinfo = "text",
331
- key = ~row_key,
332
- marker = list(size = 9, color = cols)
333
- ) %>%
334
- layout(
335
- title = "Pitch Location (Editable)",
336
- xaxis = list(title="Plate X (ft)", range=c(-2,2), zeroline=TRUE),
337
- yaxis = list(title="Plate Z (ft)", range=c(0,4.5), zeroline=TRUE),
338
- dragmode = if (input$selection_mode == "drag") "select" else "zoom",
339
- shapes = list(
340
- # strike zone rectangle
341
- list(type="rect", x0=-0.8303, x1=0.8303, y0=1.6, y1=3.5,
342
- line=list(color="black", width=1), fillcolor="rgba(0,0,0,0)"),
343
- # home plate path
344
- list(type="path",
345
- path = paste0("M -0.708 0.15 L 0.708 0.15 L 0.708 0.3 L 0 0.5 L -0.708 0.3 Z"),
346
- line=list(color="black", width=0.8))
347
- )
348
- ) %>% config(displaylogo = FALSE)
349
-
350
- p
351
- })
352
 
353
  # ---- Click handlers (single mode) for both plots ----
354
  show_pitch_modal <- function(hit_row){
 
281
  mutate(row_key = row_number())
282
  })
283
 
284
+ output$movement_plot <- renderPlotly({
285
+ d <- pitcher_df()
286
+ validate(need(nrow(d) > 0, "No data for selected pitcher"))
287
+
288
+ plot_ly(
289
+ data = d, source = "mv", type = "scatter", mode = "markers",
290
+ x = ~HorzBreak, y = ~InducedVertBreak,
291
+ text = ~paste0(
292
+ "<b>", TaggedPitchType, "</b>",
293
+ "<br>Velo: ", round(RelSpeed,1), " mph",
294
+ "<br>IVB: ", round(InducedVertBreak,1), " in",
295
+ "<br>HB: ", round(HorzBreak,1), " in",
296
+ if ("SpinRate" %in% names(d)) paste0("<br>Spin: ", round(SpinRate), " rpm") else ""
297
+ ),
298
+ hoverinfo = "text",
299
+ key = ~row_key,
300
+ color = ~factor(TaggedPitchType), # <-- data-driven
301
+ colors = pitch_colors, # <-- your palette
302
+ marker = list(size = 10)
303
+ ) |>
304
+ layout(
305
+ title = paste("Pitch Movement Chart -", input$pitcher_select),
306
+ xaxis = list(title="Horizontal Break (in)", range=c(-25,25), zeroline=TRUE),
307
+ yaxis = list(title="Induced Vertical Break (in)", range=c(-25,25), zeroline=TRUE),
308
+ dragmode = if (input$selection_mode == "drag") "select" else "zoom"
309
+ ) |>
310
+ config(displaylogo = FALSE)
311
+ })
312
+
313
+ output$location_plot <- renderPlotly({
314
+ d <- pitcher_df()
315
+ validate(need(nrow(d) > 0, "No data for selected pitcher"))
316
+
317
+ plot_ly(
318
+ data = d, source = "loc", type = "scatter", mode = "markers",
319
+ x = ~PlateLocSide, y = ~PlateLocHeight,
320
+ text = ~paste0(
321
+ "<b>", TaggedPitchType, "</b>",
322
+ "<br>Velo: ", round(RelSpeed,1), " mph",
323
+ "<br>X: ", round(PlateLocSide,2),
324
+ "<br>Z: ", round(PlateLocHeight,2),
325
+ "<br>IVB: ", round(InducedVertBreak,1), " in",
326
+ "<br>HB: ", round(HorzBreak,1), " in",
327
+ if ("SpinRate" %in% names(d)) paste0("<br>Spin: ", round(SpinRate), " rpm") else ""
328
+ ),
329
+ hoverinfo = "text",
330
+ key = ~row_key,
331
+ color = ~factor(TaggedPitchType), # <-- data-driven
332
+ colors = pitch_colors, # <-- your palette
333
+ marker = list(size = 9)
334
+ ) |>
335
+ layout(
336
+ title = "Pitch Location (Editable)",
337
+ xaxis = list(title="Plate X (ft)", range=c(-2,2), zeroline=TRUE),
338
+ yaxis = list(title="Plate Z (ft)", range=c(0,4.5), zeroline=TRUE),
339
+ dragmode = if (input$selection_mode == "drag") "select" else "zoom",
340
+ shapes = list(
341
+ list(type="rect", x0=-0.8303, x1=0.8303, y0=1.6, y1=3.5,
342
+ line=list(color="black", width=1), fillcolor="rgba(0,0,0,0)"),
343
+ list(type="path",
344
+ path="M -0.708 0.15 L 0.708 0.15 L 0.708 0.3 L 0 0.5 L -0.708 0.3 Z",
345
+ line=list(color="black", width=0.8))
346
+ )
347
+ ) |>
348
+ config(displaylogo = FALSE)
349
+ })
 
 
350
 
351
  # ---- Click handlers (single mode) for both plots ----
352
  show_pitch_modal <- function(hit_row){