TimStats commited on
Commit
ec45c0c
·
verified ·
1 Parent(s): b8b7425

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +683 -315
app.R CHANGED
@@ -3,11 +3,9 @@ library(gt)
3
  library(dplyr)
4
  library(shinyjs)
5
  library(shinyauthr)
6
- #library(gtExtras)
7
- library(httr)
8
  library(bslib)
9
-
10
- #Beam <- read.csv('Beam.csv')
11
 
12
  download_private_csv <- function(repo_id, filename) {
13
  url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/", filename)
@@ -19,10 +17,10 @@ download_private_csv <- function(repo_id, filename) {
19
 
20
  # Try different read options
21
  data <- read.csv(con,
22
- header = TRUE,
23
- check.names = FALSE, # This prevents R from modifying column names
24
- fileEncoding = "UTF-8",
25
- stringsAsFactors = FALSE)
26
  close(con)
27
  return(data)
28
  } else {
@@ -31,6 +29,8 @@ download_private_csv <- function(repo_id, filename) {
31
  }
32
 
33
  Beam <- download_private_csv("TimStats/CollegePriv", "Beam.csv")
 
 
34
 
35
  test <- Beam %>%
36
  select(Date,PitchofPA,Pitcher,PitcherId,PitcherThrows,PitcherTeam,Batter,BatterId,
@@ -74,6 +74,11 @@ test <- test %>%
74
  "AB" = Strikeout + BIP - Sac,
75
  "PA" = Strikeout + BIP + Walk + HBP
76
  )
 
 
 
 
 
77
 
78
 
79
  gt_theme_tim <- function(gt_object,...) {
@@ -159,51 +164,118 @@ gt_theme_tim <- function(gt_object,...) {
159
  }
160
 
161
  user_base <- tibble::tibble(
162
- user = c(Sys.getenv("Username")),
163
- password = c(Sys.getenv("Password")),
164
- permissions = c("admin"),
165
- name = c("User One")
166
  )
167
 
168
  # Define UI for application that draws a histogram
169
  ui <- fluidPage(
170
- div(class = "pull-right", shinyauthr::logoutUI(id = "logout")),
171
 
172
- # login section
173
- shinyauthr::loginUI(id = "login"),
 
 
 
 
 
 
174
 
 
 
 
 
 
 
175
  div(
176
  id = "bar",
177
- # Application title
178
- titlePanel("College Pitcher"),
179
-
180
- # Sidebar layout
181
- sidebarLayout(
182
- sidebarPanel(
183
- selectInput("player",
184
- "Select Player",
185
- choices = unique(test$Pitcher)),
186
- width = 3
187
- ),
188
 
189
- mainPanel(
190
- tabsetPanel(
191
- tabPanel("Splits",
192
- gt_output("CountFilter"),
193
- gt_output("PitchFilter"),
194
- gt_output("PitchSideFilter"),
195
- gt_output("InningFilter"),
196
- gt_output("SideFilter")
197
- )
198
  ),
199
- width = 9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
  )
201
- )
202
  ) %>% shinyjs::hidden()
203
  )
204
 
205
- # Define server logic required to draw a histogram
206
- server <- function(input, output) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
 
208
  credentials <- shinyauthr::loginServer(
209
  id = "login",
@@ -227,284 +299,580 @@ server <- function(input, output) {
227
  shinyjs::hide(id = "bar")
228
  }
229
  })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
 
231
- output$CountFilter <- render_gt({
232
- test <- test %>% filter(Pitcher == input$player) %>% group_by(Count) %>%
233
- summarise(
234
- Pitches = n(),
235
- #BBE = sum(BBE,na.rm = TRUE),
236
- "Whiff%" = mean(Whiff[Swing == TRUE],na.rm = TRUE),
237
- "SwStr%" = mean(Whiff,na.rm = TRUE),
238
- "CSW%" = mean(CSW,na.rm = TRUE),
239
- "Zone%" = mean(Zone,na.rm = TRUE),
240
- "ZCon%" = mean(Contact[Zone == TRUE & Swing == TRUE],na.rm = TRUE),
241
- "ZSwing%" = mean(Swing[Zone == TRUE],na.rm = TRUE),
242
- "OCon%" = mean(Contact[Zone == FALSE & Swing == TRUE],na.rm = TRUE),
243
- "OSwing%" = mean(Swing[Zone == FALSE],na.rm = TRUE),
244
- "Avg EV" = mean(ExitSpeed[BBE == TRUE],na.rm = TRUE),
245
- "EV90" = quantile(ExitSpeed[BBE == TRUE],.9,na.rm = TRUE),
246
- "Max EV" = max(ExitSpeed[BBE == TRUE],na.rm = TRUE),
247
- "Max EV" = ifelse(is.infinite(`Max EV`),NA,`Max EV`),
248
- "GB%" = mean(GB[BBE == TRUE],na.rm = TRUE),
249
- "LD%" = mean(LD[BBE == TRUE],na.rm = TRUE),
250
- "FB%" = mean(FB[BBE == TRUE],na.rm = TRUE),
251
- "AVG" = round(sum(Hit,na.rm = TRUE) / sum(AB,na.rm = TRUE),digits = 3),
252
- "OBP" = round((sum(Hit,na.rm = TRUE) + sum(Walk,na.rm = TRUE) + sum(HBP,na.rm = TRUE))/
253
- sum(PA,na.rm = TRUE),digits = 3),
254
- "SLG" = (round(sum(Single,na.rm = TRUE) + sum(Double,na.rm = TRUE) * 2 +
 
255
  sum(Triple,na.rm = TRUE) * 3 + sum(HR,na.rm = TRUE)))/sum(AB,na.rm = TRUE))%>%
256
- ungroup()%>%
257
- gt() %>%
258
- gt_theme_tim() %>%
259
- fmt_number(columns = `Avg EV`:`Max EV`,decimals = 1) %>%
260
- cols_width(Count:Pitches ~ px(70),
261
- everything() ~ px(85)) %>%
262
- cols_align(align = "center") %>%
263
- fmt_number(columns = AVG:SLG,decimals = 3) %>%
264
- fmt_percent(columns = ends_with("%")) %>%
265
- sub_missing() %>%
266
- #gt_theme_savant() %>%
267
- opt_interactive(
268
- use_compact_mode = TRUE,
269
- use_pagination = FALSE
270
- )
271
- })
272
- output$PitchFilter <- render_gt({
273
- tes <- test %>%
274
- filter(Pitcher == input$player) %>%
275
- group_by(AutoPitchType) %>%
276
- summarise(
277
- Pitches = n(),
278
- # BBE = sum(BBE,na.rm = TRUE),
279
- "Usage%" = n() / nrow(.), # Added Usage% calculation
280
- "Whiff%" = mean(Whiff[Swing == TRUE],na.rm = TRUE),
281
- "SwStr%" = mean(Whiff,na.rm = TRUE),
282
- "CSW%" = mean(CSW, na.rm = TRUE),
283
- "Zone%" = mean(Zone, na.rm = TRUE),
284
- "ZCon%" = mean(Contact[Zone == TRUE & Swing == TRUE], na.rm = TRUE),
285
- "ZSwing%" = mean(Swing[Zone == TRUE], na.rm = TRUE),
286
- "OCon%" = mean(Contact[Zone == FALSE & Swing == TRUE], na.rm = TRUE),
287
- "OSwing%" = mean(Swing[Zone == FALSE], na.rm = TRUE),
288
- "Avg EV" = mean(ExitSpeed[BBE == TRUE], na.rm = TRUE),
289
- "EV90" = quantile(ExitSpeed[BBE == TRUE], .9, na.rm = TRUE),
290
- "Max EV" = max(ExitSpeed[BBE == TRUE], na.rm = TRUE),
291
- "Max EV" = ifelse(is.infinite(`Max EV`), NA, `Max EV`),
292
- "GB%" = mean(GB[BBE == TRUE], na.rm = TRUE),
293
- "LD%" = mean(LD[BBE == TRUE], na.rm = TRUE),
294
- "FB%" = mean(FB[BBE == TRUE], na.rm = TRUE),
295
- "AVG" = round(sum(Hit, na.rm = TRUE) / sum(AB, na.rm = TRUE), digits = 3),
296
- "OBP" = round((sum(Hit, na.rm = TRUE) + sum(Walk, na.rm = TRUE) + sum(HBP, na.rm = TRUE)) /
297
- sum(PA, na.rm = TRUE), digits = 3),
298
- "SLG" = (round(sum(Single, na.rm = TRUE) + sum(Double, na.rm = TRUE) * 2 +
299
- sum(Triple, na.rm = TRUE) * 3 + sum(HR, na.rm = TRUE))) / sum(AB, na.rm = TRUE)
300
- ) %>%
301
- ungroup() %>%
302
- arrange(desc(Pitches)) %>%
303
- gt() %>%
304
- gt_theme_tim() %>%
305
- fmt_number(columns = `Avg EV`:`Max EV`, decimals = 1) %>%
306
- cols_label(AutoPitchType = "Type") %>%
307
- cols_width(
308
- AutoPitchType ~ px(100),
309
- Pitches ~ px(70),
310
- everything() ~ px(85)
311
- ) %>%
312
- cols_align(align = "center") %>%
313
- fmt_number(columns = AVG:SLG, decimals = 3) %>%
314
- fmt_percent(columns = ends_with("%")) %>%
315
- sub_missing() %>%
316
- opt_interactive(
317
- use_compact_mode = TRUE,
318
- use_pagination = FALSE
319
- )
320
- })
321
- output$PitchSideFilter <- render_gt({
322
- test <- test %>%
323
- filter(Pitcher == input$player) %>%
324
- group_by(BatterSide) %>%
325
- mutate(total_side = n()) %>%
326
- ungroup() %>%
327
- group_by(AutoPitchType, BatterSide) %>%
328
- summarise(
329
- Pitches = n(),
330
- #BBE = sum(BBE,na.rm = TRUE),
331
- "Usage%" = (n() / first(total_side)),
332
- "Whiff%" = mean(Whiff[Swing == TRUE],na.rm = TRUE),
333
- "SwStr%" = mean(Whiff,na.rm = TRUE),
334
- "CSW%" = mean(CSW, na.rm = TRUE),
335
- "Zone%" = mean(Zone, na.rm = TRUE),
336
- "ZCon%" = mean(Contact[Zone == TRUE & Swing == TRUE], na.rm = TRUE),
337
- "ZSwing%" = mean(Swing[Zone == TRUE], na.rm = TRUE),
338
- "OCon%" = mean(Contact[Zone == FALSE & Swing == TRUE], na.rm = TRUE),
339
- "OSwing%" = mean(Swing[Zone == FALSE], na.rm = TRUE),
340
- "Avg EV" = mean(ExitSpeed[BBE == TRUE], na.rm = TRUE),
341
- "EV90" = quantile(ExitSpeed[BBE == TRUE], .9, na.rm = TRUE),
342
- "Max EV" = max(ExitSpeed[BBE == TRUE], na.rm = TRUE),
343
- "Max EV" = ifelse(is.infinite(`Max EV`), NA, `Max EV`),
344
- "GB%" = mean(GB[BBE == TRUE], na.rm = TRUE),
345
- "LD%" = mean(LD[BBE == TRUE], na.rm = TRUE),
346
- "FB%" = mean(FB[BBE == TRUE], na.rm = TRUE),
347
- "AVG" = round(sum(Hit, na.rm = TRUE) / sum(AB, na.rm = TRUE), digits = 3),
348
- "OBP" = round((sum(Hit, na.rm = TRUE) + sum(Walk, na.rm = TRUE) + sum(HBP, na.rm = TRUE)) /
349
- sum(PA, na.rm = TRUE), digits = 3),
350
- "SLG" = (round(sum(Single, na.rm = TRUE) + sum(Double, na.rm = TRUE) * 2 +
351
- sum(Triple, na.rm = TRUE) * 3 + sum(HR, na.rm = TRUE))) / sum(AB, na.rm = TRUE)
352
- ) %>%
353
- ungroup() %>%
354
- arrange(desc(BatterSide),desc(Pitches)) %>%
355
- gt() %>%
356
- gt_theme_tim() %>%
357
- fmt_number(columns = `Avg EV`:`Max EV`, decimals = 1) %>%
358
- cols_label(AutoPitchType = "Type", BatterSide = "Side") %>%
359
- cols_width(
360
- AutoPitchType ~ px(100),
361
- Pitches ~ px(70),
362
- BatterSide ~ px(70),
363
- everything() ~ px(85)
364
- ) %>%
365
- cols_align(align = "center") %>%
366
- fmt_number(columns = AVG:SLG, decimals = 3) %>%
367
- fmt_percent(columns = ends_with("%")) %>%
368
- sub_missing() %>%
369
- opt_interactive(
370
- use_compact_mode = TRUE,
371
- use_pagination = FALSE
372
- )
373
- })
374
- output$InningFilter <- render_gt({
375
- test <- test %>% filter(Pitcher == input$player) %>% group_by(Inning) %>%
376
- summarise(
377
- Pitches = n(),
378
- #BBE = sum(BBE,na.rm = TRUE),
379
- "Whiff%" = mean(Whiff[Swing == TRUE],na.rm = TRUE),
380
- "SwStr%" = mean(Whiff,na.rm = TRUE),
381
- "CSW%" = mean(CSW,na.rm = TRUE),
382
- "Zone%" = mean(Zone,na.rm = TRUE),
383
- "ZCon%" = mean(Contact[Zone == TRUE & Swing == TRUE],na.rm = TRUE),
384
- "ZSwing%" = mean(Swing[Zone == TRUE],na.rm = TRUE),
385
- "OCon%" = mean(Contact[Zone == FALSE & Swing == TRUE],na.rm = TRUE),
386
- "OSwing%" = mean(Swing[Zone == FALSE],na.rm = TRUE),
387
- "Avg EV" = mean(ExitSpeed[BBE == TRUE],na.rm = TRUE),
388
- "EV90" = quantile(ExitSpeed[BBE == TRUE],.9,na.rm = TRUE),
389
- "Max EV" = max(ExitSpeed[BBE == TRUE],na.rm = TRUE),
390
- "Max EV" = ifelse(is.infinite(`Max EV`),NA,`Max EV`),
391
- "GB%" = mean(GB[BBE == TRUE],na.rm = TRUE),
392
- "LD%" = mean(LD[BBE == TRUE],na.rm = TRUE),
393
- "FB%" = mean(FB[BBE == TRUE],na.rm = TRUE),
394
- "AVG" = round(sum(Hit,na.rm = TRUE) / sum(AB,na.rm = TRUE),digits = 3),
395
- "OBP" = round((sum(Hit,na.rm = TRUE) + sum(Walk,na.rm = TRUE) + sum(HBP,na.rm = TRUE))/
396
- sum(PA,na.rm = TRUE),digits = 3),
397
- "SLG" = (round(sum(Single,na.rm = TRUE) + sum(Double,na.rm = TRUE) * 2 +
398
- sum(Triple,na.rm = TRUE) * 3 + sum(HR,na.rm = TRUE)))/sum(AB,na.rm = TRUE))%>%
399
- ungroup()%>%
400
- gt() %>%
401
- gt_theme_tim() %>%
402
- fmt_number(columns = `Avg EV`:`Max EV`,decimals = 1) %>%
403
- cols_width(Inning:Pitches ~ px(70),
404
- everything() ~ px(85)) %>%
405
- cols_align(align = "center") %>%
406
- fmt_number(columns = AVG:SLG,decimals = 3) %>%
407
- fmt_percent(columns = ends_with("%")) %>%
408
- sub_missing() %>%
409
- #gt_theme_savant() %>%
410
- opt_interactive(
411
- use_compact_mode = TRUE,
412
- use_pagination = FALSE
413
- )
414
- })
415
- output$SideFilter <- render_gt({
416
- test1 <- test %>% filter(Pitcher == input$player) %>% group_by(BatterSide) %>%
417
- summarise(
418
- Pitches = n(),
419
- #BBE = sum(BBE,na.rm = TRUE),
420
- "Whiff%" = mean(Whiff[Swing == TRUE],na.rm = TRUE),
421
- "SwStr%" = mean(Whiff,na.rm = TRUE),
422
- "CSW%" = mean(CSW,na.rm = TRUE),
423
- "Zone%" = mean(Zone,na.rm = TRUE),
424
- "ZCon%" = mean(Contact[Zone == TRUE & Swing == TRUE],na.rm = TRUE),
425
- "ZSwing%" = mean(Swing[Zone == TRUE],na.rm = TRUE),
426
- "OCon%" = mean(Contact[Zone == FALSE & Swing == TRUE],na.rm = TRUE),
427
- "OSwing%" = mean(Swing[Zone == FALSE],na.rm = TRUE),
428
- "Avg EV" = mean(ExitSpeed[BBE == TRUE],na.rm = TRUE),
429
- "EV90" = quantile(ExitSpeed[BBE == TRUE],.9,na.rm = TRUE),
430
- "Max EV" = max(ExitSpeed[BBE == TRUE],na.rm = TRUE),
431
- "Max EV" = ifelse(is.infinite(`Max EV`),NA,`Max EV`),
432
- "GB%" = mean(GB[BBE == TRUE],na.rm = TRUE),
433
- "LD%" = mean(LD[BBE == TRUE],na.rm = TRUE),
434
- "FB%" = mean(FB[BBE == TRUE],na.rm = TRUE),
435
- "AVG" = round(sum(Hit,na.rm = TRUE) / sum(AB,na.rm = TRUE),digits = 3),
436
- "OBP" = round((sum(Hit,na.rm = TRUE) + sum(Walk,na.rm = TRUE) + sum(HBP,na.rm = TRUE))/
437
- sum(PA,na.rm = TRUE),digits = 3),
438
- "SLG" = (round(sum(Single,na.rm = TRUE) + sum(Double,na.rm = TRUE) * 2 +
439
- sum(Triple,na.rm = TRUE) * 3 + sum(HR,na.rm = TRUE)))/sum(AB,na.rm = TRUE))%>%
440
- ungroup()
441
- test2 <- test %>% mutate(BatterSide = "Both") %>%filter(Pitcher == input$player) %>% group_by(BatterSide) %>%
442
- summarise(
443
- Pitches = n(),
444
- #BBE = sum(BBE,na.rm = TRUE),
445
- "Whiff%" = mean(Whiff[Swing == TRUE],na.rm = TRUE),
446
- "SwStr%" = mean(Whiff,na.rm = TRUE),
447
- "CSW%" = mean(CSW,na.rm = TRUE),
448
- "Zone%" = mean(Zone,na.rm = TRUE),
449
- "ZCon%" = mean(Contact[Zone == TRUE & Swing == TRUE],na.rm = TRUE),
450
- "ZSwing%" = mean(Swing[Zone == TRUE],na.rm = TRUE),
451
- "OCon%" = mean(Contact[Zone == FALSE & Swing == TRUE],na.rm = TRUE),
452
- "OSwing%" = mean(Swing[Zone == FALSE],na.rm = TRUE),
453
- "Avg EV" = mean(ExitSpeed[BBE == TRUE],na.rm = TRUE),
454
- "EV90" = quantile(ExitSpeed[BBE == TRUE],.9,na.rm = TRUE),
455
- "Max EV" = max(ExitSpeed[BBE == TRUE],na.rm = TRUE),
456
- "Max EV" = ifelse(is.infinite(`Max EV`),NA,`Max EV`),
457
- "GB%" = mean(GB[BBE == TRUE],na.rm = TRUE),
458
- "LD%" = mean(LD[BBE == TRUE],na.rm = TRUE),
459
- "FB%" = mean(FB[BBE == TRUE],na.rm = TRUE),
460
- "AVG" = round(sum(Hit,na.rm = TRUE) / sum(AB,na.rm = TRUE),digits = 3),
461
- "OBP" = round((sum(Hit,na.rm = TRUE) + sum(Walk,na.rm = TRUE) + sum(HBP,na.rm = TRUE))/
462
- sum(PA,na.rm = TRUE),digits = 3),
463
- "SLG" = (round(sum(Single,na.rm = TRUE) + sum(Double,na.rm = TRUE) * 2 +
464
- sum(Triple,na.rm = TRUE) * 3 + sum(HR,na.rm = TRUE)))/sum(AB,na.rm = TRUE))%>%
465
- ungroup()
466
- test <- rbind(test1,test2)
467
- test %>%
468
- gt() %>%
469
- gt_theme_tim() %>%
470
- fmt_number(columns = `Avg EV`:`Max EV`,decimals = 1) %>%
471
- cols_width(BatterSide:Pitches ~ px(70),
472
- everything() ~ px(85)) %>%
473
- cols_align(align = "center") %>%
474
- cols_label(BatterSide = "Side") %>%
475
- fmt_number(columns = AVG:SLG,decimals = 3) %>%
476
- fmt_percent(columns = ends_with("%")) %>%
477
- sub_missing() %>%
478
- #gt_theme_savant() %>%
479
- opt_interactive(
480
- use_compact_mode = TRUE,
481
- use_pagination = FALSE
482
- )
483
-
484
- })
485
- output$slashline <- render_gt({
486
- test <- test %>% filter(Pitcher == input$player) %>%
487
- summarise(
488
- Pitches = n(),
489
- `1B` = sum(Single,na.rm = TRUE),
490
- `2B` = sum(Double,na.rm = TRUE),
491
- `3B` = sum(Triple,na.rm = TRUE),
492
- HR = sum(HR,na.rm = TRUE),
493
- SO = sum(Strikeout,na.rm = TRUE),
494
- BB = sum(Walk,na.rm = TRUE),
495
- Whiffs = sum(Whiff,na.rm = TRUE),
496
- `Hard Hits` = sum(HardHit,na.rm = TRUE)
497
- )%>%
498
- ungroup()%>%
499
- gt() %>%
500
- gt_theme_tim() %>%
501
- cols_align(align = "center") %>%
502
- # gt_theme_savant() %>%
503
- opt_interactive(
504
- use_compact_mode = TRUE,
505
- use_pagination = FALSE
506
- )
507
- })
 
 
 
 
 
 
 
508
  }
509
 
510
  # Run the application
 
3
  library(dplyr)
4
  library(shinyjs)
5
  library(shinyauthr)
6
+ library(gtExtras)
 
7
  library(bslib)
8
+ library(baseballr)
 
9
 
10
  download_private_csv <- function(repo_id, filename) {
11
  url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/", filename)
 
17
 
18
  # Try different read options
19
  data <- read.csv(con,
20
+ header = TRUE,
21
+ check.names = FALSE, # This prevents R from modifying column names
22
+ fileEncoding = "UTF-8",
23
+ stringsAsFactors = FALSE)
24
  close(con)
25
  return(data)
26
  } else {
 
29
  }
30
 
31
  Beam <- download_private_csv("TimStats/CollegePriv", "Beam.csv")
32
+ Beam <- download_private_csv("TimStats/TMP", "Beam.csv")
33
+ Beam <- download_private_csv("TimStats/RMB", "Beam.csv")
34
 
35
  test <- Beam %>%
36
  select(Date,PitchofPA,Pitcher,PitcherId,PitcherThrows,PitcherTeam,Batter,BatterId,
 
74
  "AB" = Strikeout + BIP - Sac,
75
  "PA" = Strikeout + BIP + Walk + HBP
76
  )
77
+ test <- test %>%
78
+ left_join(TMB, by = c(BatterTeam = "team_abbr")) %>%
79
+ left_join(TMP, by = c(PitcherTeam = "team_abbr")) %>%
80
+ mutate(PitcherAndTeam = paste0(Pitcher," - ",PTeamName)) %>%
81
+ mutate(BatterAndTeam = paste0(Batter," - ",BTeamName))
82
 
83
 
84
  gt_theme_tim <- function(gt_object,...) {
 
164
  }
165
 
166
  user_base <- tibble::tibble(
167
+ user = c("TrentR", "TimChen"),
168
+ password = (c("Caprisun", "TimStats")),
169
+ permissions = c("admin", "standard"),
170
+ name = c("User One", "User Two")
171
  )
172
 
173
  # Define UI for application that draws a histogram
174
  ui <- fluidPage(
 
175
 
176
+ tags$head(
177
+ tags$style(
178
+ HTML("
179
+ .shiny-output-error { visibility: hidden; }
180
+ .shiny-output-error:before { visibility: hidden; }
181
+ ")
182
+ )
183
+ ),
184
 
185
+ # Rest of your existing UI code stays exactly the same
186
+ div(class = "pull-right", shinyauthr::logoutUI(id = "logout")),
187
+ # div(class = "pull-right", shinyauthr::logoutUI(id = "logout")),
188
+ #
189
+ # shinyauthr::loginUI(id = "login"),
190
+ #
191
  div(
192
  id = "bar",
193
+ titlePanel("TimMedia"),
 
 
 
 
 
 
 
 
 
 
194
 
195
+ sidebarLayout(
196
+ sidebarPanel(
197
+ uiOutput("dynamicSelect"), # Replace selectInput with uiOutput
198
+ dateRangeInput("date","Date Range:",start = "2024-01-01"),
199
+ imageOutput("teamLogo", height = "150px"),
200
+ width = 3
 
 
 
201
  ),
202
+
203
+ mainPanel(
204
+ tabsetPanel(id = "tabs", # Add id here
205
+ tabPanel("Pitcher Splits",
206
+ gt_output("CountFilterP"),
207
+ gt_output("PitchFilterP"),
208
+ gt_output("PitchSideFilterP"),
209
+ gt_output("InningFilterP"),
210
+ gt_output("SideFilterP")
211
+ ),
212
+ tabPanel("Batter Splits",
213
+ gt_output("CountFilterB"),
214
+ gt_output("PitchFilterB"),
215
+ gt_output("PitchSideFilterB"),
216
+ gt_output("InningFilterB"),
217
+ gt_output("SideFilterB")
218
+ )
219
+ ),
220
+ width = 9
221
+ )
222
  )
 
223
  ) %>% shinyjs::hidden()
224
  )
225
 
226
+ # Then in the server, add these new reactive elements:
227
+ server <- function(input, output, session) {
228
+ options(shiny.sanitize.errors = TRUE)
229
+
230
+ # Add this reactive element
231
+ playerType <- reactive({
232
+ if(input$tabs == "Pitcher Splits") {
233
+ list(
234
+ choices = unique(test$PitcherAndTeam),
235
+ selected = NULL,
236
+ logo_column = "PTeamLogo",
237
+ filter_column = "PitcherAndTeam"
238
+ )
239
+ } else {
240
+ list(
241
+ choices = unique(test$BatterAndTeam),
242
+ selected = NULL,
243
+ logo_column = "BTeamLogo",
244
+ filter_column = "BatterAndTeam"
245
+ )
246
+ }
247
+ })
248
+
249
+ # Create dynamic select input
250
+ output$dynamicSelect <- renderUI({
251
+ selectInput("player",
252
+ "Select Player",
253
+ choices = playerType()$choices)
254
+ })
255
+
256
+ # Modify the team logo render
257
+ output$teamLogo <- renderImage({
258
+ req(input$player)
259
+
260
+ selected_logo <- test %>%
261
+ filter(!!sym(playerType()$filter_column) == input$player) %>%
262
+ slice(1) %>%
263
+ pull(!!sym(playerType()$logo_column))
264
+
265
+ temp_file <- tempfile(fileext = ".png")
266
+ download.file(selected_logo, temp_file, mode = "wb")
267
+
268
+ list(
269
+ src = temp_file,
270
+ contentType = "image/png",
271
+ width = "150px",
272
+ height = "150px",
273
+ alt = "Team Logo",
274
+ deleteFile = TRUE,
275
+ style = "display: block; margin: auto;"
276
+ )
277
+ }, deleteFile = TRUE)
278
+
279
 
280
  credentials <- shinyauthr::loginServer(
281
  id = "login",
 
299
  shinyjs::hide(id = "bar")
300
  }
301
  })
302
+ # observeEvent(input$date,{
303
+ # test <- test %>% filter(between(as.Date(Date),input$date[1],input$date[2]))
304
+ # })
305
+
306
+ output$CountFilterP <- render_gt({
307
+ test <- test %>% filter(PitcherAndTeam == input$player) %>% group_by(Count) %>%
308
+ filter(between(as.Date(Date),input$date[1],input$date[2])) %>%
309
+ summarise(
310
+ Pitches = n(),
311
+ #BBE = sum(BBE,na.rm = TRUE),
312
+ "Whiff%" = mean(Whiff[Swing == TRUE],na.rm = TRUE),
313
+ "SwStr%" = mean(Whiff,na.rm = TRUE),
314
+ "CSW%" = mean(CSW,na.rm = TRUE),
315
+ "Zone%" = mean(Zone,na.rm = TRUE),
316
+ "ZCon%" = mean(Contact[Zone == TRUE & Swing == TRUE],na.rm = TRUE),
317
+ "ZSwing%" = mean(Swing[Zone == TRUE],na.rm = TRUE),
318
+ "OCon%" = mean(Contact[Zone == FALSE & Swing == TRUE],na.rm = TRUE),
319
+ "OSwing%" = mean(Swing[Zone == FALSE],na.rm = TRUE),
320
+ "Avg EV" = mean(ExitSpeed[BBE == TRUE],na.rm = TRUE),
321
+ "EV90" = quantile(ExitSpeed[BBE == TRUE],.9,na.rm = TRUE),
322
+ "Max EV" = max(ExitSpeed[BBE == TRUE],na.rm = TRUE),
323
+ "Max EV" = ifelse(is.infinite(`Max EV`),NA,`Max EV`),
324
+ "GB%" = mean(GB[BBE == TRUE],na.rm = TRUE),
325
+ "LD%" = mean(LD[BBE == TRUE],na.rm = TRUE),
326
+ "FB%" = mean(FB[BBE == TRUE],na.rm = TRUE),
327
+ "AVG" = round(sum(Hit,na.rm = TRUE) / sum(AB,na.rm = TRUE),digits = 3),
328
+ "OBP" = round((sum(Hit,na.rm = TRUE) + sum(Walk,na.rm = TRUE) + sum(HBP,na.rm = TRUE))/
329
+ sum(PA,na.rm = TRUE),digits = 3),
330
+ "SLG" = (round(sum(Single,na.rm = TRUE) + sum(Double,na.rm = TRUE) * 2 +
331
+ sum(Triple,na.rm = TRUE) * 3 + sum(HR,na.rm = TRUE)))/sum(AB,na.rm = TRUE))%>%
332
+ ungroup()%>%
333
+ gt() %>%
334
+ gt_theme_tim() %>%
335
+ fmt_number(columns = `Avg EV`:`Max EV`,decimals = 1) %>%
336
+ cols_width(Count:Pitches ~ px(70),
337
+ everything() ~ px(85)) %>%
338
+ cols_align(align = "center") %>%
339
+ fmt_number(columns = AVG:SLG,decimals = 3) %>%
340
+ fmt_percent(columns = ends_with("%")) %>%
341
+ sub_missing() %>%
342
+ #gt_theme_savant() %>%
343
+ opt_interactive(
344
+ use_compact_mode = TRUE,
345
+ use_pagination = FALSE
346
+ )
347
+ })
348
+ output$PitchFilterP <- render_gt({
349
+ tes <- test %>%
350
+ filter(PitcherAndTeam == input$player) %>%
351
+ filter(between(as.Date(Date),input$date[1],input$date[2])) %>%
352
+ group_by(AutoPitchType) %>%
353
+ summarise(
354
+ Pitches = n(),
355
+ # BBE = sum(BBE,na.rm = TRUE),
356
+ "Usage%" = n() / nrow(.), # Added Usage% calculation
357
+ "Whiff%" = mean(Whiff[Swing == TRUE],na.rm = TRUE),
358
+ "SwStr%" = mean(Whiff,na.rm = TRUE),
359
+ "CSW%" = mean(CSW, na.rm = TRUE),
360
+ "Zone%" = mean(Zone, na.rm = TRUE),
361
+ "ZCon%" = mean(Contact[Zone == TRUE & Swing == TRUE], na.rm = TRUE),
362
+ "ZSwing%" = mean(Swing[Zone == TRUE], na.rm = TRUE),
363
+ "OCon%" = mean(Contact[Zone == FALSE & Swing == TRUE], na.rm = TRUE),
364
+ "OSwing%" = mean(Swing[Zone == FALSE], na.rm = TRUE),
365
+ "Avg EV" = mean(ExitSpeed[BBE == TRUE], na.rm = TRUE),
366
+ "EV90" = quantile(ExitSpeed[BBE == TRUE], .9, na.rm = TRUE),
367
+ "Max EV" = max(ExitSpeed[BBE == TRUE], na.rm = TRUE),
368
+ "Max EV" = ifelse(is.infinite(`Max EV`), NA, `Max EV`),
369
+ "GB%" = mean(GB[BBE == TRUE], na.rm = TRUE),
370
+ "LD%" = mean(LD[BBE == TRUE], na.rm = TRUE),
371
+ "FB%" = mean(FB[BBE == TRUE], na.rm = TRUE),
372
+ "AVG" = round(sum(Hit, na.rm = TRUE) / sum(AB, na.rm = TRUE), digits = 3),
373
+ "OBP" = round((sum(Hit, na.rm = TRUE) + sum(Walk, na.rm = TRUE) + sum(HBP, na.rm = TRUE)) /
374
+ sum(PA, na.rm = TRUE), digits = 3),
375
+ "SLG" = (round(sum(Single, na.rm = TRUE) + sum(Double, na.rm = TRUE) * 2 +
376
+ sum(Triple, na.rm = TRUE) * 3 + sum(HR, na.rm = TRUE))) / sum(AB, na.rm = TRUE)
377
+ ) %>%
378
+ ungroup() %>%
379
+ arrange(desc(Pitches)) %>%
380
+ gt() %>%
381
+ gt_theme_tim() %>%
382
+ fmt_number(columns = `Avg EV`:`Max EV`, decimals = 1) %>%
383
+ cols_label(AutoPitchType = "Type") %>%
384
+ cols_width(
385
+ AutoPitchType ~ px(100),
386
+ Pitches ~ px(70),
387
+ everything() ~ px(85)
388
+ ) %>%
389
+ cols_align(align = "center") %>%
390
+ fmt_number(columns = AVG:SLG, decimals = 3) %>%
391
+ fmt_percent(columns = ends_with("%")) %>%
392
+ sub_missing() %>%
393
+ opt_interactive(
394
+ use_compact_mode = TRUE,
395
+ use_pagination = FALSE
396
+ )
397
+ })
398
+ output$PitchSideFilterP <- render_gt({
399
+ test <- test %>%
400
+ filter(PitcherAndTeam == input$player) %>%
401
+ filter(between(as.Date(Date),input$date[1],input$date[2])) %>%
402
+ group_by(BatterSide) %>%
403
+ mutate(total_side = n()) %>%
404
+ ungroup() %>%
405
+ group_by(AutoPitchType, BatterSide) %>%
406
+ summarise(
407
+ Pitches = n(),
408
+ #BBE = sum(BBE,na.rm = TRUE),
409
+ "Usage%" = (n() / first(total_side)),
410
+ "Whiff%" = mean(Whiff[Swing == TRUE],na.rm = TRUE),
411
+ "SwStr%" = mean(Whiff,na.rm = TRUE),
412
+ "CSW%" = mean(CSW, na.rm = TRUE),
413
+ "Zone%" = mean(Zone, na.rm = TRUE),
414
+ "ZCon%" = mean(Contact[Zone == TRUE & Swing == TRUE], na.rm = TRUE),
415
+ "ZSwing%" = mean(Swing[Zone == TRUE], na.rm = TRUE),
416
+ "OCon%" = mean(Contact[Zone == FALSE & Swing == TRUE], na.rm = TRUE),
417
+ "OSwing%" = mean(Swing[Zone == FALSE], na.rm = TRUE),
418
+ "Avg EV" = mean(ExitSpeed[BBE == TRUE], na.rm = TRUE),
419
+ "EV90" = quantile(ExitSpeed[BBE == TRUE], .9, na.rm = TRUE),
420
+ "Max EV" = max(ExitSpeed[BBE == TRUE], na.rm = TRUE),
421
+ "Max EV" = ifelse(is.infinite(`Max EV`), NA, `Max EV`),
422
+ "GB%" = mean(GB[BBE == TRUE], na.rm = TRUE),
423
+ "LD%" = mean(LD[BBE == TRUE], na.rm = TRUE),
424
+ "FB%" = mean(FB[BBE == TRUE], na.rm = TRUE),
425
+ "AVG" = round(sum(Hit, na.rm = TRUE) / sum(AB, na.rm = TRUE), digits = 3),
426
+ "OBP" = round((sum(Hit, na.rm = TRUE) + sum(Walk, na.rm = TRUE) + sum(HBP, na.rm = TRUE)) /
427
+ sum(PA, na.rm = TRUE), digits = 3),
428
+ "SLG" = (round(sum(Single, na.rm = TRUE) + sum(Double, na.rm = TRUE) * 2 +
429
+ sum(Triple, na.rm = TRUE) * 3 + sum(HR, na.rm = TRUE))) / sum(AB, na.rm = TRUE)
430
+ ) %>%
431
+ ungroup() %>%
432
+ arrange(desc(BatterSide),desc(Pitches)) %>%
433
+ gt() %>%
434
+ gt_theme_tim() %>%
435
+ fmt_number(columns = `Avg EV`:`Max EV`, decimals = 1) %>%
436
+ cols_label(AutoPitchType = "Type", BatterSide = "Side") %>%
437
+ cols_width(
438
+ AutoPitchType ~ px(100),
439
+ Pitches ~ px(70),
440
+ BatterSide ~ px(70),
441
+ everything() ~ px(85)
442
+ ) %>%
443
+ cols_align(align = "center") %>%
444
+ fmt_number(columns = AVG:SLG, decimals = 3) %>%
445
+ fmt_percent(columns = ends_with("%")) %>%
446
+ sub_missing() %>%
447
+ opt_interactive(
448
+ use_compact_mode = TRUE,
449
+ use_pagination = FALSE
450
+ )
451
+ })
452
+ output$InningFilterP <- render_gt({
453
+ test <- test %>% filter(PitcherAndTeam == input$player) %>% group_by(Inning) %>%
454
+ filter(between(as.Date(Date),input$date[1],input$date[2])) %>%
455
+ summarise(
456
+ Pitches = n(),
457
+ #BBE = sum(BBE,na.rm = TRUE),
458
+ "Whiff%" = mean(Whiff[Swing == TRUE],na.rm = TRUE),
459
+ "SwStr%" = mean(Whiff,na.rm = TRUE),
460
+ "CSW%" = mean(CSW,na.rm = TRUE),
461
+ "Zone%" = mean(Zone,na.rm = TRUE),
462
+ "ZCon%" = mean(Contact[Zone == TRUE & Swing == TRUE],na.rm = TRUE),
463
+ "ZSwing%" = mean(Swing[Zone == TRUE],na.rm = TRUE),
464
+ "OCon%" = mean(Contact[Zone == FALSE & Swing == TRUE],na.rm = TRUE),
465
+ "OSwing%" = mean(Swing[Zone == FALSE],na.rm = TRUE),
466
+ "Avg EV" = mean(ExitSpeed[BBE == TRUE],na.rm = TRUE),
467
+ "EV90" = quantile(ExitSpeed[BBE == TRUE],.9,na.rm = TRUE),
468
+ "Max EV" = max(ExitSpeed[BBE == TRUE],na.rm = TRUE),
469
+ "Max EV" = ifelse(is.infinite(`Max EV`),NA,`Max EV`),
470
+ "GB%" = mean(GB[BBE == TRUE],na.rm = TRUE),
471
+ "LD%" = mean(LD[BBE == TRUE],na.rm = TRUE),
472
+ "FB%" = mean(FB[BBE == TRUE],na.rm = TRUE),
473
+ "AVG" = round(sum(Hit,na.rm = TRUE) / sum(AB,na.rm = TRUE),digits = 3),
474
+ "OBP" = round((sum(Hit,na.rm = TRUE) + sum(Walk,na.rm = TRUE) + sum(HBP,na.rm = TRUE))/
475
+ sum(PA,na.rm = TRUE),digits = 3),
476
+ "SLG" = (round(sum(Single,na.rm = TRUE) + sum(Double,na.rm = TRUE) * 2 +
477
+ sum(Triple,na.rm = TRUE) * 3 + sum(HR,na.rm = TRUE)))/sum(AB,na.rm = TRUE))%>%
478
+ ungroup()%>%
479
+ gt() %>%
480
+ gt_theme_tim() %>%
481
+ fmt_number(columns = `Avg EV`:`Max EV`,decimals = 1) %>%
482
+ cols_width(Inning:Pitches ~ px(70),
483
+ everything() ~ px(85)) %>%
484
+ cols_align(align = "center") %>%
485
+ fmt_number(columns = AVG:SLG,decimals = 3) %>%
486
+ fmt_percent(columns = ends_with("%")) %>%
487
+ sub_missing() %>%
488
+ #gt_theme_savant() %>%
489
+ opt_interactive(
490
+ use_compact_mode = TRUE,
491
+ use_pagination = FALSE
492
+ )
493
+ })
494
+ output$SideFilterP <- render_gt({
495
+ test1 <- test %>% filter(PitcherAndTeam == input$player) %>% group_by(BatterSide) %>%
496
+ filter(between(as.Date(Date),input$date[1],input$date[2])) %>%
497
+ summarise(
498
+ Pitches = n(),
499
+ #BBE = sum(BBE,na.rm = TRUE),
500
+ "Whiff%" = mean(Whiff[Swing == TRUE],na.rm = TRUE),
501
+ "SwStr%" = mean(Whiff,na.rm = TRUE),
502
+ "CSW%" = mean(CSW,na.rm = TRUE),
503
+ "Zone%" = mean(Zone,na.rm = TRUE),
504
+ "ZCon%" = mean(Contact[Zone == TRUE & Swing == TRUE],na.rm = TRUE),
505
+ "ZSwing%" = mean(Swing[Zone == TRUE],na.rm = TRUE),
506
+ "OCon%" = mean(Contact[Zone == FALSE & Swing == TRUE],na.rm = TRUE),
507
+ "OSwing%" = mean(Swing[Zone == FALSE],na.rm = TRUE),
508
+ "Avg EV" = mean(ExitSpeed[BBE == TRUE],na.rm = TRUE),
509
+ "EV90" = quantile(ExitSpeed[BBE == TRUE],.9,na.rm = TRUE),
510
+ "Max EV" = max(ExitSpeed[BBE == TRUE],na.rm = TRUE),
511
+ "Max EV" = ifelse(is.infinite(`Max EV`),NA,`Max EV`),
512
+ "GB%" = mean(GB[BBE == TRUE],na.rm = TRUE),
513
+ "LD%" = mean(LD[BBE == TRUE],na.rm = TRUE),
514
+ "FB%" = mean(FB[BBE == TRUE],na.rm = TRUE),
515
+ "AVG" = round(sum(Hit,na.rm = TRUE) / sum(AB,na.rm = TRUE),digits = 3),
516
+ "OBP" = round((sum(Hit,na.rm = TRUE) + sum(Walk,na.rm = TRUE) + sum(HBP,na.rm = TRUE))/
517
+ sum(PA,na.rm = TRUE),digits = 3),
518
+ "SLG" = (round(sum(Single,na.rm = TRUE) + sum(Double,na.rm = TRUE) * 2 +
519
+ sum(Triple,na.rm = TRUE) * 3 + sum(HR,na.rm = TRUE)))/sum(AB,na.rm = TRUE))%>%
520
+ ungroup()
521
+ test2 <- test %>% mutate(BatterSide = "Both") %>% filter(PitcherAndTeam == input$player) %>% group_by(BatterSide) %>%
522
+ filter(between(as.Date(Date),input$date[1],input$date[2])) %>%
523
+ summarise(
524
+ Pitches = n(),
525
+ #BBE = sum(BBE,na.rm = TRUE),
526
+ "Whiff%" = mean(Whiff[Swing == TRUE],na.rm = TRUE),
527
+ "SwStr%" = mean(Whiff,na.rm = TRUE),
528
+ "CSW%" = mean(CSW,na.rm = TRUE),
529
+ "Zone%" = mean(Zone,na.rm = TRUE),
530
+ "ZCon%" = mean(Contact[Zone == TRUE & Swing == TRUE],na.rm = TRUE),
531
+ "ZSwing%" = mean(Swing[Zone == TRUE],na.rm = TRUE),
532
+ "OCon%" = mean(Contact[Zone == FALSE & Swing == TRUE],na.rm = TRUE),
533
+ "OSwing%" = mean(Swing[Zone == FALSE],na.rm = TRUE),
534
+ "Avg EV" = mean(ExitSpeed[BBE == TRUE],na.rm = TRUE),
535
+ "EV90" = quantile(ExitSpeed[BBE == TRUE],.9,na.rm = TRUE),
536
+ "Max EV" = max(ExitSpeed[BBE == TRUE],na.rm = TRUE),
537
+ "Max EV" = ifelse(is.infinite(`Max EV`),NA,`Max EV`),
538
+ "GB%" = mean(GB[BBE == TRUE],na.rm = TRUE),
539
+ "LD%" = mean(LD[BBE == TRUE],na.rm = TRUE),
540
+ "FB%" = mean(FB[BBE == TRUE],na.rm = TRUE),
541
+ "AVG" = round(sum(Hit,na.rm = TRUE) / sum(AB,na.rm = TRUE),digits = 3),
542
+ "OBP" = round((sum(Hit,na.rm = TRUE) + sum(Walk,na.rm = TRUE) + sum(HBP,na.rm = TRUE))/
543
+ sum(PA,na.rm = TRUE),digits = 3),
544
+ "SLG" = (round(sum(Single,na.rm = TRUE) + sum(Double,na.rm = TRUE) * 2 +
545
+ sum(Triple,na.rm = TRUE) * 3 + sum(HR,na.rm = TRUE)))/sum(AB,na.rm = TRUE))%>%
546
+ ungroup()
547
+ test <- rbind(test1,test2)
548
+ test %>%
549
+ gt() %>%
550
+ gt_theme_tim() %>%
551
+ fmt_number(columns = `Avg EV`:`Max EV`,decimals = 1) %>%
552
+ cols_width(BatterSide:Pitches ~ px(70),
553
+ everything() ~ px(85)) %>%
554
+ cols_align(align = "center") %>%
555
+ cols_label(BatterSide = "Side") %>%
556
+ fmt_number(columns = AVG:SLG,decimals = 3) %>%
557
+ fmt_percent(columns = ends_with("%")) %>%
558
+ sub_missing() %>%
559
+ #gt_theme_savant() %>%
560
+ opt_interactive(
561
+ use_compact_mode = TRUE,
562
+ use_pagination = FALSE
563
+ )
564
+
565
+ })
566
+ output$slashlineP <- render_gt({
567
+ test <- test %>% filter(PitcherAndTeam == input$player) %>%
568
+ filter(between(as.Date(Date),input$date[1],input$date[2])) %>%
569
+ summarise(
570
+ Pitches = n(),
571
+ `1B` = sum(Single,na.rm = TRUE),
572
+ `2B` = sum(Double,na.rm = TRUE),
573
+ `3B` = sum(Triple,na.rm = TRUE),
574
+ HR = sum(HR,na.rm = TRUE),
575
+ SO = sum(Strikeout,na.rm = TRUE),
576
+ BB = sum(Walk,na.rm = TRUE),
577
+ Whiffs = sum(Whiff,na.rm = TRUE),
578
+ `Hard Hits` = sum(HardHit,na.rm = TRUE)
579
+ )%>%
580
+ ungroup()%>%
581
+ gt() %>%
582
+ gt_theme_tim() %>%
583
+ cols_align(align = "center") %>%
584
+ # gt_theme_savant() %>%
585
+ opt_interactive(
586
+ use_compact_mode = TRUE,
587
+ use_pagination = FALSE
588
+ )
589
+ })
590
 
591
+ output$CountFilterB <- render_gt({
592
+ test <- test %>% filter(BatterAndTeam == input$player) %>% group_by(Count) %>%
593
+ filter(between(as.Date(Date),input$date[1],input$date[2])) %>%
594
+ summarise(
595
+ Pitches = n(),
596
+ #BBE = sum(BBE,na.rm = TRUE),
597
+ "Whiff%" = mean(Whiff[Swing == TRUE],na.rm = TRUE),
598
+ "SwStr%" = mean(Whiff,na.rm = TRUE),
599
+ "CSW%" = mean(CSW,na.rm = TRUE),
600
+ "Zone%" = mean(Zone,na.rm = TRUE),
601
+ "ZCon%" = mean(Contact[Zone == TRUE & Swing == TRUE],na.rm = TRUE),
602
+ "ZSwing%" = mean(Swing[Zone == TRUE],na.rm = TRUE),
603
+ "OCon%" = mean(Contact[Zone == FALSE & Swing == TRUE],na.rm = TRUE),
604
+ "OSwing%" = mean(Swing[Zone == FALSE],na.rm = TRUE),
605
+ "Avg EV" = mean(ExitSpeed[BBE == TRUE],na.rm = TRUE),
606
+ "EV90" = quantile(ExitSpeed[BBE == TRUE],.9,na.rm = TRUE),
607
+ "Max EV" = max(ExitSpeed[BBE == TRUE],na.rm = TRUE),
608
+ "Max EV" = ifelse(is.infinite(`Max EV`),NA,`Max EV`),
609
+ "GB%" = mean(GB[BBE == TRUE],na.rm = TRUE),
610
+ "LD%" = mean(LD[BBE == TRUE],na.rm = TRUE),
611
+ "FB%" = mean(FB[BBE == TRUE],na.rm = TRUE),
612
+ "AVG" = round(sum(Hit,na.rm = TRUE) / sum(AB,na.rm = TRUE),digits = 3),
613
+ "OBP" = round((sum(Hit,na.rm = TRUE) + sum(Walk,na.rm = TRUE) + sum(HBP,na.rm = TRUE))/
614
+ sum(PA,na.rm = TRUE),digits = 3),
615
+ "SLG" = (round(sum(Single,na.rm = TRUE) + sum(Double,na.rm = TRUE) * 2 +
616
  sum(Triple,na.rm = TRUE) * 3 + sum(HR,na.rm = TRUE)))/sum(AB,na.rm = TRUE))%>%
617
+ ungroup()%>%
618
+ gt() %>%
619
+ gt_theme_tim() %>%
620
+ fmt_number(columns = `Avg EV`:`Max EV`,decimals = 1) %>%
621
+ cols_width(Count:Pitches ~ px(70),
622
+ everything() ~ px(85)) %>%
623
+ cols_align(align = "center") %>%
624
+ fmt_number(columns = AVG:SLG,decimals = 3) %>%
625
+ fmt_percent(columns = ends_with("%")) %>%
626
+ sub_missing() %>%
627
+ #gt_theme_savant() %>%
628
+ opt_interactive(
629
+ use_compact_mode = TRUE,
630
+ use_pagination = FALSE
631
+ )
632
+ })
633
+ output$PitchFilterB <- render_gt({
634
+ tes <- test %>%
635
+ filter(BatterAndTeam == input$player) %>%
636
+ filter(between(as.Date(Date),input$date[1],input$date[2])) %>%
637
+ group_by(AutoPitchType) %>%
638
+ summarise(
639
+ Pitches = n(),
640
+ # BBE = sum(BBE,na.rm = TRUE),
641
+ "Pitch%" = n() / nrow(.), # Added Pitch% calculation
642
+ "Whiff%" = mean(Whiff[Swing == TRUE],na.rm = TRUE),
643
+ "SwStr%" = mean(Whiff,na.rm = TRUE),
644
+ "CSW%" = mean(CSW, na.rm = TRUE),
645
+ "Zone%" = mean(Zone, na.rm = TRUE),
646
+ "ZCon%" = mean(Contact[Zone == TRUE & Swing == TRUE], na.rm = TRUE),
647
+ "ZSwing%" = mean(Swing[Zone == TRUE], na.rm = TRUE),
648
+ "OCon%" = mean(Contact[Zone == FALSE & Swing == TRUE], na.rm = TRUE),
649
+ "OSwing%" = mean(Swing[Zone == FALSE], na.rm = TRUE),
650
+ "Avg EV" = mean(ExitSpeed[BBE == TRUE], na.rm = TRUE),
651
+ "EV90" = quantile(ExitSpeed[BBE == TRUE], .9, na.rm = TRUE),
652
+ "Max EV" = max(ExitSpeed[BBE == TRUE], na.rm = TRUE),
653
+ "Max EV" = ifelse(is.infinite(`Max EV`), NA, `Max EV`),
654
+ "GB%" = mean(GB[BBE == TRUE], na.rm = TRUE),
655
+ "LD%" = mean(LD[BBE == TRUE], na.rm = TRUE),
656
+ "FB%" = mean(FB[BBE == TRUE], na.rm = TRUE),
657
+ "AVG" = round(sum(Hit, na.rm = TRUE) / sum(AB, na.rm = TRUE), digits = 3),
658
+ "OBP" = round((sum(Hit, na.rm = TRUE) + sum(Walk, na.rm = TRUE) + sum(HBP, na.rm = TRUE)) /
659
+ sum(PA, na.rm = TRUE), digits = 3),
660
+ "SLG" = (round(sum(Single, na.rm = TRUE) + sum(Double, na.rm = TRUE) * 2 +
661
+ sum(Triple, na.rm = TRUE) * 3 + sum(HR, na.rm = TRUE))) / sum(AB, na.rm = TRUE)
662
+ ) %>%
663
+ ungroup() %>%
664
+ arrange(desc(Pitches)) %>%
665
+ gt() %>%
666
+ gt_theme_tim() %>%
667
+ fmt_number(columns = `Avg EV`:`Max EV`, decimals = 1) %>%
668
+ cols_label(AutoPitchType = "Type") %>%
669
+ cols_width(
670
+ AutoPitchType ~ px(100),
671
+ Pitches ~ px(70),
672
+ everything() ~ px(85)
673
+ ) %>%
674
+ cols_align(align = "center") %>%
675
+ fmt_number(columns = AVG:SLG, decimals = 3) %>%
676
+ fmt_percent(columns = ends_with("%")) %>%
677
+ sub_missing() %>%
678
+ opt_interactive(
679
+ use_compact_mode = TRUE,
680
+ use_pagination = FALSE
681
+ )
682
+ })
683
+ output$PitchSideFilterB <- render_gt({
684
+ test <- test %>%
685
+ filter(BatterAndTeam == input$player) %>%
686
+ filter(between(as.Date(Date),input$date[1],input$date[2])) %>%
687
+ group_by(PitcherThrows) %>%
688
+ mutate(total_side = n()) %>%
689
+ ungroup() %>%
690
+ group_by(AutoPitchType, PitcherThrows) %>%
691
+ summarise(
692
+ Pitches = n(),
693
+ #BBE = sum(BBE,na.rm = TRUE),
694
+ "Pitch%" = (n() / first(total_side)),
695
+ "Whiff%" = mean(Whiff[Swing == TRUE],na.rm = TRUE),
696
+ "SwStr%" = mean(Whiff,na.rm = TRUE),
697
+ "CSW%" = mean(CSW, na.rm = TRUE),
698
+ "Zone%" = mean(Zone, na.rm = TRUE),
699
+ "ZCon%" = mean(Contact[Zone == TRUE & Swing == TRUE], na.rm = TRUE),
700
+ "ZSwing%" = mean(Swing[Zone == TRUE], na.rm = TRUE),
701
+ "OCon%" = mean(Contact[Zone == FALSE & Swing == TRUE], na.rm = TRUE),
702
+ "OSwing%" = mean(Swing[Zone == FALSE], na.rm = TRUE),
703
+ "Avg EV" = mean(ExitSpeed[BBE == TRUE], na.rm = TRUE),
704
+ "EV90" = quantile(ExitSpeed[BBE == TRUE], .9, na.rm = TRUE),
705
+ "Max EV" = max(ExitSpeed[BBE == TRUE], na.rm = TRUE),
706
+ "Max EV" = ifelse(is.infinite(`Max EV`), NA, `Max EV`),
707
+ "GB%" = mean(GB[BBE == TRUE], na.rm = TRUE),
708
+ "LD%" = mean(LD[BBE == TRUE], na.rm = TRUE),
709
+ "FB%" = mean(FB[BBE == TRUE], na.rm = TRUE),
710
+ "AVG" = round(sum(Hit, na.rm = TRUE) / sum(AB, na.rm = TRUE), digits = 3),
711
+ "OBP" = round((sum(Hit, na.rm = TRUE) + sum(Walk, na.rm = TRUE) + sum(HBP, na.rm = TRUE)) /
712
+ sum(PA, na.rm = TRUE), digits = 3),
713
+ "SLG" = (round(sum(Single, na.rm = TRUE) + sum(Double, na.rm = TRUE) * 2 +
714
+ sum(Triple, na.rm = TRUE) * 3 + sum(HR, na.rm = TRUE))) / sum(AB, na.rm = TRUE)
715
+ ) %>%
716
+ ungroup() %>%
717
+ arrange(desc(PitcherThrows),desc(Pitches)) %>%
718
+ gt() %>%
719
+ gt_theme_tim() %>%
720
+ fmt_number(columns = `Avg EV`:`Max EV`, decimals = 1) %>%
721
+ cols_label(AutoPitchType = "Type", PitcherThrows = "Hand") %>%
722
+ cols_width(
723
+ AutoPitchType ~ px(100),
724
+ Pitches ~ px(70),
725
+ PitcherThrows ~ px(70),
726
+ everything() ~ px(85)
727
+ ) %>%
728
+ cols_align(align = "center") %>%
729
+ fmt_number(columns = AVG:SLG, decimals = 3) %>%
730
+ fmt_percent(columns = ends_with("%")) %>%
731
+ sub_missing() %>%
732
+ opt_interactive(
733
+ use_compact_mode = TRUE,
734
+ use_pagination = FALSE
735
+ )
736
+ })
737
+ output$InningFilterB <- render_gt({
738
+ test <- test %>% filter(BatterAndTeam == input$player) %>% group_by(Inning) %>%
739
+ filter(between(as.Date(Date),input$date[1],input$date[2])) %>%
740
+ summarise(
741
+ Pitches = n(),
742
+ #BBE = sum(BBE,na.rm = TRUE),
743
+ "Whiff%" = mean(Whiff[Swing == TRUE],na.rm = TRUE),
744
+ "SwStr%" = mean(Whiff,na.rm = TRUE),
745
+ "CSW%" = mean(CSW,na.rm = TRUE),
746
+ "Zone%" = mean(Zone,na.rm = TRUE),
747
+ "ZCon%" = mean(Contact[Zone == TRUE & Swing == TRUE],na.rm = TRUE),
748
+ "ZSwing%" = mean(Swing[Zone == TRUE],na.rm = TRUE),
749
+ "OCon%" = mean(Contact[Zone == FALSE & Swing == TRUE],na.rm = TRUE),
750
+ "OSwing%" = mean(Swing[Zone == FALSE],na.rm = TRUE),
751
+ "Avg EV" = mean(ExitSpeed[BBE == TRUE],na.rm = TRUE),
752
+ "EV90" = quantile(ExitSpeed[BBE == TRUE],.9,na.rm = TRUE),
753
+ "Max EV" = max(ExitSpeed[BBE == TRUE],na.rm = TRUE),
754
+ "Max EV" = ifelse(is.infinite(`Max EV`),NA,`Max EV`),
755
+ "GB%" = mean(GB[BBE == TRUE],na.rm = TRUE),
756
+ "LD%" = mean(LD[BBE == TRUE],na.rm = TRUE),
757
+ "FB%" = mean(FB[BBE == TRUE],na.rm = TRUE),
758
+ "AVG" = round(sum(Hit,na.rm = TRUE) / sum(AB,na.rm = TRUE),digits = 3),
759
+ "OBP" = round((sum(Hit,na.rm = TRUE) + sum(Walk,na.rm = TRUE) + sum(HBP,na.rm = TRUE))/
760
+ sum(PA,na.rm = TRUE),digits = 3),
761
+ "SLG" = (round(sum(Single,na.rm = TRUE) + sum(Double,na.rm = TRUE) * 2 +
762
+ sum(Triple,na.rm = TRUE) * 3 + sum(HR,na.rm = TRUE)))/sum(AB,na.rm = TRUE))%>%
763
+ ungroup()%>%
764
+ gt() %>%
765
+ gt_theme_tim() %>%
766
+ fmt_number(columns = `Avg EV`:`Max EV`,decimals = 1) %>%
767
+ cols_width(Inning:Pitches ~ px(70),
768
+ everything() ~ px(85)) %>%
769
+ cols_align(align = "center") %>%
770
+ fmt_number(columns = AVG:SLG,decimals = 3) %>%
771
+ fmt_percent(columns = ends_with("%")) %>%
772
+ sub_missing() %>%
773
+ #gt_theme_savant() %>%
774
+ opt_interactive(
775
+ use_compact_mode = TRUE,
776
+ use_pagination = FALSE
777
+ )
778
+ })
779
+ output$SideFilterB <- render_gt({
780
+ test1 <- test %>% filter(BatterAndTeam == input$player) %>% group_by(PitcherThrows) %>%
781
+ filter(between(as.Date(Date),input$date[1],input$date[2])) %>%
782
+ summarise(
783
+ Pitches = n(),
784
+ #BBE = sum(BBE,na.rm = TRUE),
785
+ "Whiff%" = mean(Whiff[Swing == TRUE],na.rm = TRUE),
786
+ "SwStr%" = mean(Whiff,na.rm = TRUE),
787
+ "CSW%" = mean(CSW,na.rm = TRUE),
788
+ "Zone%" = mean(Zone,na.rm = TRUE),
789
+ "ZCon%" = mean(Contact[Zone == TRUE & Swing == TRUE],na.rm = TRUE),
790
+ "ZSwing%" = mean(Swing[Zone == TRUE],na.rm = TRUE),
791
+ "OCon%" = mean(Contact[Zone == FALSE & Swing == TRUE],na.rm = TRUE),
792
+ "OSwing%" = mean(Swing[Zone == FALSE],na.rm = TRUE),
793
+ "Avg EV" = mean(ExitSpeed[BBE == TRUE],na.rm = TRUE),
794
+ "EV90" = quantile(ExitSpeed[BBE == TRUE],.9,na.rm = TRUE),
795
+ "Max EV" = max(ExitSpeed[BBE == TRUE],na.rm = TRUE),
796
+ "Max EV" = ifelse(is.infinite(`Max EV`),NA,`Max EV`),
797
+ "GB%" = mean(GB[BBE == TRUE],na.rm = TRUE),
798
+ "LD%" = mean(LD[BBE == TRUE],na.rm = TRUE),
799
+ "FB%" = mean(FB[BBE == TRUE],na.rm = TRUE),
800
+ "AVG" = round(sum(Hit,na.rm = TRUE) / sum(AB,na.rm = TRUE),digits = 3),
801
+ "OBP" = round((sum(Hit,na.rm = TRUE) + sum(Walk,na.rm = TRUE) + sum(HBP,na.rm = TRUE))/
802
+ sum(PA,na.rm = TRUE),digits = 3),
803
+ "SLG" = (round(sum(Single,na.rm = TRUE) + sum(Double,na.rm = TRUE) * 2 +
804
+ sum(Triple,na.rm = TRUE) * 3 + sum(HR,na.rm = TRUE)))/sum(AB,na.rm = TRUE))%>%
805
+ ungroup()
806
+ test2 <- test %>% mutate(PitcherThrows = "Both") %>% filter(BatterAndTeam == input$player) %>% group_by(PitcherThrows) %>%
807
+ filter(between(as.Date(Date),input$date[1],input$date[2])) %>%
808
+ summarise(
809
+ Pitches = n(),
810
+ #BBE = sum(BBE,na.rm = TRUE),
811
+ "Whiff%" = mean(Whiff[Swing == TRUE],na.rm = TRUE),
812
+ "SwStr%" = mean(Whiff,na.rm = TRUE),
813
+ "CSW%" = mean(CSW,na.rm = TRUE),
814
+ "Zone%" = mean(Zone,na.rm = TRUE),
815
+ "ZCon%" = mean(Contact[Zone == TRUE & Swing == TRUE],na.rm = TRUE),
816
+ "ZSwing%" = mean(Swing[Zone == TRUE],na.rm = TRUE),
817
+ "OCon%" = mean(Contact[Zone == FALSE & Swing == TRUE],na.rm = TRUE),
818
+ "OSwing%" = mean(Swing[Zone == FALSE],na.rm = TRUE),
819
+ "Avg EV" = mean(ExitSpeed[BBE == TRUE],na.rm = TRUE),
820
+ "EV90" = quantile(ExitSpeed[BBE == TRUE],.9,na.rm = TRUE),
821
+ "Max EV" = max(ExitSpeed[BBE == TRUE],na.rm = TRUE),
822
+ "Max EV" = ifelse(is.infinite(`Max EV`),NA,`Max EV`),
823
+ "GB%" = mean(GB[BBE == TRUE],na.rm = TRUE),
824
+ "LD%" = mean(LD[BBE == TRUE],na.rm = TRUE),
825
+ "FB%" = mean(FB[BBE == TRUE],na.rm = TRUE),
826
+ "AVG" = round(sum(Hit,na.rm = TRUE) / sum(AB,na.rm = TRUE),digits = 3),
827
+ "OBP" = round((sum(Hit,na.rm = TRUE) + sum(Walk,na.rm = TRUE) + sum(HBP,na.rm = TRUE))/
828
+ sum(PA,na.rm = TRUE),digits = 3),
829
+ "SLG" = (round(sum(Single,na.rm = TRUE) + sum(Double,na.rm = TRUE) * 2 +
830
+ sum(Triple,na.rm = TRUE) * 3 + sum(HR,na.rm = TRUE)))/sum(AB,na.rm = TRUE))%>%
831
+ ungroup()
832
+ test <- rbind(test1,test2)
833
+ test %>%
834
+ gt() %>%
835
+ gt_theme_tim() %>%
836
+ fmt_number(columns = `Avg EV`:`Max EV`,decimals = 1) %>%
837
+ cols_width(PitcherThrows:Pitches ~ px(70),
838
+ everything() ~ px(85)) %>%
839
+ cols_align(align = "center") %>%
840
+ cols_label(PitcherThrows = "Hand") %>%
841
+ fmt_number(columns = AVG:SLG,decimals = 3) %>%
842
+ fmt_percent(columns = ends_with("%")) %>%
843
+ sub_missing() %>%
844
+ #gt_theme_savant() %>%
845
+ opt_interactive(
846
+ use_compact_mode = TRUE,
847
+ use_pagination = FALSE
848
+ )
849
+
850
+ })
851
+ output$slashlineB <- render_gt({
852
+ test <- test %>% filter(BatterAndTeam == input$player) %>%
853
+ filter(between(as.Date(Date),input$date[1],input$date[2])) %>%
854
+ summarise(
855
+ Pitches = n(),
856
+ `1B` = sum(Single,na.rm = TRUE),
857
+ `2B` = sum(Double,na.rm = TRUE),
858
+ `3B` = sum(Triple,na.rm = TRUE),
859
+ HR = sum(HR,na.rm = TRUE),
860
+ SO = sum(Strikeout,na.rm = TRUE),
861
+ BB = sum(Walk,na.rm = TRUE),
862
+ Whiffs = sum(Whiff,na.rm = TRUE),
863
+ `Hard Hits` = sum(HardHit,na.rm = TRUE)
864
+ )%>%
865
+ ungroup()%>%
866
+ gt() %>%
867
+ gt_theme_tim() %>%
868
+ cols_align(align = "center") %>%
869
+ # gt_theme_savant() %>%
870
+ opt_interactive(
871
+ use_compact_mode = TRUE,
872
+ use_pagination = FALSE
873
+ )
874
+ })
875
+
876
  }
877
 
878
  # Run the application