DaveM2430 commited on
Commit
c0c71d6
Β·
verified Β·
1 Parent(s): 125b3a3

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +34 -46
app.R CHANGED
@@ -3,20 +3,7 @@ library(dplyr)
3
  library(arrow)
4
  library(DT)
5
 
6
- # ── Load data ──────────────────────────────────────────────────────────────────
7
- load_data <- function(level) {
8
- path <- paste0("/app/", gsub("[^A-Za-z0-9]","_",level), ".parquet")
9
- arrow::read_parquet(path)
10
- }
11
-
12
- build_pitcher_stats <- function(df, min_pitches) {
13
-
14
- format_name <- function(name) {
15
- parts <- strsplit(name, ", ")[[1]]
16
- if (length(parts) == 2) paste(trimws(parts[2]), trimws(parts[1])) else name
17
- }
18
-
19
- df %>%
20
  mutate(
21
  IsStrike = PitchCall %in% c("StrikeCalled","StrikeSwinging","FoulBall",
22
  "FoulBallNotFieldable","FoulTip","InPlay"),
@@ -34,60 +21,61 @@ build_pitcher_stats <- function(df, min_pitches) {
34
  IsPA = (!is.na(KorBB) & KorBB %in% c("Strikeout","Walk")) |
35
  (!is.na(PitchCall) & PitchCall %in% c("InPlay","HitByPitch"))
36
  ) %>%
37
- group_by(Pitcher, Level) %>%
38
  summarise(
39
- Pitches = n(),
40
- K = sum(IsK, na.rm=TRUE),
41
- BB = sum(IsBB, na.rm=TRUE),
42
- PA = sum(IsPA, na.rm=TRUE),
43
- zone_n = sum(IsZone, na.rm=TRUE),
44
- strike_n = sum(IsStrike, na.rm=TRUE),
45
- whiff_n = sum(IsWhiff, na.rm=TRUE),
46
- swing_n = sum(IsSwing, na.rm=TRUE),
47
- chase_n = sum(IsChase, na.rm=TRUE),
48
- outzone_n = sum(IsOutZone, na.rm=TRUE),
49
- hh_n = sum(IsHardHit, na.rm=TRUE),
50
- bip_n = sum(!is.na(ExitSpeed) &
51
- !PitchCall %in% c("FoulBall","FoulBallNotFieldable",
52
- "FoulBallFieldable","FoulTip"), na.rm=TRUE),
53
- fb_velo_sum = sum(ifelse(!is.na(RelSpeed) &
54
  TaggedPitchType %in% c("Fastball","FourSeamFastBall",
55
- "Four-Seam","FourSeam",
56
- "Sinker","TwoSeamFastBall",
57
- "OneSeamFastball","Cutter"),
58
  RelSpeed, NA), na.rm=TRUE),
59
- fb_velo_n = sum(!is.na(RelSpeed) &
60
  TaggedPitchType %in% c("Fastball","FourSeamFastBall",
61
- "Four-Seam","FourSeam",
62
- "Sinker","TwoSeamFastBall",
63
- "OneSeamFastball","Cutter"),
64
  na.rm=TRUE),
65
- .groups = "drop"
66
  ) %>%
 
 
67
  filter(Pitches >= min_pitches) %>%
68
  mutate(
69
  Name = sapply(Pitcher, format_name),
70
- Team = first(PitcherTeam),
71
  `Zone%` = round(zone_n / Pitches * 100, 1),
72
  `Strike%` = round(strike_n / Pitches * 100, 1),
73
  `K's` = K,
74
  `BB's` = BB,
75
  `K/BB` = case_when(
76
- K == 0 & BB == 0 ~ NA_real_,
77
- BB == 0 ~ NA_real_, # undefined β€” infinite
78
- K == 0 ~ 0,
79
- TRUE ~ round(K / BB, 2)
80
  ),
81
  `Whiff%` = round(whiff_n / pmax(swing_n, 1) * 100, 1),
82
  `Chase%` = round(chase_n / pmax(outzone_n, 1) * 100, 1),
83
- `FB Velo` = ifelse(fb_velo_n == 0, NA_real_, round(fb_velo_sum / fb_velo_n, 1)),
 
84
  `Hard Hit%` = round(hh_n / pmax(bip_n, 1) * 100, 1)
85
  ) %>%
86
- # filter out blank or incomplete names
87
  filter(
88
  !is.na(Name),
89
  trimws(Name) != "",
90
- # must have both first and last name (at least one space)
91
  grepl(" ", trimws(Name))
92
  ) %>%
93
  select(Name, Level, Team, Pitches, `FB Velo`, `Zone%`, `Strike%`,
 
3
  library(arrow)
4
  library(DT)
5
 
6
+ df %>%
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  mutate(
8
  IsStrike = PitchCall %in% c("StrikeCalled","StrikeSwinging","FoulBall",
9
  "FoulBallNotFieldable","FoulTip","InPlay"),
 
21
  IsPA = (!is.na(KorBB) & KorBB %in% c("Strikeout","Walk")) |
22
  (!is.na(PitchCall) & PitchCall %in% c("InPlay","HitByPitch"))
23
  ) %>%
24
+ group_by(Pitcher, Level, PitcherTeam) %>%
25
  summarise(
26
+ Pitches = n(),
27
+ K = sum(IsK, na.rm=TRUE),
28
+ BB = sum(IsBB, na.rm=TRUE),
29
+ PA = sum(IsPA, na.rm=TRUE),
30
+ zone_n = sum(IsZone, na.rm=TRUE),
31
+ strike_n = sum(IsStrike, na.rm=TRUE),
32
+ whiff_n = sum(IsWhiff, na.rm=TRUE),
33
+ swing_n = sum(IsSwing, na.rm=TRUE),
34
+ chase_n = sum(IsChase, na.rm=TRUE),
35
+ outzone_n = sum(IsOutZone, na.rm=TRUE),
36
+ hh_n = sum(IsHardHit, na.rm=TRUE),
37
+ bip_n = sum(!is.na(ExitSpeed) &
38
+ !PitchCall %in% c("FoulBall","FoulBallNotFieldable",
39
+ "FoulBallFieldable","FoulTip"), na.rm=TRUE),
40
+ fb_velo_sum = sum(ifelse(!is.na(RelSpeed) &
41
  TaggedPitchType %in% c("Fastball","FourSeamFastBall",
42
+ "Four-Seam","FourSeam",
43
+ "Sinker","TwoSeamFastBall",
44
+ "OneSeamFastball","Cutter"),
45
  RelSpeed, NA), na.rm=TRUE),
46
+ fb_velo_n = sum(!is.na(RelSpeed) &
47
  TaggedPitchType %in% c("Fastball","FourSeamFastBall",
48
+ "Four-Seam","FourSeam",
49
+ "Sinker","TwoSeamFastBall",
50
+ "OneSeamFastball","Cutter"),
51
  na.rm=TRUE),
52
+ .groups = "drop"
53
  ) %>%
54
+ ungroup() %>%
55
+ as.data.frame() %>%
56
  filter(Pitches >= min_pitches) %>%
57
  mutate(
58
  Name = sapply(Pitcher, format_name),
59
+ Team = PitcherTeam,
60
  `Zone%` = round(zone_n / Pitches * 100, 1),
61
  `Strike%` = round(strike_n / Pitches * 100, 1),
62
  `K's` = K,
63
  `BB's` = BB,
64
  `K/BB` = case_when(
65
+ K == 0 & BB == 0 ~ NA_real_,
66
+ BB == 0 ~ NA_real_,
67
+ K == 0 ~ 0,
68
+ TRUE ~ round(K / BB, 2)
69
  ),
70
  `Whiff%` = round(whiff_n / pmax(swing_n, 1) * 100, 1),
71
  `Chase%` = round(chase_n / pmax(outzone_n, 1) * 100, 1),
72
+ `FB Velo` = ifelse(fb_velo_n == 0, NA_real_,
73
+ round(fb_velo_sum / fb_velo_n, 1)),
74
  `Hard Hit%` = round(hh_n / pmax(bip_n, 1) * 100, 1)
75
  ) %>%
 
76
  filter(
77
  !is.na(Name),
78
  trimws(Name) != "",
 
79
  grepl(" ", trimws(Name))
80
  ) %>%
81
  select(Name, Level, Team, Pitches, `FB Velo`, `Zone%`, `Strike%`,