File size: 16,041 Bytes
c83b73b
0a61c43
 
c83b73b
0a61c43
efc6028
0a61c43
c83b73b
0a61c43
 
c83b73b
0a61c43
 
 
 
 
703939d
 
 
5c6fee0
2e8630c
 
ff0a150
 
 
 
 
3322f58
0a61c43
 
 
 
 
3322f58
f4289ee
60b8531
 
0a61c43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60b8531
 
 
0a61c43
3322f58
0a61c43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3322f58
0a61c43
 
 
 
 
 
 
 
 
 
 
 
703939d
0a61c43
 
 
 
 
 
 
3322f58
0a61c43
 
 
 
 
 
 
 
 
60b8531
 
 
 
 
 
 
0a61c43
 
 
 
 
 
 
703939d
 
 
 
0a61c43
 
 
8b943dc
 
0a61c43
3322f58
0a61c43
 
 
 
 
 
 
3322f58
0a61c43
 
 
 
 
 
 
3322f58
0a61c43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3322f58
180871b
0a61c43
 
 
 
 
 
3322f58
 
d1a2a92
 
 
3322f58
 
d1a2a92
 
 
 
 
 
3322f58
 
 
703939d
3322f58
703939d
3322f58
 
 
703939d
 
3322f58
 
 
 
703939d
 
3322f58
 
703939d
0a61c43
 
 
c83b73b
3322f58
 
0a61c43
 
 
 
 
 
 
 
 
 
 
 
703939d
0a61c43
 
 
 
3322f58
703939d
ee73302
 
3322f58
 
 
 
ee73302
703939d
ee73302
 
 
 
 
 
 
 
703939d
ee73302
 
 
 
 
 
 
 
703939d
ee73302
703939d
ee73302
3322f58
 
 
 
ee73302
703939d
0a61c43
 
3322f58
703939d
3322f58
0a61c43
60b8531
 
 
 
 
 
 
 
 
0a61c43
 
 
 
 
 
 
60b8531
0a61c43
 
 
 
 
 
 
 
 
 
60b8531
0a61c43
 
 
 
703939d
0a61c43
 
 
 
 
60b8531
0a61c43
 
 
 
3322f58
703939d
 
60b8531
703939d
 
 
 
33e14e0
9dabf8b
60b8531
9dabf8b
 
 
 
 
 
 
 
 
 
 
 
 
 
703939d
 
 
 
 
 
 
 
 
 
3322f58
703939d
 
 
 
 
 
 
3322f58
703939d
 
 
 
3322f58
60b8531
703939d
 
 
 
 
 
 
 
 
 
 
c83b73b
60b8531
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
library(shiny)
library(plotly)
library(gridlayout)
library(bslib)
library(DT)
library(rsconnect)
library(baseballr)
library(dplyr)
library(tidyverse)
library(rvest)
library(ggplot2)
library(janitor)
library(ggthemes)
library(ggpubr)
library(jsonlite)
library(utils)
library(grid)
library(gridExtra)
library(png)
pdf(file = NULL)
Sys.setenv(TZ='EST')

is_barrel <- function(df) {
  df$barrel <- with(df, ifelse(hit_angle <= 50 & hit_speed >= 97 & hit_speed * 1.5 - 
                                 hit_angle >= 117 & hit_speed + hit_angle >= 123, 1, 0))
  return(df)
}

VAA <- function(milbtotal){
  milbtotal <- milbtotal |>
    mutate(VAA = -atan((vz0+(az*(-sqrt((vy0*vy0)-(2*ay*(y0-(17/12))))-vy0)/
                               ay))/(-sqrt((vy0*vy0)-(2*ay*(y0-(17/12))))))*(180/pi))
} 

