TimStats commited on
Commit
3322f58
·
verified ·
1 Parent(s): d1a2a92

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +89 -123
app.R CHANGED
@@ -21,11 +21,13 @@ is_barrel <- function(df) {
21
  hit_angle >= 117 & hit_speed + hit_angle >= 123, 1, 0))
22
  return(df)
23
  }
 
24
  VAA <- function(milbtotal){
25
  milbtotal <- milbtotal |>
26
  mutate(VAA = -atan((vz0+(az*(-sqrt((vy0*vy0)-(2*ay*(y0-(17/12))))-vy0)/
27
  ay))/(-sqrt((vy0*vy0)-(2*ay*(y0-(17/12))))))*(180/pi))
28
  }
 
29
  pitcher_summary <- function(game_pk,date){
30
  gdate <- as.Date.character(date)
31
  gdate <- as.Date(gdate)
@@ -56,8 +58,8 @@ pitcher_summary <- function(game_pk,date){
56
  tmilb <- tmilb |>
57
  mutate(date = gdate)
58
  return(tmilb)
59
-
60
  }
 
61
  break_plot <- function(game){
62
  ggplot()+
63
  geom_point(game,mapping =
@@ -73,8 +75,8 @@ break_plot <- function(game){
73
  size = 0.25,
74
  linetype = 1))+
75
  guides(color = guide_legend(title = "Pitch Type"))
76
-
77
  }
 
78
  pitch_plot <- function(game){
79
  ggplot()+
80
  geom_point(game,mapping =
@@ -94,6 +96,7 @@ pitch_plot <- function(game){
94
  axis.text.y=element_blank(),
95
  axis.ticks.y=element_blank())
96
  }
 
97
  summary_table <- function(game){
98
  rows <- nrow(game)
99
  sumtable <- game |>
@@ -126,7 +129,7 @@ summary_table <- function(game){
126
  'Whiff%' = round(sum(is_strike_swinging)/
127
  sum(swing)*100,digits = 1))
128
  }
129
- #MLB ID Stuff
130
  mlbid <- mlb_schedule(season = 2024, level_ids = "1")
131
  mlbteamH <- mlbid |>
132
  select(teams_home_team_name)
@@ -134,7 +137,7 @@ mlbteamH <- distinct(mlbteamH)
134
  mlbteamA <- mlbid |>
135
  select(teams_away_team_name)
136
  mlbteamA <- distinct(mlbteamA)
137
- #AAA ID Stuff
138
  aaaid <- mlb_schedule(season = 2024, level_ids = "11")
139
  aaateamH <- aaaid |>
140
  select(teams_home_team_name)
@@ -142,7 +145,7 @@ aaateamH <- distinct(aaateamH)
142
  aaateamA <- aaaid |>
143
  select(teams_away_team_name)
144
  aaateamA <- distinct(aaateamA)
145
- #FSL ID Stuff
146
  fslid <- mlb_schedule(season = 2024, level_ids = "14")
147
  fslid <- fslid |>
148
  filter(teams_home_team_name == "Daytona Tortugas" |
@@ -161,7 +164,7 @@ fslteamH <- distinct(fslteamH)
161
  fslteamA <- fslid |>
162
  select(teams_away_team_name)
163
  fslteamA <- distinct(fslteamA)
164
- #SB ID Stuff
165
  sbid <- mlb_schedule(season = 2024, level_ids = "22")
166
  sbteamH <- sbid |>
167
  select(teams_home_team_name)
@@ -169,97 +172,54 @@ sbteamH <- distinct(sbteamH)
169
  sbteamA <- sbid |>
170
  select(teams_away_team_name)
171
  sbteamA <- distinct(sbteamA)
172
- ui <- grid_page(
173
- layout = c(
174
- "header header",
175
- "sidebar area2 "
176
- ),
177
- row_sizes = c(
178
- "40px",
179
- "1fr"
180
- ),
181
- col_sizes = c(
182
- "280px",
183
- "1fr"
184
- ),
185
- gap_size = "1rem",
186
  tags$head(
187
  tags$style(HTML("
188
  @media (max-width: 768px) {
 
 
189
  .selectize-input { font-size: 14px; }
190
  .form-group { margin-bottom: 10px; }
191
  .action-button { width: 100%; }
 
192
  }
193
  "))
194
  ),
195
- sidebarPanel(
196
- dateInput("date",
197
- "Date:"),
198
- selectizeInput("level","Level:",c("MLB","AAA","FSL","Spring Breakout")),
199
- selectizeInput("homeT","Home Team:",mlbteamH[,1]),
200
- selectizeInput("awayT", "Away Team:",mlbteamA[,1]),
201
- selectizeInput("gamenum","Game Number (For Doubleheaders):",c("1","2")),
202
- #actionButton("update","Find Pitchers",icon("magnifying-glass"),
203
- # style = "color: #FFFFFF; background-color: #0077B6"),
204
- selectizeInput("pitcher","Pitcher Name:",c(" ")),
205
- selectizeInput("league","MLB or MiLB Photo",c("MLB","MiLB")),
206
- textInput("title","Card Title"),
207
- actionButton("update1","Make Card",icon("plus"),
208
- style = "color: #FFFFFF; background-color: #0077B6"),
209
- width = 15,
210
- ),
211
- grid_card_text(
212
- area = "header",
213
- content = "2024 MLB/AAA/FSL Summary Cards (BETA: Meant for Computers, If not all stats show, Zoom out on computer)",
214
- alignment = "start",
215
- is_title = FALSE
216
- ),
217
- grid_card(
218
- area = "area2",
219
- full_screen = TRUE,
220
- card_header(textOutput(outputId = "cardTitle")),
221
- card_body(
222
- gap = "0px",
223
- grid_container(
224
- layout = c(
225
- "area3 chart chart chart ",
226
- "breakgraph breakgraph strikezone strikezone"
227
- ),
228
- row_sizes = c(
229
- "1fr",
230
- "1fr"
231
- ),
232
- col_sizes = c(
233
- "1fr",
234
- "1fr",
235
- "1fr",
236
- "1fr"
237
- ),
238
- gap_size = "0px",
239
- grid_card_plot(area = "breakgraph"),
240
- grid_card_plot(area = "strikezone"),
241
- grid_card(
242
- area = "chart",
243
- card_body(
244
- tableOutput(
245
- outputId = "myTable"
246
- ),
247
- )
248
- ),
249
- grid_card(
250
- area = "area3",
251
- plotOutput(outputId = "picture")
252
- )
253
  )
254
  )
255
  )
256
  )
257
 
258
-
259
- server <- function(input, output) {
260
- # game <- reactive(pitcher_summary(input$game_pk))
261
- # id <- reactive(as.character(game()[1,4]))
262
- observeEvent(input$level,{
263
  if(input$level == "AAA"){
264
  updateSelectizeInput(session = getDefaultReactiveDomain(),"homeT","Home Team:",choices = aaateamH[,1])
265
  updateSelectizeInput(session = getDefaultReactiveDomain(),"awayT","Away Team:",choices = aaateamA[,1])
@@ -277,13 +237,14 @@ server <- function(input, output) {
277
  updateSelectizeInput(session = getDefaultReactiveDomain(),"awayT","Away Team:",choices = sbteamA[,1])
278
  }
279
  })
280
- observeEvent(input$awayT,{
 
281
  if(input$level == "AAA"){
282
  pname <- aaaid |>
283
- filter(date == input$date) |>
284
- filter(teams_home_team_name == input$homeT) |>
285
- filter(teams_away_team_name == input$awayT) |>
286
- filter(game_number == input$gamenum)
287
  pname <- tryCatch({pitcher_summary(pname[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
288
  updateSelectizeInput(session = getDefaultReactiveDomain(),"pitcher","Pitcher:(For Spring Breakout if pitchers not loading, try changing game number)",choices = pname[,3])
289
  }
@@ -307,21 +268,22 @@ server <- function(input, output) {
307
  }
308
  if(input$level == "Spring Breakout"){
309
  pname <- sbid |>
310
- filter(date == input$date) |>
311
- filter(teams_home_team_name == input$homeT) |>
312
- filter(teams_away_team_name == input$awayT) |>
313
- filter(game_number == input$gamenum)
314
  pname <- tryCatch({pitcher_summary(pname[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
315
  updateSelectizeInput(session = getDefaultReactiveDomain(),"pitcher","Pitcher:(For Spring Breakout if pitchers not loading, try changing game number)",choices = pname[,3])
316
  }
317
  })
318
- observeEvent(input$homeT,{
 
319
  if(input$level == "AAA"){
320
  pname <- aaaid |>
321
- filter(date == input$date) |>
322
- filter(teams_home_team_name == input$homeT) |>
323
- filter(teams_away_team_name == input$awayT) |>
324
- filter(game_number == input$gamenum)
325
  pname <- tryCatch({pitcher_summary(pname[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
326
  updateSelectizeInput(session = getDefaultReactiveDomain(),"pitcher","Pitcher:(For Spring Breakout if pitchers not loading, try changing game number)",choices = pname[,3])
327
  }
@@ -345,21 +307,22 @@ server <- function(input, output) {
345
  }
346
  if(input$level == "Spring Breakout"){
347
  pname <- sbid |>
348
- filter(date == input$date) |>
349
- filter(teams_home_team_name == input$homeT) |>
350
- filter(teams_away_team_name == input$awayT) |>
351
- filter(game_number == input$gamenum)
352
  pname <- tryCatch({pitcher_summary(pname[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
353
  updateSelectizeInput(session = getDefaultReactiveDomain(),"pitcher","Pitcher:(For Spring Breakout if pitchers not loading, try changing game number)",choices = pname[,3])
354
  }
355
  })
356
- observeEvent(input$gamenum,{
 
357
  if(input$level == "AAA"){
358
  pname <- aaaid |>
359
- filter(date == input$date) |>
360
- filter(teams_home_team_name == input$homeT) |>
361
- filter(teams_away_team_name == input$awayT) |>
362
- filter(game_number == input$gamenum)
363
  pname <- tryCatch({pitcher_summary(pname[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
364
  updateSelectizeInput(session = getDefaultReactiveDomain(),"pitcher","Pitcher:(For Spring Breakout if pitchers not loading, try changing game number)",choices = pname[,3])
365
  }
@@ -383,15 +346,16 @@ server <- function(input, output) {
383
  }
384
  if(input$level == "Spring Breakout"){
385
  pname <- sbid |>
386
- filter(date == input$date) |>
387
- filter(teams_home_team_name == input$homeT) |>
388
- filter(teams_away_team_name == input$awayT) |>
389
- filter(game_number == input$gamenum)
390
  pname <- tryCatch({pitcher_summary(pname[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
391
  updateSelectizeInput(session = getDefaultReactiveDomain(),"pitcher","Pitcher:(For Spring Breakout if pitchers not loading, try changing game number)",choices = pname[,3])
392
  }
393
  })
394
- observeEvent(input$update1,{
 
395
  if(input$level == "MLB"){
396
  id <- mlbid |>
397
  filter(date == as.character.Date(input$date)) |>
@@ -436,18 +400,20 @@ server <- function(input, output) {
436
  filter(`Pitcher Name` == input$pitcher)
437
  id <- as.character(game[1,4])
438
  }
439
- output$breakgraph <- renderPlot({
440
- break_plot(game)
 
441
  })
442
 
443
- output$strikezone <- renderPlot({
444
- pitch_plot(game)
445
  })
446
 
447
- output$myTable <- renderTable({
448
  t <- summary_table(game)
449
- return(t)
450
- },width = '1120px',digits = 1,na = "NA",align = 'c')
 
451
  output$picture <- renderImage({
452
  if(input$league == "MLB"){
453
  y <- paste("https://midfield.mlbstatic.com/v1/people/","/mlb/300?circle=false",sep = id)
@@ -460,12 +426,12 @@ server <- function(input, output) {
460
  list(
461
  src = photo
462
  )
463
- },deleteFile = TRUE)
464
- output$cardTitle <- renderText(
465
- title <- paste(input$title,"Made by @TimStats", sep = " ")
466
- )
 
467
  })
468
  }
469
 
470
-
471
  shinyApp(ui, server)
 
21
  hit_angle >= 117 & hit_speed + hit_angle >= 123, 1, 0))
22
  return(df)
23
  }
24
+
25
  VAA <- function(milbtotal){
26
  milbtotal <- milbtotal |>
27
  mutate(VAA = -atan((vz0+(az*(-sqrt((vy0*vy0)-(2*ay*(y0-(17/12))))-vy0)/
28
  ay))/(-sqrt((vy0*vy0)-(2*ay*(y0-(17/12))))))*(180/pi))
29
  }
30
+
31
  pitcher_summary <- function(game_pk,date){
32
  gdate <- as.Date.character(date)
33
  gdate <- as.Date(gdate)
 
58
  tmilb <- tmilb |>
59
  mutate(date = gdate)
60
  return(tmilb)
 
61
  }
62
+
63
  break_plot <- function(game){
64
  ggplot()+
65
  geom_point(game,mapping =
 
75
  size = 0.25,
76
  linetype = 1))+
77
  guides(color = guide_legend(title = "Pitch Type"))
 
78
  }
79
+
80
  pitch_plot <- function(game){
81
  ggplot()+
82
  geom_point(game,mapping =
 
96
  axis.text.y=element_blank(),
97
  axis.ticks.y=element_blank())
98
  }
99
+
100
  summary_table <- function(game){
101
  rows <- nrow(game)
102
  sumtable <- game |>
 
129
  'Whiff%' = round(sum(is_strike_swinging)/
130
  sum(swing)*100,digits = 1))
131
  }
132
+
133
  mlbid <- mlb_schedule(season = 2024, level_ids = "1")
134
  mlbteamH <- mlbid |>
135
  select(teams_home_team_name)
 
137
  mlbteamA <- mlbid |>
138
  select(teams_away_team_name)
139
  mlbteamA <- distinct(mlbteamA)
140
+
141
  aaaid <- mlb_schedule(season = 2024, level_ids = "11")
142
  aaateamH <- aaaid |>
143
  select(teams_home_team_name)
 
145
  aaateamA <- aaaid |>
146
  select(teams_away_team_name)
147
  aaateamA <- distinct(aaateamA)
148
+
149
  fslid <- mlb_schedule(season = 2024, level_ids = "14")
150
  fslid <- fslid |>
151
  filter(teams_home_team_name == "Daytona Tortugas" |
 
164
  fslteamA <- fslid |>
165
  select(teams_away_team_name)
166
  fslteamA <- distinct(fslteamA)
167
+
168
  sbid <- mlb_schedule(season = 2024, level_ids = "22")
169
  sbteamH <- sbid |>
170
  select(teams_home_team_name)
 
172
  sbteamA <- sbid |>
173
  select(teams_away_team_name)
174
  sbteamA <- distinct(sbteamA)
175
+
176
+ ui <- fluidPage(
 
 
 
 
 
 
 
 
 
 
 
 
177
  tags$head(
178
  tags$style(HTML("
179
  @media (max-width: 768px) {
180
+ .sidebar { width: 100%; float: none; }
181
+ .main-content { margin-left: 0; }
182
  .selectize-input { font-size: 14px; }
183
  .form-group { margin-bottom: 10px; }
184
  .action-button { width: 100%; }
185
+ .grid-card { width: 100% !important; }
186
  }
187
  "))
188
  ),
189
+ titlePanel("2024 MLB/AAA/FSL Summary Cards"),
190
+ sidebarLayout(
191
+ sidebarPanel(
192
+ dateInput("date", "Date:"),
193
+ selectizeInput("level", "Level:", c("MLB", "AAA", "FSL", "Spring Breakout")),
194
+ selectizeInput("homeT", "Home Team:", mlbteamH[,1]),
195
+ selectizeInput("awayT", "Away Team:", mlbteamA[,1]),
196
+ selectizeInput("gamenum", "Game Number (For Doubleheaders):", c("1", "2")),
197
+ selectizeInput("pitcher", "Pitcher Name:", c(" ")),
198
+ selectizeInput("league", "MLB or MiLB Photo", c("MLB", "MiLB")),
199
+ textInput("title", "Card Title"),
200
+ actionButton("update1", "Make Card", icon("plus"),
201
+ style = "color: #FFFFFF; background-color: #0077B6; width: 100%;")
202
+ ),
203
+ mainPanel(
204
+ fluidRow(
205
+ column(12, textOutput(outputId = "cardTitle"))
206
+ ),
207
+ fluidRow(
208
+ column(6, plotlyOutput(outputId = "breakgraph")),
209
+ column(6, plotlyOutput(outputId = "strikezone"))
210
+ ),
211
+ fluidRow(
212
+ column(12, DTOutput(outputId = "myTable"))
213
+ ),
214
+ fluidRow(
215
+ column(12, imageOutput(outputId = "picture"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216
  )
217
  )
218
  )
219
  )
220
 
221
+ server <- function(input, output, session) {
222
+ observeEvent(input$level, {
 
 
 
223
  if(input$level == "AAA"){
224
  updateSelectizeInput(session = getDefaultReactiveDomain(),"homeT","Home Team:",choices = aaateamH[,1])
225
  updateSelectizeInput(session = getDefaultReactiveDomain(),"awayT","Away Team:",choices = aaateamA[,1])
 
237
  updateSelectizeInput(session = getDefaultReactiveDomain(),"awayT","Away Team:",choices = sbteamA[,1])
238
  }
239
  })
240
+
241
+ observeEvent(input$awayT, {
242
  if(input$level == "AAA"){
243
  pname <- aaaid |>
244
+ filter(date == input$date) |>
245
+ filter(teams_home_team_name == input$homeT) |>
246
+ filter(teams_away_team_name == input$awayT) |>
247
+ filter(game_number == input$gamenum)
248
  pname <- tryCatch({pitcher_summary(pname[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
249
  updateSelectizeInput(session = getDefaultReactiveDomain(),"pitcher","Pitcher:(For Spring Breakout if pitchers not loading, try changing game number)",choices = pname[,3])
250
  }
 
268
  }
269
  if(input$level == "Spring Breakout"){
270
  pname <- sbid |>
271
+ filter(date == input$date) |>
272
+ filter(teams_home_team_name == input$homeT) |>
273
+ filter(teams_away_team_name == input$awayT) |>
274
+ filter(game_number == input$gamenum)
275
  pname <- tryCatch({pitcher_summary(pname[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
276
  updateSelectizeInput(session = getDefaultReactiveDomain(),"pitcher","Pitcher:(For Spring Breakout if pitchers not loading, try changing game number)",choices = pname[,3])
277
  }
278
  })
279
+
280
+ observeEvent(input$homeT, {
281
  if(input$level == "AAA"){
282
  pname <- aaaid |>
283
+ filter(date == input$date) |>
284
+ filter(teams_home_team_name == input$homeT) |>
285
+ filter(teams_away_team_name == input$awayT) |>
286
+ filter(game_number == input$gamenum)
287
  pname <- tryCatch({pitcher_summary(pname[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
288
  updateSelectizeInput(session = getDefaultReactiveDomain(),"pitcher","Pitcher:(For Spring Breakout if pitchers not loading, try changing game number)",choices = pname[,3])
289
  }
 
307
  }
308
  if(input$level == "Spring Breakout"){
309
  pname <- sbid |>
310
+ filter(date == input$date) |>
311
+ filter(teams_home_team_name == input$homeT) |>
312
+ filter(teams_away_team_name == input$awayT) |>
313
+ filter(game_number == input$gamenum)
314
  pname <- tryCatch({pitcher_summary(pname[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
315
  updateSelectizeInput(session = getDefaultReactiveDomain(),"pitcher","Pitcher:(For Spring Breakout if pitchers not loading, try changing game number)",choices = pname[,3])
316
  }
317
  })
318
+
319
+ observeEvent(input$gamenum, {
320
  if(input$level == "AAA"){
321
  pname <- aaaid |>
322
+ filter(date == input$date) |>
323
+ filter(teams_home_team_name == input$homeT) |>
324
+ filter(teams_away_team_name == input$awayT) |>
325
+ filter(game_number == input$gamenum)
326
  pname <- tryCatch({pitcher_summary(pname[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
327
  updateSelectizeInput(session = getDefaultReactiveDomain(),"pitcher","Pitcher:(For Spring Breakout if pitchers not loading, try changing game number)",choices = pname[,3])
328
  }
 
346
  }
347
  if(input$level == "Spring Breakout"){
348
  pname <- sbid |>
349
+ filter(date == input$date) |>
350
+ filter(teams_home_team_name == input$homeT) |>
351
+ filter(teams_away_team_name == input$awayT) |>
352
+ filter(game_number == input$gamenum)
353
  pname <- tryCatch({pitcher_summary(pname[,6],input$date)},error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
354
  updateSelectizeInput(session = getDefaultReactiveDomain(),"pitcher","Pitcher:(For Spring Breakout if pitchers not loading, try changing game number)",choices = pname[,3])
355
  }
356
  })
357
+
358
+ observeEvent(input$update1, {
359
  if(input$level == "MLB"){
360
  id <- mlbid |>
361
  filter(date == as.character.Date(input$date)) |>
 
400
  filter(`Pitcher Name` == input$pitcher)
401
  id <- as.character(game[1,4])
402
  }
403
+
404
+ output$breakgraph <- renderPlotly({
405
+ ggplotly(break_plot(game))
406
  })
407
 
408
+ output$strikezone <- renderPlotly({
409
+ ggplotly(pitch_plot(game))
410
  })
411
 
412
+ output$myTable <- renderDT({
413
  t <- summary_table(game)
414
+ datatable(t, options = list(scrollX = TRUE))
415
+ })
416
+
417
  output$picture <- renderImage({
418
  if(input$league == "MLB"){
419
  y <- paste("https://midfield.mlbstatic.com/v1/people/","/mlb/300?circle=false",sep = id)
 
426
  list(
427
  src = photo
428
  )
429
+ }, deleteFile = TRUE)
430
+
431
+ output$cardTitle <- renderText({
432
+ paste(input$title, "Made by @TimStats", sep = " ")
433
+ })
434
  })
435
  }
436
 
 
437
  shinyApp(ui, server)