TimStats commited on
Commit
c9507b9
·
verified ·
1 Parent(s): 7b3e91e

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +86 -0
app.R CHANGED
@@ -115,6 +115,92 @@ heatMap <- function(data, gtitle, concen) {
115
  )
116
  }
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  MLB25 <- download_private_parquet("TimStats/StatcastDataAll", "MLB25.parquet")
119
  MLB25$level <- "MLB"
120
  AAA25 <- download_private_parquet("TimStats/StatcastDataAll", "AAA25.parquet")
 
115
  )
116
  }
117
 
118
+ download_private_parquet <- function(repo_id, filename) {
119
+ library(httr)
120
+ library(arrow)
121
+
122
+ # Create the direct download URL based on your example
123
+ url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/", filename, "?download=true")
124
+
125
+ # Create a temporary file
126
+ temp_file <- tempfile(fileext = ".parquet")
127
+
128
+ # Download directly to file
129
+ response <- GET(
130
+ url,
131
+ add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))),
132
+ write_disk(temp_file, overwrite = TRUE)
133
+ )
134
+
135
+ # Check if download was successful
136
+ if (status_code(response) == 200) {
137
+ tryCatch({
138
+ # Read the parquet file
139
+ data <- read_parquet(temp_file)
140
+ file.remove(temp_file)
141
+ return(data)
142
+ }, error = function(e) {
143
+ file.remove(temp_file)
144
+ stop(paste("Error reading parquet file:", e$message))
145
+ })
146
+ } else {
147
+ file.remove(temp_file)
148
+ stop(paste("Failed to download file. Status code:", status_code(response)))
149
+ }
150
+ }
151
+
152
+ font_add_google("Roboto Condensed")
153
+
154
+ is_barrel <- function(df) {
155
+ df$barrel <- with(df, ifelse(hit_angle <= 50 & hit_speed >= 97 & hit_speed * 1.5 -
156
+ hit_angle >= 117 & hit_speed + hit_angle >= 123, 1, 0))
157
+ return(df)
158
+ }
159
+
160
+ apply_percentile_calcs <- function(data) {
161
+ # List of columns to apply percent_rank
162
+ percent_rank_cols <- c("Z-Con%", "Z-Swing%", "O-Con%", "Avg EV", "Max EV", "EV90", "Barrel%", "Swing%", "wOBA",
163
+ "wOBACON","xwOBA","xDamage")
164
+
165
+ # List of columns to apply inverse percent_rank
166
+ inverse_percent_rank_cols <- c("Chase%", "Whiff%", "stdev(LA)", "SwStr%")
167
+
168
+ # Create an empty list to store results
169
+ percentile_list <- list()
170
+
171
+ # Calculate regular percentiles
172
+ for(col in percent_rank_cols) {
173
+ percentile_list[[col]] <- data.frame(
174
+ `Batter Name` = data[["Batter Name"]], # Using [[ ]] to preserve exact column name
175
+ `Batter ID` = data[["Batter ID"]], # Using [[ ]] to preserve exact column name
176
+ metric = col,
177
+ percentile = round(percent_rank(data[[col]]) * 100),
178
+ value = data[[col]],
179
+ stringsAsFactors = FALSE
180
+ )
181
+ }
182
+
183
+ # Calculate inverse percentiles
184
+ for(col in inverse_percent_rank_cols) {
185
+ percentile_list[[col]] <- data.frame(
186
+ `Batter Name` = data[["Batter Name"]], # Using [[ ]] to preserve exact column name
187
+ `Batter ID` = data[["Batter ID"]], # Using [[ ]] to preserve exact column name
188
+ metric = col,
189
+ percentile = round((1 - percent_rank(data[[col]])) * 100),
190
+ value = data[[col]],
191
+ stringsAsFactors = FALSE
192
+ )
193
+ }
194
+
195
+ # Combine all results into one data frame
196
+ result <- do.call(rbind, percentile_list)
197
+
198
+ # Reset row names
199
+ rownames(result) <- NULL
200
+
201
+ return(result)
202
+ }
203
+
204
  MLB25 <- download_private_parquet("TimStats/StatcastDataAll", "MLB25.parquet")
205
  MLB25$level <- "MLB"
206
  AAA25 <- download_private_parquet("TimStats/StatcastDataAll", "AAA25.parquet")