pitcher_summary <- function(game_pk,date){
  gdate <- as.Date.character(date)
  gdate <- as.Date(gdate)
  tmilb <- mlb_pbp(game_pk)
  tmilb <- tmilb |>
    filter(type == "pitch")
  tmilb <- tmilb |>
    select(matchup.batter.fullName,matchup.batter.id,matchup.pitcher.fullName,
           matchup.pitcher.id,result.event,details.description,details.type.description,
           result.description,pitchData.startSpeed,
           pitchData.plateTime,pitchData.zone,pitchData.breaks.spinRate,pitchData.extension,
           pitchData.coordinates.pX,pitchData.coordinates.pZ,pitchData.coordinates.x0,
           pitchData.coordinates.y0,pitchData.coordinates.z0,pitchData.coordinates.aX,
           pitchData.coordinates.aY,pitchData.coordinates.aZ,pitchData.coordinates.vX0,
           pitchData.coordinates.vZ0,pitchData.coordinates.vY0,pitchData.coordinates.pfxX,
           pitchData.coordinates.pfxZ,pitchData.breaks.breakVerticalInduced,pitchData.breaks.breakHorizontal,
           hitData.launchSpeed,hitData.launchAngle,hitData.totalDistance,details.isInPlay,last.pitch.of.ab)
  colnames(tmilb) <- c("Batter Name","Batter ID","Pitcher Name","Pitcher ID",
                       "result","description","pitch_name",
                       "des","start_speed","plateTime","zone","spin_rate",
                       "extension","px","pz","x0","y0","z0","ax","ay","az",
                       "vx0","vz0","vy0","pfxX","pfxZ","IVB","HB","hit_speed",
                       "hit_angle","hit_distance","inPlay","lastPitch")
  tmilb <- is_barrel(tmilb)
  tmilb <- tmilb |>
    mutate(is_strike_swinging = ifelse(description == "Swinging Strike" |
                                         description == "Foul Tip",TRUE,FALSE))
  tmilb <- tmilb |>
    mutate(date = gdate)
  return(tmilb)
}

break_plot <- function(game){
  ggplot()+
    geom_point(game,mapping = 
                 aes(x=HB, y = IVB,color = pitch_name),size = 4) +
    geom_vline(xintercept = 0,  color = "lightblue",linewidth = 1,linetype = 4)+
    geom_hline(yintercept = 0,  color = "lightblue",linewidth = 1,linetype = 4)+
    xlab("Horizontal Break from Pitcher's Perspective (in) ")+
    ylab("Induced Verical Break (in)") +
    xlim(-25,25) +
    ylim(-25,25) +
    ggthemes::theme_igray()+
    theme(panel.grid.minor = element_line(color = "gray",
                                          size = 0.25,
                                          linetype = 1))+
    guides(color = guide_legend(title = "Pitch Type"))
}

pitch_plot <- function(game){
  ggplot()+
    geom_point(game,mapping = 
                 aes(x=px, y = pz,color = pitch_name),size = 3.5) +
    geom_segment(aes(x=-0.71,xend = 0.71, y = 1.5,yend = 1.5))+
    geom_segment(aes(x=-0.71,xend = 0.71, y = 3.6,yend = 3.6))+
    geom_segment(aes(x= 0.71,xend = 0.71, y = 1.5,yend = 3.6))+
    geom_segment(aes(x=-0.71,xend = -0.71, y = 1.5,yend =3.6))+
    xlab(" ")+
    ylab(" ") +
    xlim(-3,3)+
    ylim(0.2,4)+
    coord_fixed(ratio = 1) +
    ggthemes::theme_few()+
    guides(color = guide_legend(title = "Pitch Type"))+
    theme(axis.text.x=element_blank(), 
          axis.ticks.x=element_blank(), 
          axis.text.y=element_blank(), 
          axis.ticks.y=element_blank())
}

