Spaces:
Running
Running
Update app.R
Browse files
app.R
CHANGED
|
@@ -780,58 +780,36 @@ server <- function(input, output, session) {
|
|
| 780 |
TRUE ~ TaggedPitchType
|
| 781 |
),
|
| 782 |
# Create necessary indicator variables if they don't exist
|
| 783 |
-
in_zone =
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
)
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
),
|
| 794 |
-
is_whiff = case_when(
|
| 795 |
-
"WhiffIndicator" %in% names(.) ~ WhiffIndicator,
|
| 796 |
-
!is.na(PitchCall) & PitchCall == "StrikeSwinging" ~ 1,
|
| 797 |
-
TRUE ~ 0
|
| 798 |
-
),
|
| 799 |
-
chase = case_when(
|
| 800 |
-
"Chaseindicator" %in% names(.) ~ Chaseindicator,
|
| 801 |
-
!is.na(PitchCall) & !is.na(PlateLocSide) & !is.na(PlateLocHeight) &
|
| 802 |
-
PitchCall %in% c("StrikeSwinging", "FoulBallNotFieldable", "FoulBall", "InPlay") &
|
| 803 |
-
(PlateLocSide < -0.95 | PlateLocSide > 0.95 | PlateLocHeight < 1.6 | PlateLocHeight > 3.5) ~ 1,
|
| 804 |
-
TRUE ~ 0
|
| 805 |
-
)
|
| 806 |
)
|
| 807 |
|
| 808 |
# Calculate total pitches for usage percentage
|
| 809 |
total_pitches <- nrow(movement_stats)
|
| 810 |
|
| 811 |
-
# Check if required columns exist
|
| 812 |
-
has_extension <- "Extension" %in% names(movement_stats)
|
| 813 |
-
has_spinrate <- "SpinRate" %in% names(movement_stats)
|
| 814 |
-
has_relheight <- "RelHeight" %in% names(movement_stats)
|
| 815 |
-
|
| 816 |
summary_stats <- movement_stats %>%
|
| 817 |
group_by(`Pitch Type` = pitch_group) %>%
|
| 818 |
summarise(
|
| 819 |
Count = n(),
|
| 820 |
`Usage%` = sprintf("%.1f%%", (n() / total_pitches) * 100),
|
| 821 |
-
`Ext.` = if (
|
| 822 |
`Avg Velo` = sprintf("%.1f mph", mean(RelSpeed, na.rm = TRUE)),
|
| 823 |
`90th Velo` = sprintf("%.1f mph", quantile(RelSpeed, 0.9, na.rm = TRUE)),
|
| 824 |
`Max Velo` = sprintf("%.1f mph", max(RelSpeed, na.rm = TRUE)),
|
| 825 |
`Avg IVB` = sprintf("%.1f in", mean(InducedVertBreak, na.rm = TRUE)),
|
| 826 |
`Avg HB` = sprintf("%.1f in", mean(HorzBreak, na.rm = TRUE)),
|
| 827 |
-
`Avg Spin` = if (
|
| 828 |
-
`Rel Height` = if (
|
| 829 |
`Zone%` = sprintf("%.1f%%", round(mean(in_zone, na.rm = TRUE) * 100, 1)),
|
| 830 |
-
`Whiff%` =
|
| 831 |
-
sprintf("%.1f%%", round(sum(is_whiff, na.rm = TRUE) / sum(is_swing, na.rm = TRUE) * 100, 1))
|
| 832 |
-
} else {
|
| 833 |
-
"β"
|
| 834 |
-
},
|
| 835 |
`Chase%` = sprintf("%.1f%%", round(mean(chase, na.rm = TRUE) * 100, 1)),
|
| 836 |
.groups = "drop"
|
| 837 |
) %>%
|
|
@@ -851,9 +829,6 @@ server <- function(input, output, session) {
|
|
| 851 |
|
| 852 |
info_text <- paste(
|
| 853 |
paste("Pitcher:", pitch_info$pitcher),
|
| 854 |
-
if ("PitchNo" %in% names(pitch_data) && !is.na(pitch_data$PitchNo)) {
|
| 855 |
-
paste("Pitch Number:", pitch_data$PitchNo)
|
| 856 |
-
} else "",
|
| 857 |
paste("Current Type:", pitch_data$TaggedPitchType),
|
| 858 |
paste("Velocity:", round(pitch_data$RelSpeed, 1), "mph"),
|
| 859 |
paste("Horizontal Break:", round(pitch_data$HorzBreak, 1), "inches"),
|
|
|
|
| 780 |
TRUE ~ TaggedPitchType
|
| 781 |
),
|
| 782 |
# Create necessary indicator variables if they don't exist
|
| 783 |
+
in_zone = if ("StrikeZoneIndicator" %in% names(.)) StrikeZoneIndicator else
|
| 784 |
+
ifelse(!is.na(PlateLocSide) & !is.na(PlateLocHeight) &
|
| 785 |
+
PlateLocSide >= -0.95 & PlateLocSide <= 0.95 &
|
| 786 |
+
PlateLocHeight >= 1.6 & PlateLocHeight <= 3.5, 1, 0),
|
| 787 |
+
is_whiff = if ("WhiffIndicator" %in% names(.)) WhiffIndicator else
|
| 788 |
+
ifelse(!is.na(PitchCall) & PitchCall == "StrikeSwinging", 1, 0),
|
| 789 |
+
chase = if ("Chaseindicator" %in% names(.)) Chaseindicator else
|
| 790 |
+
ifelse(!is.na(PitchCall) & !is.na(PlateLocSide) & !is.na(PlateLocHeight) &
|
| 791 |
+
PitchCall %in% c("StrikeSwinging", "FoulBallNotFieldable", "FoulBall", "InPlay") &
|
| 792 |
+
(PlateLocSide < -0.95 | PlateLocSide > 0.95 | PlateLocHeight < 1.6 | PlateLocHeight > 3.5), 1, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 793 |
)
|
| 794 |
|
| 795 |
# Calculate total pitches for usage percentage
|
| 796 |
total_pitches <- nrow(movement_stats)
|
| 797 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 798 |
summary_stats <- movement_stats %>%
|
| 799 |
group_by(`Pitch Type` = pitch_group) %>%
|
| 800 |
summarise(
|
| 801 |
Count = n(),
|
| 802 |
`Usage%` = sprintf("%.1f%%", (n() / total_pitches) * 100),
|
| 803 |
+
`Ext.` = if ("Extension" %in% names(.)) sprintf("%.1f", mean(Extension, na.rm = TRUE)) else "β",
|
| 804 |
`Avg Velo` = sprintf("%.1f mph", mean(RelSpeed, na.rm = TRUE)),
|
| 805 |
`90th Velo` = sprintf("%.1f mph", quantile(RelSpeed, 0.9, na.rm = TRUE)),
|
| 806 |
`Max Velo` = sprintf("%.1f mph", max(RelSpeed, na.rm = TRUE)),
|
| 807 |
`Avg IVB` = sprintf("%.1f in", mean(InducedVertBreak, na.rm = TRUE)),
|
| 808 |
`Avg HB` = sprintf("%.1f in", mean(HorzBreak, na.rm = TRUE)),
|
| 809 |
+
`Avg Spin` = if ("SpinRate" %in% names(.)) sprintf("%.0f rpm", mean(SpinRate, na.rm = TRUE)) else "β",
|
| 810 |
+
`Rel Height` = if ("RelHeight" %in% names(.)) sprintf("%.1f", mean(RelHeight, na.rm = TRUE)) else "β",
|
| 811 |
`Zone%` = sprintf("%.1f%%", round(mean(in_zone, na.rm = TRUE) * 100, 1)),
|
| 812 |
+
`Whiff%` = sprintf("%.1f%%", round(mean(is_whiff, na.rm = TRUE) * 100, 1)),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 813 |
`Chase%` = sprintf("%.1f%%", round(mean(chase, na.rm = TRUE) * 100, 1)),
|
| 814 |
.groups = "drop"
|
| 815 |
) %>%
|
|
|
|
| 829 |
|
| 830 |
info_text <- paste(
|
| 831 |
paste("Pitcher:", pitch_info$pitcher),
|
|
|
|
|
|
|
|
|
|
| 832 |
paste("Current Type:", pitch_data$TaggedPitchType),
|
| 833 |
paste("Velocity:", round(pitch_data$RelSpeed, 1), "mph"),
|
| 834 |
paste("Horizontal Break:", round(pitch_data$HorzBreak, 1), "inches"),
|