Upload 5 files
Browse files- Dockerfile +32 -4
- TimStuff2 (1).model +3 -0
- app.R +1048 -43
- left_batter.png +0 -0
- right_batter.png +0 -0
Dockerfile
CHANGED
|
@@ -1,14 +1,42 @@
|
|
| 1 |
-
FROM rocker/
|
| 2 |
|
| 3 |
WORKDIR /code
|
| 4 |
|
|
|
|
| 5 |
RUN install2.r --error \
|
|
|
|
| 6 |
shiny \
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
dplyr \
|
|
|
|
|
|
|
| 8 |
ggplot2 \
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
COPY . .
|
| 13 |
|
| 14 |
CMD ["R", "--quiet", "-e", "shiny::runApp(host='0.0.0.0', port=7860)"]
|
|
|
|
| 1 |
+
FROM rocker/shiny-verse:latest
|
| 2 |
|
| 3 |
WORKDIR /code
|
| 4 |
|
| 5 |
+
# Install stable packages from CRAN
|
| 6 |
RUN install2.r --error \
|
| 7 |
+
ggExtra \
|
| 8 |
shiny \
|
| 9 |
+
plotly \
|
| 10 |
+
bslib \
|
| 11 |
+
DT \
|
| 12 |
+
rsconnect \
|
| 13 |
+
baseballr \
|
| 14 |
dplyr \
|
| 15 |
+
tidyverse \
|
| 16 |
+
rvest \
|
| 17 |
ggplot2 \
|
| 18 |
+
janitor \
|
| 19 |
+
ggthemes \
|
| 20 |
+
ggpubr \
|
| 21 |
+
jsonlite \
|
| 22 |
+
utils \
|
| 23 |
+
grid \
|
| 24 |
+
gridExtra \
|
| 25 |
+
png \
|
| 26 |
+
xgboost \
|
| 27 |
+
httr \
|
| 28 |
+
jpeg \
|
| 29 |
+
zoo \
|
| 30 |
+
gtable \
|
| 31 |
+
lightgbm
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Install development packages from GitHub
|
| 35 |
+
RUN installGithub.r \
|
| 36 |
+
rstudio/bslib \
|
| 37 |
+
rstudio/httpuv \
|
| 38 |
+
rstudio/gridlayout
|
| 39 |
+
|
| 40 |
COPY . .
|
| 41 |
|
| 42 |
CMD ["R", "--quiet", "-e", "shiny::runApp(host='0.0.0.0', port=7860)"]
|
TimStuff2 (1).model
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1737b5f802e144b8e3e09ea35be21870bad9aec236a6ee4f8aaebfca641879c3
|
| 3 |
+
size 1708451
|
app.R
CHANGED
|
@@ -1,58 +1,1063 @@
|
|
| 1 |
library(shiny)
|
|
|
|
|
|
|
| 2 |
library(bslib)
|
|
|
|
|
|
|
|
|
|
| 3 |
library(dplyr)
|
|
|
|
|
|
|
| 4 |
library(ggplot2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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)
|
| 10 |
+
library(rvest)
|
| 11 |
library(ggplot2)
|
| 12 |
+
library(janitor)
|
| 13 |
+
library(ggthemes)
|
| 14 |
+
library(ggpubr)
|
| 15 |
+
library(jsonlite)
|
| 16 |
+
library(utils)
|
| 17 |
+
library(grid)
|
| 18 |
+
library(gridExtra)
|
| 19 |
+
library(png)
|
| 20 |
+
library(lightgbm)
|
| 21 |
+
library(httr)
|
| 22 |
+
library(jpeg)
|
| 23 |
+
library(zoo)
|
| 24 |
+
library(gtable)
|
| 25 |
|
| 26 |
+
options(shiny.maxRequestSize = 10000000 * 1024^2)
|
| 27 |
+
pdf(file = NULL)
|
| 28 |
+
Sys.setenv(TZ='EST')
|
| 29 |
+
|
| 30 |
+
download_private_csv <- function(repo_id, filename) {
|
| 31 |
+
url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/", filename)
|
| 32 |
+
|
| 33 |
+
response <- GET(url,
|
| 34 |
+
add_headers(Authorization = paste("Bearer", Sys.getenv("HF_TOKEN"))))
|
| 35 |
+
|
| 36 |
+
if (status_code(response) == 200) {
|
| 37 |
+
content <- content(response, "text")
|
| 38 |
+
con <- textConnection(content)
|
| 39 |
+
data <- read.csv(con)
|
| 40 |
+
close(con)
|
| 41 |
+
return(data)
|
| 42 |
+
} else {
|
| 43 |
+
stop("Failed to download dataset")
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
# Usage
|
| 48 |
+
t <- download_private_csv("TimStats/Passwords", "demo.csv")
|
| 49 |
+
pass <- t[1,1]
|
| 50 |
+
|
| 51 |
+
#model <- xgb.load('TimStuff2.model')
|
| 52 |
+
|
| 53 |
+
# Helper functions
|
| 54 |
+
download_and_process_image <- function(url) {
|
| 55 |
+
tryCatch({
|
| 56 |
+
response <- GET(url)
|
| 57 |
+
content_type <- http_type(response)
|
| 58 |
+
|
| 59 |
+
if (content_type %in% c("image/png", "image/jpeg")) {
|
| 60 |
+
temp_file <- tempfile(fileext = ifelse(content_type == "image/png", ".png", ".jpg"))
|
| 61 |
+
writeBin(content(response, "raw"), temp_file)
|
| 62 |
+
|
| 63 |
+
if (content_type == "image/png") {
|
| 64 |
+
img <- readPNG(temp_file)
|
| 65 |
+
} else {
|
| 66 |
+
img <- readJPEG(temp_file)
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
return(list(img = img, type = content_type))
|
| 70 |
+
} else {
|
| 71 |
+
warning(paste("Unsupported image type:", content_type))
|
| 72 |
+
return(NULL)
|
| 73 |
+
}
|
| 74 |
+
}, error = function(e) {
|
| 75 |
+
warning(paste("Error processing image:", e$message))
|
| 76 |
+
return(NULL)
|
| 77 |
+
})
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
is_barrel <- function(df) {
|
| 81 |
+
df$barrel <- with(df, ifelse(hit_angle <= 50 & hit_speed >= 97 & hit_speed * 1.5 -
|
| 82 |
+
hit_angle >= 117 & hit_speed + hit_angle >= 123, 1, 0))
|
| 83 |
+
return(df)
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
VAA <- function(milbtotal){
|
| 87 |
+
milbtotal <- milbtotal %>%
|
| 88 |
+
mutate(VAA = -atan((vz0+(az*(-sqrt((vy0*vy0)-(2*ay*(y0-(17/12))))-vy0)/
|
| 89 |
+
ay))/(-sqrt((vy0*vy0)-(2*ay*(y0-(17/12))))))*(180/pi))
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
pitcher_summary <- function(tmilb){
|
| 93 |
+
tmilb %>%
|
| 94 |
+
select(Batter,Pitcher,PlayResult,PitchCall,TaggedPitchType,RelSpeed,SpinRate,
|
| 95 |
+
Extension,PlateLocSide,PlateLocHeight,RelSide,RelHeight,ax0,ay0,az0,vx0,
|
| 96 |
+
vz0,vy0,pfxx,pfxz,InducedVertBreak,HorzBreak,SpinAxis,PitcherThrows,BatterSide,PitchofPA,VertApprAngle) %>%
|
| 97 |
+
rename(
|
| 98 |
+
"Batter Name" = Batter, "Pitcher Name" = Pitcher, result = PlayResult,
|
| 99 |
+
description = PitchCall, pitch_name = TaggedPitchType, start_speed = RelSpeed,
|
| 100 |
+
spin_rate = SpinRate, extension = Extension, px = PlateLocSide, pz = PlateLocHeight,
|
| 101 |
+
x0 = RelSide, z0 = RelHeight, ax = ax0, ay = ay0, az = az0, vx0 = vx0,
|
| 102 |
+
vz0 = vz0, vy0 = vy0, pfxX = pfxx, pfxZ = pfxz, IVB = InducedVertBreak,
|
| 103 |
+
HB = HorzBreak, spinDirection = SpinAxis, phand = PitcherThrows, bhand = BatterSide, PitchNum = PitchofPA, VAA = VertApprAngle
|
| 104 |
+
) %>%
|
| 105 |
+
mutate(
|
| 106 |
+
is_strike_swinging = ifelse(description == "StrikeSwinging", TRUE, FALSE),
|
| 107 |
+
phand = ifelse(phand == "Right", "R", "L"),
|
| 108 |
+
bhand = ifelse(bhand == "Right", "R", "L")
|
| 109 |
+
)
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
break_plot <- function(game){
|
| 113 |
+
game <- game %>% mutate(
|
| 114 |
+
pitch_name = case_when(
|
| 115 |
+
pitch_name %in% c("Fastball", "FourSeamFastBall") ~ "Four-Seam",
|
| 116 |
+
pitch_name %in% c("TwoSeamFastBall", "OneSeamFastBall", "Sinker") ~ "Sinker",
|
| 117 |
+
pitch_name %in% c("ChangeUp") ~ "Changeup",
|
| 118 |
+
pitch_name %in% c("KnuckleCurve") ~ "Knuckle Curve",
|
| 119 |
+
pitch_name %in% c("CutFastBall") ~ "Cutter",
|
| 120 |
+
TRUE ~ pitch_name
|
| 121 |
+
))
|
| 122 |
+
pitch_colors <- c(
|
| 123 |
+
"Four-Seam"= "#FF4136", "Sinker"= "#FF851B", "Cutter"= "#FFDC00",
|
| 124 |
+
"Changeup"= "#2ECC40", "Slider"= "#0074D9", "Sweeper"= "#ED68ED",
|
| 125 |
+
"Curveball"= "#B10DC9", "Splitter"= "#01FF70", "Knuckle Curve"= "#85144b",
|
| 126 |
+
"Slurve"= "#3D9970", "Knuckle Ball"= "#39CCCC", "Two-Seam Fastball"= "#F012BE",
|
| 127 |
+
"Eephus"= "#AAAAAA", "Fastball"= "#7FDBFF", "Slow Curve"= "#DDDDDD",
|
| 128 |
+
"Screwball"= "#FF69B4"
|
| 129 |
+
)
|
| 130 |
+
ggplot(game, aes(x = HB, y = IVB, color = pitch_name)) +
|
| 131 |
+
geom_point(size = 2) +
|
| 132 |
+
geom_vline(xintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
|
| 133 |
+
geom_hline(yintercept = 0, color = "lightblue", linewidth = 1, linetype = 4) +
|
| 134 |
+
scale_color_manual(values = pitch_colors) +
|
| 135 |
+
labs(x = "Horizontal Break (in)", y = "Induced Vertical Break (in)",
|
| 136 |
+
title = "Pitch Movement") +
|
| 137 |
+
xlim(-25, 25) + ylim(-25, 25) + theme_minimal() +
|
| 138 |
+
theme(
|
| 139 |
+
legend.position = "bottom",
|
| 140 |
+
plot.title = element_text(hjust = 0.5, face = "bold"),
|
| 141 |
+
panel.grid.minor = element_line(color = "gray", size = 0.25, linetype = 1),
|
| 142 |
+
aspect.ratio = 1
|
| 143 |
+
) +
|
| 144 |
+
guides(color = guide_legend(title = "Pitch Type", nrow = 1))
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
pitch_plot <- function(game, title) {
|
| 148 |
+
game <- game %>% mutate(
|
| 149 |
+
pitch_name = case_when(
|
| 150 |
+
pitch_name %in% c("Fastball", "FourSeamFastBall") ~ "Four-Seam",
|
| 151 |
+
pitch_name %in% c("TwoSeamFastBall", "OneSeamFastBall", "Sinker") ~ "Sinker",
|
| 152 |
+
pitch_name %in% c("ChangeUp") ~ "Changeup",
|
| 153 |
+
pitch_name %in% c("KnuckleCurve") ~ "Knuckle Curve",
|
| 154 |
+
pitch_name %in% c("CutFastBall") ~ "Cutter",
|
| 155 |
+
TRUE ~ pitch_name
|
| 156 |
+
))
|
| 157 |
+
pitch_colors <- c(
|
| 158 |
+
"Four-Seam"= "#FF4136", "Sinker"= "#FF851B", "Cutter"= "#FFDC00",
|
| 159 |
+
"Changeup"= "#2ECC40", "Slider"= "#0074D9", "Sweeper"= "#ED68ED",
|
| 160 |
+
"Curveball"= "#B10DC9", "Splitter"= "#01FF70", "Knuckle Curve"= "#85144b",
|
| 161 |
+
"Slurve"= "#3D9970", "Knuckle Ball"= "#39CCCC", "Two-Seam Fastball"= "#F012BE",
|
| 162 |
+
"Eephus"= "#AAAAAA", "Fastball"= "#7FDBFF", "Slow Curve"= "#DDDDDD",
|
| 163 |
+
"Screwball"= "#FF69B4"
|
| 164 |
+
)
|
| 165 |
+
ggplot(game, aes(x = px, y = pz, color = pitch_name)) +
|
| 166 |
+
geom_point(size = 3.5) +
|
| 167 |
+
geom_segment(aes(x = -0.71, xend = 0.71, y = 1.5, yend = 1.5)) +
|
| 168 |
+
geom_segment(aes(x = -0.71, xend = 0.71, y = 3.6, yend = 3.6)) +
|
| 169 |
+
geom_segment(aes(x = 0.71, xend = 0.71, y = 1.5, yend = 3.6)) +
|
| 170 |
+
geom_segment(aes(x = -0.71, xend = -0.71, y = 1.5, yend = 3.6)) +
|
| 171 |
+
scale_color_manual(values = pitch_colors) +
|
| 172 |
+
labs(x = NULL, y = NULL, title = title) +
|
| 173 |
+
xlim(-3, 3) + ylim(0.2, 4) + coord_fixed(ratio = 1) + theme_minimal() +
|
| 174 |
+
theme(
|
| 175 |
+
legend.position = "bottom",
|
| 176 |
+
plot.title = element_text(hjust = 0.5, face = "bold"),
|
| 177 |
+
axis.text = element_blank(),
|
| 178 |
+
axis.ticks = element_blank()
|
| 179 |
+
) +
|
| 180 |
+
guides(color = guide_legend(title = "Pitch Type", nrow = 1))
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
left_batter <- png::readPNG("left_batter.png")
|
| 184 |
+
right_batter <- png::readPNG("right_batter.png")
|
| 185 |
+
|
| 186 |
+
pitch_plot_split <- function(game, title) {
|
| 187 |
+
game_lhb <- game %>% filter(bhand == "L") %>% mutate(
|
| 188 |
+
pitch_name = case_when(
|
| 189 |
+
pitch_name %in% c("Fastball", "FourSeamFastBall") ~ "Four-Seam",
|
| 190 |
+
pitch_name %in% c("TwoSeamFastBall", "OneSeamFastBall", "Sinker") ~ "Sinker",
|
| 191 |
+
pitch_name %in% c("ChangeUp") ~ "Changeup",
|
| 192 |
+
pitch_name %in% c("KnuckleCurve") ~ "Knuckle Curve",
|
| 193 |
+
pitch_name %in% c("CutFastBall") ~ "Cutter",
|
| 194 |
+
TRUE ~ pitch_name
|
| 195 |
+
))
|
| 196 |
+
game_rhb <- game %>% filter(bhand == "R") %>% mutate(
|
| 197 |
+
pitch_name = case_when(
|
| 198 |
+
pitch_name %in% c("Fastball", "FourSeamFastBall") ~ "Four-Seam",
|
| 199 |
+
pitch_name %in% c("TwoSeamFastBall", "OneSeamFastBall", "Sinker") ~ "Sinker",
|
| 200 |
+
pitch_name %in% c("ChangeUp") ~ "Changeup",
|
| 201 |
+
pitch_name %in% c("KnuckleCurve") ~ "Knuckle Curve",
|
| 202 |
+
pitch_name %in% c("CutFastBall") ~ "Cutter",
|
| 203 |
+
TRUE ~ pitch_name
|
| 204 |
+
))
|
| 205 |
+
|
| 206 |
+
# Define home plate coordinates (adjusted position)
|
| 207 |
+
home_plate <- data.frame(
|
| 208 |
+
x = c(0.6, -0.6, -0.7083, 0, 0.7083), # Keep x-coordinates
|
| 209 |
+
y = c(0.5, 0.5, 0.25, 0, 0.25) # Adjust y-coordinates to put point at 0
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
pitch_colors <- c(
|
| 213 |
+
"Four-Seam"= "#FF4136", "Sinker"= "#FF851B", "Cutter"= "#FFDC00",
|
| 214 |
+
"Changeup"= "#2ECC40", "Slider"= "#0074D9", "Sweeper"= "#ED68ED",
|
| 215 |
+
"Curveball"= "#B10DC9", "Splitter"= "#01FF70", "Knuckle Curve"= "#85144b",
|
| 216 |
+
"Slurve"= "#3D9970", "Knuckle Ball"= "#39CCCC", "Two-Seam Fastball"= "#F012BE",
|
| 217 |
+
"Eephus"= "#AAAAAA", "Fastball"= "#7FDBFF", "Slow Curve"= "#DDDDDD",
|
| 218 |
+
"Screwball"= "#FF69B4"
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
# Create left-handed batter plot
|
| 222 |
+
plot_lhb <- ggplot(game_lhb, aes(x = -px, y = pz, color = pitch_name)) +
|
| 223 |
+
# Add home plate first (so it appears under the points)
|
| 224 |
+
geom_polygon(data = home_plate, aes(x = x, y = y),
|
| 225 |
+
fill = "white", color = "black", inherit.aes = FALSE) +
|
| 226 |
+
# Add batter image (now using left_batter on right side)
|
| 227 |
+
annotation_custom(rasterGrob(left_batter,
|
| 228 |
+
width = unit(.75, "npc"),
|
| 229 |
+
height = unit(1.5, "npc")),
|
| 230 |
+
xmin = .7, xmax = 3.2,
|
| 231 |
+
ymin = 1, ymax = 5) +
|
| 232 |
+
geom_point(size = 3) +
|
| 233 |
+
geom_segment(aes(x = -0.71, xend = 0.71, y = 1.5, yend = 1.5)) +
|
| 234 |
+
geom_segment(aes(x = -0.71, xend = 0.71, y = 3.6, yend = 3.6)) +
|
| 235 |
+
geom_segment(aes(x = 0.71, xend = 0.71, y = 1.5, yend = 3.6)) +
|
| 236 |
+
geom_segment(aes(x = -0.71, xend = -0.71, y = 1.5, yend = 3.6)) +
|
| 237 |
+
scale_color_manual(values = pitch_colors) +
|
| 238 |
+
labs(x = NULL, y = NULL, title = "LHB") +
|
| 239 |
+
xlim(-3, 3) +
|
| 240 |
+
ylim(0, 5) +
|
| 241 |
+
coord_fixed(ratio = 1) +
|
| 242 |
+
theme_void() +
|
| 243 |
+
theme(
|
| 244 |
+
legend.position = "none",
|
| 245 |
+
plot.title = element_text(hjust = 0.5, face = "bold"),
|
| 246 |
+
axis.text = element_blank(),
|
| 247 |
+
axis.ticks = element_blank()
|
| 248 |
+
)
|
| 249 |
+
|
| 250 |
+
# Create right-handed batter plot
|
| 251 |
+
plot_rhb <- ggplot(game_rhb, aes(x = -px, y = pz, color = pitch_name)) +
|
| 252 |
+
# Add home plate first
|
| 253 |
+
geom_polygon(data = home_plate, aes(x = x, y = y),
|
| 254 |
+
fill = "white", color = "black", inherit.aes = FALSE) +
|
| 255 |
+
# Add batter image (now using right_batter on left side)
|
| 256 |
+
annotation_custom(rasterGrob(right_batter,
|
| 257 |
+
width = unit(.75, "npc"),
|
| 258 |
+
height = unit(1.5, "npc")),
|
| 259 |
+
xmin = -3, xmax = -0.5,
|
| 260 |
+
ymin = 1, ymax = 5) +
|
| 261 |
+
geom_point(size = 3) +
|
| 262 |
+
geom_segment(aes(x = -0.71, xend = 0.71, y = 1.5, yend = 1.5)) +
|
| 263 |
+
geom_segment(aes(x = -0.71, xend = 0.71, y = 3.6, yend = 3.6)) +
|
| 264 |
+
geom_segment(aes(x = 0.71, xend = 0.71, y = 1.5, yend = 3.6)) +
|
| 265 |
+
geom_segment(aes(x = -0.71, xend = -0.71, y = 1.5, yend = 3.6)) +
|
| 266 |
+
scale_color_manual(values = pitch_colors) +
|
| 267 |
+
labs(x = NULL, y = NULL, title = "RHB") +
|
| 268 |
+
xlim(-3, 3) +
|
| 269 |
+
ylim(0, 5) +
|
| 270 |
+
coord_fixed(ratio = 1) +
|
| 271 |
+
theme_void() +
|
| 272 |
+
theme(
|
| 273 |
+
legend.position = "none",
|
| 274 |
+
plot.title = element_text(hjust = 0.5, face = "bold"),
|
| 275 |
+
axis.text = element_blank(),
|
| 276 |
+
axis.ticks = element_blank()
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
return(list(lhb = plot_lhb, rhb = plot_rhb))
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
calculate_VAA <- function(vz0, ay, az, vy0, y0) {
|
| 283 |
+
-atan((vz0+(az*(-sqrt((vy0*vy0)-(2*ay*(y0-(17/12))))-vy0)/
|
| 284 |
+
ay))/(-sqrt((vy0*vy0)-(2*ay*(y0-(17/12))))))*(180/pi)
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
calculate_EAA <- function(extension) {
|
| 288 |
+
extension / 6.3
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
calculate_SADiff <- function(pfxX, pfxZ, spinDirection) {
|
| 292 |
+
inSA <- atan2(pfxZ, pfxX) * 180/pi + 90
|
| 293 |
+
inSA <- ifelse(inSA < 0, inSA + 360, inSA)
|
| 294 |
+
SADiff <- spinDirection - inSA
|
| 295 |
+
SADiff <- ifelse(SADiff > 180, SADiff - 360, SADiff)
|
| 296 |
+
SADiff <- ifelse(SADiff < -180, SADiff + 360, SADiff)
|
| 297 |
+
return(SADiff)
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
scale_TimStuff <- function(raw_score, model_mean, model_sd) {
|
| 301 |
+
scaled_score <- (raw_score - model_mean) / model_sd
|
| 302 |
+
result <- 100 - (scaled_score * 10)
|
| 303 |
+
return(result)
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
transform_statcast_data <- function(df,
|
| 307 |
+
release_extension_col = "extension",
|
| 308 |
+
vx0_col = "vx0",
|
| 309 |
+
vy0_col = "vy0",
|
| 310 |
+
vz0_col = "vz0",
|
| 311 |
+
ax_col = "ax",
|
| 312 |
+
ay_col = "ay",
|
| 313 |
+
az_col = "az",
|
| 314 |
+
arm_angle_col = NULL,
|
| 315 |
+
p_throws_col = NULL) {
|
| 316 |
+
|
| 317 |
+
# Constants
|
| 318 |
+
MOUND_DISTANCE <- 60.5
|
| 319 |
+
STATCAST_INITIAL_MEASUREMENT <- 50.0
|
| 320 |
+
Z_CONSTANT <- 32.174
|
| 321 |
+
HOME_PLATE_HEIGHT <- 17/12 # Convert 17 inches to feet
|
| 322 |
+
print("etds")
|
| 323 |
+
# Compute release position
|
| 324 |
+
df$release_pos_y <- MOUND_DISTANCE - df[[release_extension_col]]
|
| 325 |
+
print("etds1")
|
| 326 |
+
# Compute release time
|
| 327 |
+
df$release_time <- (-df[[vy0_col]] -
|
| 328 |
+
sqrt((df[[vy0_col]]^2) -
|
| 329 |
+
(2 * df[[ay_col]] * (STATCAST_INITIAL_MEASUREMENT - df$release_pos_y)))) / df[[ay_col]]
|
| 330 |
+
print("etds23")
|
| 331 |
+
# Compute release velocities
|
| 332 |
+
df$vxR <- df[[vx0_col]] + (df[[ax_col]] * df$release_time)
|
| 333 |
+
df$vyR <- df[[vy0_col]] + (df[[ay_col]] * df$release_time)
|
| 334 |
+
df$vzR <- df[[vz0_col]] + (df[[az_col]] * df$release_time)
|
| 335 |
+
print("etds2")
|
| 336 |
+
# Compute time to home plate
|
| 337 |
+
df$tf <- (-df$vyR - sqrt(df$vyR^2 - 2 * df[[ay_col]] * (df$release_pos_y - HOME_PLATE_HEIGHT))) / df[[ay_col]]
|
| 338 |
+
print("etds3")
|
| 339 |
+
# Compute average velocities
|
| 340 |
+
df$vxbar <- (2 * df$vxR + df[[ax_col]] * df$tf) / 2
|
| 341 |
+
df$vybar <- (2 * df$vyR + df[[ay_col]] * df$tf) / 2
|
| 342 |
+
df$vzbar <- (2 * df$vzR + df[[az_col]] * df$tf) / 2
|
| 343 |
+
|
| 344 |
+
# Compute average velocity magnitude
|
| 345 |
+
df$vbar <- sqrt(df$vxbar^2 + df$vybar^2 + df$vzbar^2)
|
| 346 |
+
print("etds4")
|
| 347 |
+
# Compute drag acceleration
|
| 348 |
+
df$adrag <- -(df[[ax_col]] * df$vxbar +
|
| 349 |
+
df[[ay_col]] * df$vybar +
|
| 350 |
+
(df[[az_col]] + Z_CONSTANT) * df$vzbar) / df$vbar
|
| 351 |
+
|
| 352 |
+
# Compute Magnus force components
|
| 353 |
+
df$amagx <- df[[ax_col]] + df$adrag * df$vxbar / df$vbar
|
| 354 |
+
df$amagy <- df[[ay_col]] + df$adrag * df$vybar / df$vbar
|
| 355 |
+
df$amagz <- df[[az_col]] + df$adrag * df$vzbar / df$vbar + Z_CONSTANT
|
| 356 |
+
|
| 357 |
+
# Compute Magnus force magnitude
|
| 358 |
+
df$amag <- sqrt(df$amagx^2 + df$amagy^2 + df$amagz^2)
|
| 359 |
+
|
| 360 |
+
# Original transformation calculations
|
| 361 |
+
df$v_mag <- sqrt(df$vxR^2 + df$vyR^2 + df$vzR^2)
|
| 362 |
+
|
| 363 |
+
# Compute tangential unit vectors
|
| 364 |
+
df$tang_x <- df$vxR / df$v_mag
|
| 365 |
+
df$tang_y <- df$vyR / df$v_mag
|
| 366 |
+
df$tang_z <- df$vzR / df$v_mag
|
| 367 |
+
print("etds5")
|
| 368 |
+
# Handle arm angle and handedness
|
| 369 |
+
if (!is.null(arm_angle_col)) {
|
| 370 |
+
df$handedness_factor <- ifelse(df[[p_throws_col]] == "R", 1, -1)
|
| 371 |
+
df$temp_x <- cos(df[[arm_angle_col]]) * df$handedness_factor
|
| 372 |
+
df$temp_y <- 0
|
| 373 |
+
df$temp_z <- sin(df[[arm_angle_col]])
|
| 374 |
+
} else {
|
| 375 |
+
df$temp_x <- 0
|
| 376 |
+
df$temp_y <- rep(0, nrow(df))
|
| 377 |
+
df$temp_z <- 1
|
| 378 |
+
}
|
| 379 |
+
|
| 380 |
+
# Compute horizontal vector
|
| 381 |
+
df$horz_x <- df$temp_y * df$tang_z - df$temp_z * df$tang_y
|
| 382 |
+
df$horz_y <- df$temp_z * df$tang_x - df$temp_x * df$tang_z
|
| 383 |
+
df$horz_z <- df$temp_x * df$tang_y - df$temp_y * df$tang_x
|
| 384 |
+
print("etds6")
|
| 385 |
+
# Normalize horizontal vector
|
| 386 |
+
df$horz_mag <- sqrt(df$horz_x^2 + df$horz_y^2 + df$horz_z^2)
|
| 387 |
+
df$horz_x <- df$horz_x / df$horz_mag
|
| 388 |
+
df$horz_y <- df$horz_y / df$horz_mag
|
| 389 |
+
df$horz_z <- df$horz_z / df$horz_mag
|
| 390 |
+
|
| 391 |
+
# Compute vertical vector
|
| 392 |
+
df$vert_x <- df$tang_y * df$horz_z - df$tang_z * df$horz_y
|
| 393 |
+
df$vert_y <- df$tang_z * df$horz_x - df$tang_x * df$horz_z
|
| 394 |
+
df$vert_z <- df$tang_x * df$horz_y - df$tang_y * df$horz_x
|
| 395 |
+
print("etds7")
|
| 396 |
+
# Project accelerations
|
| 397 |
+
df$a_tang <- df[[ax_col]] * df$tang_x + df[[ay_col]] * df$tang_y + df[[az_col]] * df$tang_z
|
| 398 |
+
df$a_horz <- df[[ax_col]] * df$horz_x + df[[ay_col]] * df$horz_y + df[[az_col]] * df$horz_z
|
| 399 |
+
df$a_vert <- df[[ax_col]] * df$vert_x + df[[ay_col]] * df$vert_y + df[[az_col]] * df$vert_z
|
| 400 |
+
print("ets")
|
| 401 |
+
# Clean up intermediate columns
|
| 402 |
+
intermediate_cols <- c("temp_x", "temp_y", "temp_z", "handedness_factor",
|
| 403 |
+
"horz_mag", "horz_x", "horz_y", "horz_z",
|
| 404 |
+
"vert_x", "vert_y", "vert_z",
|
| 405 |
+
"tang_x", "tang_y", "tang_z")
|
| 406 |
+
df <- df[, !names(df) %in% intermediate_cols]
|
| 407 |
+
|
| 408 |
+
return(df)
|
| 409 |
+
}
|
| 410 |
+
|
| 411 |
+
calculatetimstuffall <- function(game) {
|
| 412 |
+
#game <- transform_statcast_data(game) %>% mutate(SADiff = calculate_SADiff(pfxx, pfxz, SpinAxis))
|
| 413 |
+
|
| 414 |
+
# Filter and handle fastball calculations with error checking
|
| 415 |
+
# Get primary pitch info directly within the function
|
| 416 |
+
primary_pitch1 <- game %>%
|
| 417 |
+
filter(pitch_name == "Sinker" | pitch_name == "Fastball" | pitch_name == "Cutter" |
|
| 418 |
+
pitch_name == "FourSeamFastBall" | pitch_name == "OneSeamFastBall" | pitch_name == "TwoSeamFastBall") %>%
|
| 419 |
+
group_by(`Pitcher Name`) %>%
|
| 420 |
+
count(pitch_name) %>%
|
| 421 |
+
summarise(max = max(n))
|
| 422 |
+
|
| 423 |
+
primary_pitch2 <- game %>%
|
| 424 |
+
filter(pitch_name == "Sinker" | pitch_name == "Fastball" | pitch_name == "Cutter" |
|
| 425 |
+
pitch_name == "FourSeamFastBall" | pitch_name == "OneSeamFastBall" | pitch_name == "TwoSeamFastBall") %>%
|
| 426 |
+
group_by(`Pitcher Name`, pitch_name) %>%
|
| 427 |
+
count(pitch_name)
|
| 428 |
+
|
| 429 |
+
primary_pitch <- left_join(primary_pitch1, primary_pitch2, by = c('Pitcher Name')) %>%
|
| 430 |
+
filter(max == n) %>%
|
| 431 |
+
select(`Pitcher Name`, pitch_name) %>%
|
| 432 |
+
filter(pitch_name != "Undefined" & pitch_name != "Other") %>%
|
| 433 |
+
mutate(Primary = pitch_name) %>%
|
| 434 |
+
select(-pitch_name)
|
| 435 |
+
|
| 436 |
+
game <- left_join(game, primary_pitch, by = c('Pitcher Name'))
|
| 437 |
+
|
| 438 |
+
# Calculate primary pitch averages
|
| 439 |
+
primary_avgs <- game %>%
|
| 440 |
+
filter(pitch_name == Primary) %>%
|
| 441 |
+
group_by(`Pitcher Name`) %>%
|
| 442 |
+
summarise(
|
| 443 |
+
Primary_Velo = mean(start_speed, na.rm = TRUE),
|
| 444 |
+
Primary_IVB = mean(IVB, na.rm = TRUE),
|
| 445 |
+
Primary_HB = mean(HB, na.rm = TRUE)
|
| 446 |
+
)
|
| 447 |
+
|
| 448 |
+
# Join averages and calculate differences
|
| 449 |
+
game <- game %>%
|
| 450 |
+
left_join(primary_avgs, by = c('Pitcher Name')) %>%
|
| 451 |
+
mutate(
|
| 452 |
+
velo_diff = round(ifelse(is.na(Primary_Velo), 0, start_speed - Primary_Velo), 1),
|
| 453 |
+
IVB_diff = round(ifelse(is.na(Primary_IVB), 0, IVB - Primary_IVB), 1),
|
| 454 |
+
HB_diff = round(ifelse(is.na(Primary_HB), 0,
|
| 455 |
+
ifelse(phand == "Left", -HB - Primary_HB, HB - Primary_HB)), 1)
|
| 456 |
+
)
|
| 457 |
+
print(colSums(is.na(game)))
|
| 458 |
+
feature_vars <- c("start_speed", "IVB", "HB", "spin_rate",
|
| 459 |
+
"z0", "x0", "extension", "velo_diff", "IVB_diff", "HB_diff")
|
| 460 |
+
complete_rows <- complete.cases(game[, feature_vars])
|
| 461 |
+
game_complete <- game[complete_rows, ]
|
| 462 |
+
game_na <- game[!complete_rows, ]
|
| 463 |
+
game_na <- if(any(!complete_rows)) {
|
| 464 |
+
na_rows <- game[!complete_rows, ]
|
| 465 |
+
na_rows$TimStuff <- NA
|
| 466 |
+
na_rows
|
| 467 |
+
} else {
|
| 468 |
+
# Create empty data frame with same structure plus TimStuff
|
| 469 |
+
empty_df <- game_complete[0, ] # Get structure from game_complete
|
| 470 |
+
empty_df$TimStuff <- numeric() # Add empty TimStuff column
|
| 471 |
+
empty_df
|
| 472 |
+
}
|
| 473 |
+
|
| 474 |
+
# Round the features to 1 decimal point
|
| 475 |
+
game_complete$start_speed <- round(game_complete$start_speed, 1)
|
| 476 |
+
game_complete$IVB <- round(game_complete$IVB, 1)
|
| 477 |
+
game_complete$HB <- abs(round(game_complete$HB, 1))
|
| 478 |
+
game_complete$spin_rate <- round(game_complete$spin_rate, 1)
|
| 479 |
+
game_complete$z0 <- round(game_complete$z0, 1)
|
| 480 |
+
game_complete$x0 <- abs(round(game_complete$x0, 1))
|
| 481 |
+
game_complete$extension <- round(game_complete$extension, 1)
|
| 482 |
+
game_complete$velo_diff <- round(game_complete$velo_diff, 1)
|
| 483 |
+
game_complete$IVB_diff <- round(game_complete$IVB_diff, 1)
|
| 484 |
+
game_complete$HB_diff <- round(game_complete$HB_diff, 1)
|
| 485 |
+
|
| 486 |
+
# Split right-handed and left-handed pitchers
|
| 487 |
+
rhp <- game_complete %>% filter(phand == "R")
|
| 488 |
+
lhp <- game_complete %>% filter(phand == "L")
|
| 489 |
+
|
| 490 |
+
# Process right-handed pitchers by pitch type
|
| 491 |
+
if(nrow(rhp) > 0) {
|
| 492 |
+
# Split by pitch type
|
| 493 |
+
rhp_ff <- rhp %>% filter(pitch_name %in% c("Fastball","FourSeamFastBall"))
|
| 494 |
+
rhp_si <- rhp %>% filter(pitch_name %in% c("OneSeamFastBall","Sinker","TwoSeamFastBall"))
|
| 495 |
+
rhp_ct <- rhp %>% filter(pitch_name == "Cutter")
|
| 496 |
+
rhp_sl <- rhp %>% filter(pitch_name %in% c("Slider","Sweeper"))
|
| 497 |
+
rhp_cb <- rhp %>% filter(pitch_name == "Curveball")
|
| 498 |
+
rhp_ch_spl <- rhp %>% filter(pitch_name %in% c("Changeup", "ChangeUp","Splitter","Knuckleball"))
|
| 499 |
+
|
| 500 |
+
# Apply models for each pitch type with appropriate scaling
|
| 501 |
+
if(nrow(rhp_ff) > 0) {
|
| 502 |
+
rhp_ff$TimStuff <- scale_TimStuff(
|
| 503 |
+
predict(FF_model, as.matrix(cbind(
|
| 504 |
+
rhp_ff$start_speed, rhp_ff$IVB, rhp_ff$HB,
|
| 505 |
+
rhp_ff$z0, rhp_ff$x0, rhp_ff$extension))),
|
| 506 |
+
0.07666808, 0.02507584)
|
| 507 |
+
}
|
| 508 |
+
|
| 509 |
+
if(nrow(rhp_si) > 0) {
|
| 510 |
+
rhp_si$TimStuff <- scale_TimStuff(
|
| 511 |
+
predict(SI_model, as.matrix(cbind(
|
| 512 |
+
rhp_si$start_speed, rhp_si$IVB, rhp_si$HB,
|
| 513 |
+
rhp_si$z0, rhp_si$x0, rhp_si$extension))),
|
| 514 |
+
0.09427338, 0.01524531)
|
| 515 |
+
}
|
| 516 |
+
|
| 517 |
+
if(nrow(rhp_ct) > 0) {
|
| 518 |
+
rhp_ct$TimStuff <- scale_TimStuff(
|
| 519 |
+
predict(CT_model, as.matrix(cbind(
|
| 520 |
+
rhp_ct$start_speed, rhp_ct$IVB, rhp_ct$HB,
|
| 521 |
+
rhp_ct$z0, rhp_ct$x0, rhp_ct$extension))),
|
| 522 |
+
0.01484248, 0.0001241943)
|
| 523 |
+
}
|
| 524 |
+
|
| 525 |
+
if(nrow(rhp_sl) > 0) {
|
| 526 |
+
rhp_sl$TimStuff <- scale_TimStuff(
|
| 527 |
+
predict(SL_model, as.matrix(cbind(
|
| 528 |
+
rhp_sl$start_speed, rhp_sl$IVB, rhp_sl$HB,
|
| 529 |
+
rhp_sl$spin_rate, rhp_sl$z0, rhp_sl$x0, rhp_sl$extension))),
|
| 530 |
+
0.06669426, 0.01006894)
|
| 531 |
+
}
|
| 532 |
+
|
| 533 |
+
if(nrow(rhp_cb) > 0) {
|
| 534 |
+
rhp_cb$TimStuff <- scale_TimStuff(
|
| 535 |
+
predict(CB_model, as.matrix(cbind(
|
| 536 |
+
rhp_cb$start_speed, rhp_cb$IVB, rhp_cb$HB,
|
| 537 |
+
rhp_cb$spin_rate, rhp_cb$z0, rhp_cb$x0, rhp_cb$extension))),
|
| 538 |
+
0.07480712, 0.01038754)
|
| 539 |
+
}
|
| 540 |
+
|
| 541 |
+
if(nrow(rhp_ch_spl) > 0) {
|
| 542 |
+
rhp_ch_spl$TimStuff <- scale_TimStuff(
|
| 543 |
+
predict(CH_SPL_model, as.matrix(cbind(
|
| 544 |
+
rhp_ch_spl$start_speed, rhp_ch_spl$IVB, rhp_ch_spl$HB,
|
| 545 |
+
rhp_ch_spl$z0, rhp_ch_spl$x0, rhp_ch_spl$extension,
|
| 546 |
+
rhp_ch_spl$velo_diff, rhp_ch_spl$IVB_diff, rhp_ch_spl$HB_diff))),
|
| 547 |
+
0.09255855, 0.01739746)
|
| 548 |
+
}
|
| 549 |
+
|
| 550 |
+
# Combine all RHP pitch types
|
| 551 |
+
rhp <- rbind(rhp_ff, rhp_si, rhp_ct, rhp_sl, rhp_cb, rhp_ch_spl)
|
| 552 |
+
print("Rhp")
|
| 553 |
+
print(rhp)
|
| 554 |
+
}
|
| 555 |
+
|
| 556 |
+
# Process left-handed pitchers by pitch type
|
| 557 |
+
if(nrow(lhp) > 0) {
|
| 558 |
+
# Split by pitch type
|
| 559 |
+
lhp_ff <- lhp %>% filter(pitch_name %in% c("Fastball","FourSeamFastBall"))
|
| 560 |
+
lhp_si <- lhp %>% filter(pitch_name %in% c("OneSeamFastBall","Sinker","TwoSeamFastBall"))
|
| 561 |
+
lhp_ct <- lhp %>% filter(pitch_name == "Cutter")
|
| 562 |
+
lhp_sl <- lhp %>% filter(pitch_name %in% c("Slider","Sweeper"))
|
| 563 |
+
lhp_cb <- lhp %>% filter(pitch_name == "Curveball")
|
| 564 |
+
lhp_ch_spl <- lhp %>% filter(pitch_name %in% c("Changeup", "ChangeUp","Splitter","Knuckleball"))
|
| 565 |
+
|
| 566 |
+
# Apply models for each pitch type with appropriate scaling
|
| 567 |
+
if(nrow(lhp_ff) > 0) {
|
| 568 |
+
lhp_ff$TimStuff <- scale_TimStuff(
|
| 569 |
+
predict(FF_model, as.matrix(cbind(
|
| 570 |
+
lhp_ff$start_speed, lhp_ff$IVB, lhp_ff$HB,
|
| 571 |
+
lhp_ff$z0, lhp_ff$x0, lhp_ff$extension))),
|
| 572 |
+
0.07666808, 0.02507584)
|
| 573 |
+
}
|
| 574 |
+
|
| 575 |
+
if(nrow(lhp_si) > 0) {
|
| 576 |
+
lhp_si$TimStuff <- scale_TimStuff(
|
| 577 |
+
predict(SI_model, as.matrix(cbind(
|
| 578 |
+
lhp_si$start_speed, lhp_si$IVB, lhp_si$HB,
|
| 579 |
+
lhp_si$z0, lhp_si$x0, lhp_si$extension))),
|
| 580 |
+
0.09427338, 0.01524531)
|
| 581 |
+
}
|
| 582 |
+
|
| 583 |
+
if(nrow(lhp_ct) > 0) {
|
| 584 |
+
lhp_ct$TimStuff <- scale_TimStuff(
|
| 585 |
+
predict(CT_model, as.matrix(cbind(
|
| 586 |
+
lhp_ct$start_speed, lhp_ct$IVB, lhp_ct$HB,
|
| 587 |
+
lhp_ct$z0, lhp_ct$x0, lhp_ct$extension))),
|
| 588 |
+
0.01484248, 0.0001241943)
|
| 589 |
+
}
|
| 590 |
+
|
| 591 |
+
if(nrow(lhp_sl) > 0) {
|
| 592 |
+
lhp_sl$TimStuff <- scale_TimStuff(
|
| 593 |
+
predict(SL_model, as.matrix(cbind(
|
| 594 |
+
lhp_sl$start_speed, lhp_sl$IVB, lhp_sl$HB,
|
| 595 |
+
lhp_sl$spin_rate, lhp_sl$z0, lhp_sl$x0, lhp_sl$extension))),
|
| 596 |
+
0.06669426, 0.01006894)
|
| 597 |
+
}
|
| 598 |
+
|
| 599 |
+
if(nrow(lhp_cb) > 0) {
|
| 600 |
+
lhp_cb$TimStuff <- scale_TimStuff(
|
| 601 |
+
predict(CB_model, as.matrix(cbind(
|
| 602 |
+
lhp_cb$start_speed, lhp_cb$IVB, lhp_cb$HB,
|
| 603 |
+
lhp_cb$spin_rate, lhp_cb$z0, lhp_cb$x0, lhp_cb$extension))),
|
| 604 |
+
0.07480712, 0.01038754)
|
| 605 |
+
}
|
| 606 |
+
|
| 607 |
+
if(nrow(lhp_ch_spl) > 0) {
|
| 608 |
+
lhp_ch_spl$TimStuff <- scale_TimStuff(
|
| 609 |
+
predict(CH_SPL_model, as.matrix(cbind(
|
| 610 |
+
lhp_ch_spl$start_speed, lhp_ch_spl$IVB, lhp_ch_spl$HB,
|
| 611 |
+
lhp_ch_spl$z0, lhp_ch_spl$x0, lhp_ch_spl$extension,
|
| 612 |
+
lhp_ch_spl$velo_diff, lhp_ch_spl$IVB_diff, lhp_ch_spl$HB_diff))),
|
| 613 |
+
0.09255855, 0.01739746)
|
| 614 |
+
}
|
| 615 |
+
|
| 616 |
+
# Combine all LHP pitch types
|
| 617 |
+
lhp <- rbind(lhp_ff, lhp_si, lhp_ct, lhp_sl, lhp_cb, lhp_ch_spl)
|
| 618 |
+
}
|
| 619 |
+
|
| 620 |
+
# Combine all processed data
|
| 621 |
+
game_complete <- rbind(rhp, lhp, game_na)
|
| 622 |
+
|
| 623 |
+
return(game_complete)
|
| 624 |
+
}
|
| 625 |
+
summary_table <- function(game) {
|
| 626 |
+
game <- calculatetimstuffall(game)
|
| 627 |
+
|
| 628 |
+
if("TimStuff" %in% colnames(game)) {
|
| 629 |
+
game <- game %>%
|
| 630 |
+
mutate(TimStuff = ifelse(TimStuff < 50, NA, TimStuff))
|
| 631 |
+
}
|
| 632 |
+
|
| 633 |
+
# Calculate main summary
|
| 634 |
+
main_summary <- game %>%
|
| 635 |
+
mutate(
|
| 636 |
+
team_fielding_id = ifelse(description %in% c("StrikeCalled", "StrikeSwinging"), 1, 0),
|
| 637 |
+
swing = ifelse(description %in% c("FoulBall", "StrikeSwinging", "InPlay","FoulBallNotFieldable"), 1, 0),
|
| 638 |
+
is_strike_swinging = as.integer(is_strike_swinging),
|
| 639 |
+
pitch_name = case_when(
|
| 640 |
+
pitch_name %in% c("Fastball", "FourSeamFastBall") ~ "Four-Seam",
|
| 641 |
+
pitch_name %in% c("TwoSeamFastBall", "OneSeamFastBall", "Sinker") ~ "Sinker",
|
| 642 |
+
pitch_name %in% c("ChangeUp") ~ "Changeup",
|
| 643 |
+
pitch_name %in% c("KnuckleCurve") ~ "Knuckle Curve",
|
| 644 |
+
pitch_name %in% c("CutFastBall") ~ "Cutter",
|
| 645 |
+
TRUE ~ pitch_name
|
| 646 |
+
),
|
| 647 |
+
Pitch = pitch_name,
|
| 648 |
+
FPS = case_when(description %in% c("StrikeCalled","StrikeSwinging","FoulBallNotFieldable","FoulBall","InPlay") ~ TRUE, TRUE ~ FALSE),
|
| 649 |
+
) %>%
|
| 650 |
+
group_by(Pitch) %>%
|
| 651 |
+
summarize(
|
| 652 |
+
Pitches = n(),
|
| 653 |
+
'Pitch%' = round(n() / nrow(game) * 100, 1),
|
| 654 |
+
"FP Strike%" = round(mean(FPS[PitchNum == 1], na.rm = TRUE) * 100, 1),
|
| 655 |
+
'Avg. Velo' = round(mean(start_speed, na.rm = TRUE), 1),
|
| 656 |
+
'Top Velo' = round(max(start_speed, na.rm = TRUE), 1), # Add Top Velo
|
| 657 |
+
'Spin Rate' = round(mean(spin_rate, na.rm = TRUE), 0),
|
| 658 |
+
'Extension' = round(mean(extension, na.rm = TRUE), 1),
|
| 659 |
+
'IVB' = round(mean(IVB, na.rm = TRUE), 1),
|
| 660 |
+
'HB' = round(mean(HB, na.rm = TRUE), 1),
|
| 661 |
+
'RelH' = round(mean(z0, na.rm = TRUE), digits = 1),
|
| 662 |
+
'RelS' = round(mean(x0, na.rm = TRUE), digits = 1),
|
| 663 |
+
'VAA' = round(mean(VAA, na.rm = TRUE), 1),
|
| 664 |
+
'CSW%' = round(sum(team_fielding_id, na.rm = TRUE) / sum(!is.na(team_fielding_id)) * 100, 1),
|
| 665 |
+
'Whiff%' = round(sum(is_strike_swinging, na.rm = TRUE) / sum(swing, na.rm = TRUE) * 100, 1),
|
| 666 |
+
'Stuff+' = if("TimStuff" %in% colnames(game)) round(mean(TimStuff, na.rm = TRUE), 0) else NA
|
| 667 |
+
) %>%
|
| 668 |
+
arrange(-Pitches)
|
| 669 |
+
|
| 670 |
+
# Calculate totals
|
| 671 |
+
total_row <- game %>%
|
| 672 |
+
mutate(
|
| 673 |
+
team_fielding_id = ifelse(description %in% c("StrikeCalled", "StrikeSwinging"), 1, 0),
|
| 674 |
+
swing = ifelse(description %in% c("FoulBall", "StrikeSwinging", "InPlay","FoulBallNotFieldable"), 1, 0),
|
| 675 |
+
is_strike_swinging = as.integer(is_strike_swinging),
|
| 676 |
+
FPS = case_when(description %in% c("StrikeCalled","StrikeSwinging","FoulBallNotFieldable","FoulBall") ~ TRUE, TRUE ~ FALSE)
|
| 677 |
+
) %>%
|
| 678 |
+
summarize(
|
| 679 |
+
Pitch = "Total",
|
| 680 |
+
Pitches = n(),
|
| 681 |
+
'Pitch%' = 100,
|
| 682 |
+
"FP Strike%" = round(mean(FPS[PitchNum == 1], na.rm = TRUE) * 100, 1),
|
| 683 |
+
'Avg. Velo' = NA_real_,
|
| 684 |
+
'Top Velo' = round(max(start_speed, na.rm = TRUE), 1), # Add overall top velo
|
| 685 |
+
'Spin Rate' = NA_real_,
|
| 686 |
+
'Extension' = round(mean(extension, na.rm = TRUE), 1),
|
| 687 |
+
'IVB' = NA_real_,
|
| 688 |
+
'HB' = NA_real_,
|
| 689 |
+
'RelH' = round(mean(z0, na.rm = TRUE), digits = 1),
|
| 690 |
+
'RelS' = round(mean(x0, na.rm = TRUE), digits = 1),
|
| 691 |
+
'VAA' = round(mean(VAA, na.rm = TRUE), 1),
|
| 692 |
+
'CSW%' = round(sum(team_fielding_id, na.rm = TRUE) / sum(!is.na(team_fielding_id)) * 100, 1),
|
| 693 |
+
'Whiff%' = round(sum(is_strike_swinging, na.rm = TRUE) / sum(swing, na.rm = TRUE) * 100, 1),
|
| 694 |
+
'Stuff+' = if("TimStuff" %in% colnames(game)) round(mean(TimStuff, na.rm = TRUE), 0) else NA
|
| 695 |
+
)
|
| 696 |
+
|
| 697 |
+
# Combine main summary with total row
|
| 698 |
+
combined_summary <- bind_rows(main_summary, total_row) %>%
|
| 699 |
+
rename(
|
| 700 |
+
"Type" = Pitch, "#" = Pitches, "Velo" = `Avg. Velo`,
|
| 701 |
+
"Top" = `Top Velo`, # Rename Top Velo to shorter column name
|
| 702 |
+
"Spin" = `Spin Rate`, "Ext" = Extension, "Usage%" = `Pitch%`
|
| 703 |
+
) %>%
|
| 704 |
+
mutate(
|
| 705 |
+
"Usage%" = paste0(`Usage%`, "%"),
|
| 706 |
+
"Spin" = ifelse(Type == "Total", "-", format(round(as.numeric(Spin)), big.mark = ",")),
|
| 707 |
+
"Velo" = ifelse(Type == "Total", "-", as.character(Velo)),
|
| 708 |
+
"IVB" = ifelse(Type == "Total", "-", as.character(IVB)),
|
| 709 |
+
"HB" = ifelse(Type == "Total", "-", as.character(HB)),
|
| 710 |
+
`CSW%` = paste0(`CSW%`, "%"),
|
| 711 |
+
`Whiff%` = paste0(`Whiff%`, "%"),
|
| 712 |
+
`FP Strike%` = paste0(`FP Strike%`, "%")
|
| 713 |
+
)
|
| 714 |
+
|
| 715 |
+
return(combined_summary)
|
| 716 |
+
}
|
| 717 |
+
|
| 718 |
+
FF_model <- readRDS("FF_model.rds")
|
| 719 |
+
SI_model <- readRDS("SI_model.rds")
|
| 720 |
+
CT_model <- readRDS("CT_model.rds")
|
| 721 |
+
SL_model <- readRDS("SL_model.rds")
|
| 722 |
+
CB_model <- readRDS("CB_model.rds")
|
| 723 |
+
CH_SPL_model <- readRDS("CH_SPL_model.rds")
|
| 724 |
+
|
| 725 |
+
team_data <- read.csv("teampic.csv") %>%
|
| 726 |
+
select(newestTeamName, newestTeamId) %>%
|
| 727 |
+
arrange(newestTeamName) # Sort alphabetically
|
| 728 |
+
|
| 729 |
+
# Modify the UI
|
| 730 |
+
ui <- fluidPage(
|
| 731 |
+
titlePanel("Trackman Summary Card Demo"),
|
| 732 |
+
sidebarLayout(
|
| 733 |
+
sidebarPanel(
|
| 734 |
+
width = 3,
|
| 735 |
+
fileInput("incsv", "Input File", accept = c(".csv"), multiple = FALSE),
|
| 736 |
+
actionButton("update", "Load Pitchers", icon("magnifying-glass"),
|
| 737 |
+
class = "btn-primary btn-block"),
|
| 738 |
+
selectizeInput("pitcher", "Pitcher Name:", NULL),
|
| 739 |
+
selectizeInput("teamName", "Team:",
|
| 740 |
+
choices = c("Default" = "default", setNames(team_data$newestTeamId, team_data$newestTeamName)),
|
| 741 |
+
selected = "default"),
|
| 742 |
+
numericInput("rollnum", "Rolling Pitch #", c("1")),
|
| 743 |
+
textInput("pass", "Password"),
|
| 744 |
+
actionButton("update1", "Make Card", icon("plus"),
|
| 745 |
+
class = "btn-success btn-block"),
|
| 746 |
+
downloadButton("downloadPlot", "Download Card", class = "btn-info btn-block")
|
| 747 |
),
|
| 748 |
+
mainPanel(
|
| 749 |
+
div(style = "width: 1000px; height: 1000px; overflow: auto;",
|
| 750 |
+
plotOutput("combinedPlot", width = "100%", height = "100%")
|
| 751 |
+
)
|
| 752 |
+
)
|
| 753 |
+
)
|
| 754 |
)
|
| 755 |
|
| 756 |
+
# Server Definition
|
| 757 |
server <- function(input, output, session) {
|
| 758 |
+
|
| 759 |
+
game_data <- reactiveVal()
|
| 760 |
+
|
| 761 |
+
observeEvent(input$update, {
|
| 762 |
+
req(input$incsv)
|
| 763 |
+
df <- read.csv(input$incsv$datapath)
|
| 764 |
+
pname <- pitcher_summary(df)
|
| 765 |
+
game_data(pname)
|
| 766 |
+
if(nrow(pname) == 0) {
|
| 767 |
+
showNotification("No pitchers found for the selected game.", type = "warning")
|
| 768 |
+
} else {
|
| 769 |
+
updateSelectizeInput(session, "pitcher", "Pitcher:", choices = unique(pname$`Pitcher Name`))
|
| 770 |
+
game_data(pname)
|
| 771 |
+
}
|
| 772 |
})
|
| 773 |
+
|
| 774 |
+
rolling_timstuff <- reactive({
|
| 775 |
+
req(input$update1, game_data())
|
| 776 |
+
game <- game_data() %>% filter(`Pitcher Name` == input$pitcher)# %>%
|
| 777 |
+
# mutate(
|
| 778 |
+
# pitch_name = case_when(
|
| 779 |
+
# pitch_name %in% c("Fastball", "FourSeamFastBall") ~ "Four-Seam",
|
| 780 |
+
# pitch_name %in% c("TwoSeamFastBall", "OneSeamFastBall", "Sinker") ~ "Sinker",
|
| 781 |
+
# pitch_name %in% c("ChangeUp") ~ "Changeup",
|
| 782 |
+
# pitch_name %in% c("KnuckleCurve") ~ "Knuckle Curve",
|
| 783 |
+
# pitch_name %in% c("CutFastBall") ~ "Cutter",
|
| 784 |
+
# TRUE ~ pitch_name
|
| 785 |
+
# ))
|
| 786 |
+
game <- calculatetimstuffall(game)
|
| 787 |
+
print("Test")
|
| 788 |
+
print(game)
|
| 789 |
+
print(nrow(game))
|
| 790 |
+
#names(game)
|
| 791 |
+
game <- game %>%
|
| 792 |
+
mutate(TimStuff = ifelse(TimStuff < 40, NA, TimStuff))
|
| 793 |
+
|
| 794 |
+
custom_rollmean <- function(x, k) {
|
| 795 |
+
n <- length(x)
|
| 796 |
+
result <- numeric(n)
|
| 797 |
+
|
| 798 |
+
for (i in 1:n) {
|
| 799 |
+
if (i < k) {
|
| 800 |
+
# For the first k-1 points, use a window size of 1 (i.e., the point itself)
|
| 801 |
+
result[i] <- x[i]
|
| 802 |
+
} else {
|
| 803 |
+
# For k and onwards, use the specified rolling window
|
| 804 |
+
window <- x[(i-k+1):i]
|
| 805 |
+
result[i] <- mean(window, na.rm = TRUE)
|
| 806 |
+
}
|
| 807 |
+
}
|
| 808 |
+
|
| 809 |
+
return(result)
|
| 810 |
+
}
|
| 811 |
+
|
| 812 |
+
result <- game %>%
|
| 813 |
+
group_by(pitch_name) %>%
|
| 814 |
+
mutate(
|
| 815 |
+
TimStuff = as.numeric(TimStuff),
|
| 816 |
+
rolling_timstuff = custom_rollmean(TimStuff, k = input$rollnum),
|
| 817 |
+
pitch_number = row_number()
|
| 818 |
+
) %>%
|
| 819 |
+
ungroup()
|
| 820 |
+
|
| 821 |
+
return(result)
|
| 822 |
+
})
|
| 823 |
+
|
| 824 |
+
combinedPlot <- reactiveVal()
|
| 825 |
+
|
| 826 |
+
observeEvent(input$update1,{
|
| 827 |
+
if(input$pass == pass){
|
| 828 |
+
req(game_data())
|
| 829 |
+
game <- game_data() %>% filter(`Pitcher Name` == input$pitcher)
|
| 830 |
+
|
| 831 |
+
if(nrow(game) == 0) {
|
| 832 |
+
showNotification("No data available for the selected pitcher.", type = "warning")
|
| 833 |
+
return()
|
| 834 |
+
}
|
| 835 |
+
get_logo <- function() {
|
| 836 |
+
if (input$teamName != "default") {
|
| 837 |
+
# Try to get team logo using the selected team ID
|
| 838 |
+
logo_url <- paste0("https://static.trumedianetworks.com/images/ncaabaseball/teams/", input$teamName, ".png")
|
| 839 |
+
img_result <- download_and_process_image(logo_url)
|
| 840 |
+
|
| 841 |
+
if (!is.null(img_result)) {
|
| 842 |
+
return(rasterGrob(img_result$img, interpolate = TRUE))
|
| 843 |
+
}
|
| 844 |
+
}
|
| 845 |
+
|
| 846 |
+
# Fallback to default logo
|
| 847 |
+
default_url <- "https://pbs.twimg.com/profile_images/1754697221595910144/xziLC-5v_400x400.jpg"
|
| 848 |
+
img_result <- download_and_process_image(default_url)
|
| 849 |
+
|
| 850 |
+
if (!is.null(img_result)) {
|
| 851 |
+
return(rasterGrob(img_result$img, interpolate = TRUE))
|
| 852 |
+
} else {
|
| 853 |
+
return(textGrob("Image not available", gp = gpar(col = "red", fontsize = 20)))
|
| 854 |
+
}
|
| 855 |
+
}
|
| 856 |
+
|
| 857 |
+
# Get the logo once and use it twice
|
| 858 |
+
logo_grob <- get_logo()
|
| 859 |
+
|
| 860 |
+
break_plot <- break_plot(game) + theme(legend.position = "none")
|
| 861 |
+
pitch_plots <- pitch_plot_split(game)
|
| 862 |
+
|
| 863 |
+
table_data <- summary_table(game)
|
| 864 |
+
num_rows <- nrow(table_data)
|
| 865 |
+
|
| 866 |
+
print(table_data)
|
| 867 |
+
table_plot <- tableGrob(table_data, rows = NULL, theme = ttheme_minimal(
|
| 868 |
+
core = list(fg_params = list(hjust = 0.5, x = 0.5), bg_params = list(fill = "white")),
|
| 869 |
+
colhead = list(fg_params = list(hjust = 0.5, x = 0.5, fontface = "bold"), bg_params = list(fill = "#f0f0f0")),
|
| 870 |
+
rowhead = list(fg_params = list(hjust = 0.5, x = 0.5), bg_params = list(fill = "white"))
|
| 871 |
+
))
|
| 872 |
+
|
| 873 |
+
total_height <- unit(1, "npc")
|
| 874 |
+
row_height <- total_height / (num_rows + 1)
|
| 875 |
+
table_plot$heights <- unit(rep(row_height, num_rows + 1), "npc")
|
| 876 |
+
|
| 877 |
+
table_plot$widths <- unit(c(
|
| 878 |
+
0.07, # Type
|
| 879 |
+
0.04, # #
|
| 880 |
+
0.08, # Usage%
|
| 881 |
+
0.08, # FP Strike%
|
| 882 |
+
0.05, # Velo
|
| 883 |
+
0.05, # Top
|
| 884 |
+
0.08, # Spin
|
| 885 |
+
0.05, # Ext
|
| 886 |
+
0.05, # IVB
|
| 887 |
+
0.05, # HB
|
| 888 |
+
0.04, # RelH
|
| 889 |
+
0.04, # RelS
|
| 890 |
+
0.06, # VAA
|
| 891 |
+
0.06, # CSW%
|
| 892 |
+
0.06, # Whiff%
|
| 893 |
+
0.06 # Stuff+
|
| 894 |
+
), "npc")
|
| 895 |
+
|
| 896 |
+
for(i in seq(2, nrow(table_plot), 2)) {
|
| 897 |
+
table_plot$grobs[[i]]$gp$fill <- "#f9f9f9"
|
| 898 |
+
}
|
| 899 |
+
|
| 900 |
+
milb_url <- "https://x.com/TimStats/photo"
|
| 901 |
+
mlb_url <- "https://pbs.twimg.com/profile_images/1754697221595910144/xziLC-5v_400x400.jpg"
|
| 902 |
+
img_result <- download_and_process_image(mlb_url)
|
| 903 |
+
if (is.null(img_result)) {
|
| 904 |
+
img_result <- download_and_process_image(milb_url)
|
| 905 |
+
}
|
| 906 |
+
|
| 907 |
+
img_grob <- if (!is.null(img_result)) {
|
| 908 |
+
rasterGrob(img_result$img, interpolate = TRUE)
|
| 909 |
+
} else {
|
| 910 |
+
textGrob("Image not available", gp = gpar(col = "red", fontsize = 20))
|
| 911 |
+
}
|
| 912 |
+
|
| 913 |
+
pitch_colors <- c(
|
| 914 |
+
"Four-Seam"= "#FF4136", "Sinker"= "#FF851B", "Cutter"= "#FFDC00",
|
| 915 |
+
"Changeup"= "#2ECC40", "Slider"= "#0074D9", "Sweeper"= "#ED68ED",
|
| 916 |
+
"Curveball"= "#B10DC9", "Splitter"= "#01FF70", "Knuckle Curve"= "#85144b",
|
| 917 |
+
"Slurve"= "#3D9970", "Knuckle Ball"= "#39CCCC", "Two-Seam Fastball"= "#F012BE",
|
| 918 |
+
"Eephus"= "#AAAAAA", "Fastball"= "#7FDBFF", "Slow Curve"= "#DDDDDD",
|
| 919 |
+
"Screwball"= "#FF69B4"
|
| 920 |
+
)
|
| 921 |
+
|
| 922 |
+
# Create the rolling TimStuff+ graph
|
| 923 |
+
rolling_data <- rolling_timstuff() %>%
|
| 924 |
+
mutate(
|
| 925 |
+
pitch_name = case_when(
|
| 926 |
+
pitch_name %in% c("Fastball", "FourSeamFastBall") ~ "Four-Seam",
|
| 927 |
+
pitch_name %in% c("TwoSeamFastBall", "OneSeamFastBall", "Sinker") ~ "Sinker",
|
| 928 |
+
pitch_name %in% c("ChangeUp") ~ "Changeup",
|
| 929 |
+
pitch_name %in% c("KnuckleCurve") ~ "Knuckle Curve",
|
| 930 |
+
pitch_name %in% c("CutFastBall") ~ "Cutter",
|
| 931 |
+
TRUE ~ pitch_name
|
| 932 |
+
))
|
| 933 |
+
timstuff_plot <- ggplot(rolling_data, aes(x = pitch_number, y = rolling_timstuff, color = pitch_name)) +
|
| 934 |
+
geom_line(size = 1, na.rm = TRUE) +
|
| 935 |
+
geom_point(size = 1, na.rm = TRUE) +
|
| 936 |
+
scale_color_manual(values = pitch_colors) +
|
| 937 |
+
theme_minimal() +
|
| 938 |
+
labs(title = paste0(input$rollnum,"-Pitch Rolling Stuff+"),
|
| 939 |
+
x = "Pitch Number", y = "Stuff+") +
|
| 940 |
+
scale_y_continuous(limits = c(70, 130), na.value = NA) +
|
| 941 |
+
scale_x_continuous(breaks = seq(input$rollnum, max(rolling_data$pitch_number, na.rm = TRUE), by = input$rollnum), limits = c(input$rollnum, NA)) +
|
| 942 |
+
theme(
|
| 943 |
+
plot.title = element_text(hjust = 0.5, face = "bold"),
|
| 944 |
+
legend.position = "none",
|
| 945 |
+
panel.grid.major.x = element_line(color = "gray", size = 0.5)
|
| 946 |
)
|
| 947 |
+
|
| 948 |
+
# If there's no valid data, display a message
|
| 949 |
+
if(all(is.na(rolling_data$rolling_timstuff))) {
|
| 950 |
+
timstuff_plot <- ggplot() +
|
| 951 |
+
theme_void() +
|
| 952 |
+
labs(title = "Insufficient data for Stuff+ calculation") +
|
| 953 |
+
theme(
|
| 954 |
+
plot.title = element_text(hjust = 0.5, face = "bold")
|
| 955 |
+
)
|
| 956 |
+
}
|
| 957 |
+
|
| 958 |
+
title_text <- textGrob(
|
| 959 |
+
paste(input$pitcher, input$title, "Summary Card by @TimStats"),
|
| 960 |
+
gp = gpar(fontsize = 16, fontface = "bold")
|
| 961 |
+
)
|
| 962 |
+
|
| 963 |
+
data_source_text <- textGrob(
|
| 964 |
+
" ",
|
| 965 |
+
gp = gpar(fontsize = 8),
|
| 966 |
+
x = unit(1, "npc") - unit(2, "mm"),
|
| 967 |
+
y = unit(2, "mm"),
|
| 968 |
+
just = c("right", "bottom")
|
| 969 |
+
)
|
| 970 |
+
|
| 971 |
+
legend <- if(nrow(rolling_data) > 0) {
|
| 972 |
+
get_legend(
|
| 973 |
+
ggplot(rolling_data, aes(x = pitch_number, y = rolling_timstuff, color = pitch_name)) +
|
| 974 |
+
geom_point(size = 5) +
|
| 975 |
+
scale_color_manual(values = pitch_colors) +
|
| 976 |
+
theme(legend.position = "bottom",
|
| 977 |
+
legend.title = element_blank(),
|
| 978 |
+
text = element_text(size = 12.5),
|
| 979 |
+
legend.box = "horizontal" ) +
|
| 980 |
+
guides(color = guide_legend(nrow = 1))
|
| 981 |
)
|
| 982 |
+
} else {
|
| 983 |
+
ggplotGrob(ggplot() + theme_void())
|
| 984 |
}
|
| 985 |
+
|
| 986 |
+
# Combine all plots
|
| 987 |
+
combined <- grid.arrange(
|
| 988 |
+
# Row 1: Title and Player Image
|
| 989 |
+
arrangeGrob(
|
| 990 |
+
img_grob,
|
| 991 |
+
title_text,
|
| 992 |
+
logo_grob,
|
| 993 |
+
ncol = 3,
|
| 994 |
+
widths = c(1, 2, 1)
|
| 995 |
+
),
|
| 996 |
+
|
| 997 |
+
# Row 2: Rolling TimStuff+ graph and boxscore
|
| 998 |
+
arrangeGrob(
|
| 999 |
+
timstuff_plot,
|
| 1000 |
+
break_plot,
|
| 1001 |
+
ncol = 2,
|
| 1002 |
+
widths = c(2,1)
|
| 1003 |
+
),
|
| 1004 |
+
|
| 1005 |
+
# Row 3: Three plots in a row
|
| 1006 |
+
arrangeGrob(
|
| 1007 |
+
pitch_plots$lhb,
|
| 1008 |
+
pitch_plots$rhb,
|
| 1009 |
+
ncol = 2,
|
| 1010 |
+
widths = c(1,1)
|
| 1011 |
+
),
|
| 1012 |
+
|
| 1013 |
+
# Row 4: Legend
|
| 1014 |
+
legend,
|
| 1015 |
+
|
| 1016 |
+
# Row 5: Pitch summary table
|
| 1017 |
+
table_plot,
|
| 1018 |
+
|
| 1019 |
+
# Layout parameters
|
| 1020 |
+
nrow = 5,
|
| 1021 |
+
heights = c(0.8, 1.2, 1.2, 0.2, 1)
|
| 1022 |
+
)
|
| 1023 |
+
grid.arrange(
|
| 1024 |
+
gtable_add_padding(
|
| 1025 |
+
combined,
|
| 1026 |
+
padding = unit(c(20, 20, 20, 20), "points") # top, right, bottom, left margins
|
| 1027 |
+
)
|
| 1028 |
+
)
|
| 1029 |
+
combinedPlot(combined)
|
| 1030 |
+
|
| 1031 |
+
output$combinedPlot <- renderPlot({
|
| 1032 |
+
grid.draw(combinedPlot())
|
| 1033 |
+
}, width = 1200, height = 1200)
|
| 1034 |
+
|
| 1035 |
+
output$combinedPlot <- renderPlot({
|
| 1036 |
+
grid.draw(combinedPlot())
|
| 1037 |
+
}, width = 1000, height = 1000) # Modified to be square
|
| 1038 |
+
|
| 1039 |
+
# Update the download handler dimensions
|
| 1040 |
+
output$downloadPlot <- downloadHandler(
|
| 1041 |
+
filename = function() {
|
| 1042 |
+
paste("baseball_card_", Sys.Date(), ".png", sep = "")
|
| 1043 |
+
},
|
| 1044 |
+
content = function(file) {
|
| 1045 |
+
ggsave(file, plot = combinedPlot(), width = 15, height = 15, dpi = 300, units = "in") # Modified to be square
|
| 1046 |
+
}
|
| 1047 |
+
)
|
| 1048 |
+
}
|
| 1049 |
+
else{
|
| 1050 |
+
failplot <- ggplot() +
|
| 1051 |
+
annotate("text", x = 0.5, y = 0.5, label = "Incorrect Password",
|
| 1052 |
+
size = 10, color = "red") +
|
| 1053 |
+
theme_void() +
|
| 1054 |
+
theme(plot.background = element_rect(fill = "white", color = NA))
|
| 1055 |
+
output$combinedPlot <- renderPlot({
|
| 1056 |
+
failplot
|
| 1057 |
+
})
|
| 1058 |
+
}
|
| 1059 |
+
})
|
| 1060 |
}
|
| 1061 |
|
| 1062 |
+
# Run the application
|
| 1063 |
+
shinyApp(ui = ui, server = server)
|
left_batter.png
ADDED
|
right_batter.png
ADDED
|