summary_table <- function(game){
  rows <- nrow(game)
  sumtable <- game |>
    mutate(VAA = -atan((vz0+(az*(-sqrt((vy0*vy0)-(2*ay*(y0-(17/12))))-vy0)/
                               ay))/(-sqrt((vy0*vy0)-(2*ay*(y0-(17/12))))))*(180/pi)) |>
    mutate(team_fielding_id = ifelse(description == "Called Strike" |
                                       description == "Swinging Strike" |
                                       description == "Swinging Strike (Blocked)",1,0))|>
    mutate(swing = ifelse(description == "Foul" |
                            description == "Foul Pitchout" |
                            description == "In play, no out" |
                            description == "In play, out(s)" | 
                            description == "In play, run(s)" | 
                            description == "Swinging Strike" | 
                            description == "swinging Strike (Blocked)" |
                            description == "Foul Tip",1,0)) |>
    mutate(is_strike_swinging = ifelse(is_strike_swinging == TRUE,1,0)) |>
    mutate(Pitch = pitch_name) |>
    group_by(Pitch)|>
    summarize(Pitches = n(),
              'Pitch%' = round(sum(Pitches)/sum(rows) * 100,digits = 1),
              'Avg. Velo' = round(sum(start_speed)/Pitches,digits = 1),
              'Spin Rate' = round(sum(spin_rate)/ Pitches,digits = 1),
              'Extension' = round(mean(extension,na.rm = TRUE),digits = 1),
              'IVB' = round(mean(IVB,na.rm=TRUE),digits = 1),
              'HB' = round(mean(HB,na.rm = TRUE),digits = 1),
              'VAA' = round(mean(VAA,na.rm= TRUE),digits = 1),
              'CSW%' = round(sum(team_fielding_id)/
                               sum(!is.na(team_fielding_id))*100,digits = 1),
              'Whiff%' = round(sum(is_strike_swinging)/
                                 sum(swing)*100,digits = 1))|>
  arrange(-Pitches)
}

mlbid <- mlb_schedule(season = 2024, level_ids = "1")
mlbteamH <- mlbid |>
  select(teams_home_team_name)
mlbteamH <- distinct(mlbteamH)
mlbteamA <- mlbid |>
  select(teams_away_team_name)
mlbteamA <- distinct(mlbteamA)

aaaid <- mlb_schedule(season = 2024, level_ids = "11")
aaateamH <- aaaid |>
  select(teams_home_team_name)
aaateamH <- distinct(aaateamH)
aaateamA <- aaaid |>
  select(teams_away_team_name)
aaateamA <- distinct(aaateamA)

fslid <- mlb_schedule(season = 2024, level_ids = "14")
fslid <- fslid |>
  filter(teams_home_team_name == "Daytona Tortugas" |
           teams_home_team_name == "Jupiter Hammerheads" |
           teams_home_team_name == "Palm Beach Cardinals" |
           teams_home_team_name == "St. Lucie Mets" |
           teams_home_team_name == "Bradenton Marauders" |
           teams_home_team_name == "Clearwater Threshers" |
           teams_home_team_name == "Dunedin Blue Jays" |
           teams_home_team_name == "Fort Myers Mighty Mussels" |
           teams_home_team_name == "Lakeland Flying Tigers" |
           teams_home_team_name == "Tampa Tarpons")
fslteamH <- fslid |>
  select(teams_home_team_name)
fslteamH <- distinct(fslteamH)
fslteamA <- fslid |>
  select(teams_away_team_name)
fslteamA <- distinct(fslteamA)

sbid <- mlb_schedule(season = 2024, level_ids = "22")
sbteamH <- sbid |>
  select(teams_home_team_name)
sbteamH <- distinct(sbteamH)
sbteamA <- sbid |>
  select(teams_away_team_name)
sbteamA <- distinct(sbteamA)

ui <- fluidPage(
  tags$head(
    tags$style(HTML("
      @media (max-width: 768px) {
        .sidebar { width: 100%; float: none; }
        .main-content { margin-left: 0; }
        .selectize-input { font-size: 14px; }
        .form-group { margin-bottom: 10px; }
        .action-button { width: 100%; }
      }
    "))
  ),
  titlePanel("2024 MLB/AAA/FSL Summary Cards"),
  sidebarLayout(
    sidebarPanel(
      width = 2,
      dateInput("date", "Date:"),
      selectizeInput("level", "Level:", c("MLB", "AAA", "FSL", "College (Statcast Parks Only)")),
      selectizeInput("homeT", "Home Team:", mlbteamH[,1]),
      selectizeInput("awayT", "Away Team:", mlbteamA[,1]),
      selectizeInput("gamenum", "Game Number (For Doubleheaders):", c("1", "2")),
      actionButton("update", "Find Pitcher", icon("magnifying-glass"),
                   style = "color: #FFFFFF; background-color: #0077B6; width: 100%;"),
      selectizeInput("pitcher", "Pitcher Name:", c(" ")),
      selectizeInput("league", "MLB or MiLB Photo", c("MLB", "MiLB")),
      textInput("title", "Card Title"),
      actionButton("update1", "Make Card", icon("plus"),
                   style = "color: #FFFFFF; background-color: #0077B6; width: 100%;"),
      downloadButton("downloadPlot", "Download Card")
    ),
    mainPanel(
      plotOutput("combinedPlot", height = "900px",width = "1350px")
    )
  )
)

server <- function(input, output, session) {
  observeEvent(input$level, {
    if(input$level == "AAA"){
      updateSelectizeInput(session = getDefaultReactiveDomain(),"homeT","Home Team:",choices =  aaateamH[,1])
      updateSelectizeInput(session = getDefaultReactiveDomain(),"awayT","Away Team:",choices =  aaateamA[,1])
    }
    if(input$level == "FSL"){
      updateSelectizeInput(session = getDefaultReactiveDomain(),"homeT","Home Team:",choices =  fslteamH[,1])
      updateSelectizeInput(session = getDefaultReactiveDomain(),"awayT","Away Team:",choices =  fslteamA[,1])
    }
    if(input$level == "MLB"){
      updateSelectizeInput(session = getDefaultReactiveDomain(),"homeT","Home Team:",choices =  mlbteamH[,1])
      updateSelectizeInput(session = getDefaultReactiveDomain(),"awayT","Away Team:",choices =  mlbteamA[,1])
    }
    if(input$level == "College (Statcast Parks Only)"){
      updateSelectizeInput(session = getDefaultReactiveDomain(),"homeT","Home Team:",choices =  sbteamH[,1])
      updateSelectizeInput(session = getDefaultReactiveDomain(),"awayT","Away Team:",choices =  sbteamA[,1])
    }
  })
  
  observeEvent(input$update, {
    if(input$level == "AAA"){
      pname <- aaaid |>
        filter(date == input$date) |>
        filter(teams_home_team_name == input$homeT) |>
        filter(teams_away_team_name == input$awayT) |>
        filter(game_number == input$gamenum)
      pname <- tryCatch({pitcher_summary(pname[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
      updateSelectizeInput(session = getDefaultReactiveDomain(),"pitcher","Pitcher:",choices =  pname[,3])
    }
    if(input$level == "FSL"){
      pname <- fslid |>
        filter(date == input$date) |>
        filter(teams_home_team_name == input$homeT) |>
        filter(teams_away_team_name == input$awayT) |>
        filter(game_number == input$gamenum)
      pname <- tryCatch({pitcher_summary(pname[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
      updateSelectizeInput(session = getDefaultReactiveDomain(),"pitcher","Pitcher:",choices =  pname[,3])
    }
    if(input$level == "MLB"){
      pname <- mlbid |>
        filter(date == as.character.Date(input$date)) |>
        filter(teams_home_team_name == input$homeT) |>
        filter(teams_away_team_name == input$awayT) |>
        filter(game_number == input$gamenum)
      pname <- tryCatch({pitcher_summary(pname[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
      updateSelectizeInput(session = getDefaultReactiveDomain(),"pitcher","Pitcher:",choices =  pname[,3])
    }
    if(input$level == "College (Statcast Parks Only)"){
      pname <- sbid |>
        filter(date == input$date) |>
        filter(teams_home_team_name == input$homeT) |>
        filter(teams_away_team_name == input$awayT) |>
        filter(game_number == input$gamenum)
      pname <- tryCatch({pitcher_summary(pname[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
      updateSelectizeInput(session = getDefaultReactiveDomain(),"pitcher","Pitcher:",choices =  pname[,3])
    }
  })
  
  combinedPlot <- reactiveVal()
  observeEvent(input$update1, {
    if(input$level == "MLB"){
      id <- mlbid |>
        filter(date == as.character.Date(input$date)) |>
        filter(teams_home_team_name == input$homeT) |>
        filter(teams_away_team_name == input$awayT) |>
        filter(game_number == input$gamenum)
      game <- tryCatch({pitcher_summary(id[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
      game <- game |>
        filter(`Pitcher Name`  == input$pitcher)
      id <- as.character(game[1,4])
    }
    if(input$level == "FSL"){
      id <- fslid |>
        filter(date == as.character.Date(input$date)) |>
        filter(teams_home_team_name == input$homeT) |>
        filter(teams_away_team_name == input$awayT) |>
        filter(game_number == input$gamenum)
      game <- tryCatch({pitcher_summary(id[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
      game <- game |>
        filter(`Pitcher Name`  == input$pitcher)
      id <- as.character(game[1,4])
    }
    if(input$level == "AAA"){
      id <- aaaid |>
        filter(date == as.character.Date(input$date)) |>
        filter(teams_home_team_name == input$homeT) |>
        filter(teams_away_team_name == input$awayT) |>
        filter(game_number == input$gamenum)
      game <- tryCatch({pitcher_summary(id[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
      game <- game |>
        filter(`Pitcher Name`  == input$pitcher)
      id <- as.character(game[1,4])
    }
    if(input$level == "College (Statcast Parks Only)"){
      id <- sbid |>
        filter(date == as.character.Date(input$date)) |>
        filter(teams_home_team_name == input$homeT) |>
        filter(teams_away_team_name == input$awayT) |>
        filter(game_number == input$gamenum)
      game <- tryCatch({pitcher_summary(id[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
      game <- game |>
        filter(`Pitcher Name`  == input$pitcher)
      id <- as.character(game[1,4])
    }
    
    break_plot <- break_plot(game)
    pitch_plot <- pitch_plot(game)
    
    # Create table plot
    table_plot <- tableGrob(summary_table(game),theme = ttheme_default(
      core = list(fg_params = list(cex = 1.5)),  # Increase text size
      colhead = list(fg_params = list(cex = 1.5)),
      rowhead = list(fg_params = list(cex = 2.5))))
    table_plot$widths[[1]] <- unit(0, "cm")
    
    # Find the text grobs in the first column
    text_grobs <- which(sapply(table_plot$grobs, function(g) {
      g$name == "core-fg-1" && inherits(g$children[[1]], "text")
    }))
    
    # Remove the numbers from these text grobs
    for (i in text_grobs) {
      text <- table_plot$grobs[[i]]$children[[1]]$label
      table_plot$grobs[[i]]$children[[1]]$label <- sub("^\\d+\\s*", "", text)
    }
    
    # Draw the modified table
    grid.newpage()
    grid.draw(table_plot)
    # Download and process image
    if(input$league == "MLB"){
      y <- paste("https://midfield.mlbstatic.com/v1/people/","/mlb/300?circle=false",sep = id)
    } else {
      y <- paste("https://midfield.mlbstatic.com/v1/people/","/milb/300?circle=false",sep = id)
    }
    photo <- tempfile()
    download.file(y, photo, mode = 'wb')
    img <- readPNG(photo)
    img_grob <- rasterGrob(img, interpolate = TRUE)
    
    # Combine all elements into one plot
    combined <- grid.arrange(
      arrangeGrob(table_plot, img_grob, ncol = 2, widths = c(2/3, 1/3)),
      arrangeGrob(break_plot, pitch_plot, ncol = 2),
      nrow = 2,
      top = textGrob(paste(input$title, "Made by @TimStats", sep = " "), gp = gpar(fontsize = 20, font = 2))
    )
    
    combinedPlot(combined)
    
    output$combinedPlot <- renderPlot({
      grid.draw(combinedPlot())
    })
  })
  
  output$downloadPlot <- downloadHandler(
    filename = function() {
      paste("baseball_card_", Sys.Date(), ".png", sep = "")
    },
    content = function(file) {
      ggsave(file, plot = combinedPlot(), width = 18, height = 12, dpi = 300)
    }
  )
  }


shinyApp(ui, server)