TimStats commited on
Commit
284bf4a
·
verified ·
1 Parent(s): b3cd12b

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +48 -4
app.R CHANGED
@@ -12,6 +12,7 @@ library(gtable)
12
  library(httr)
13
  library(bslib)
14
  library(jsonlite)
 
15
 
16
  check_patreon_access <- function(email) {
17
  campaign_id <- Sys.getenv("PATREON_CAMPAIGN_ID")
@@ -57,6 +58,41 @@ check_patreon_access <- function(email) {
57
 
58
  return(FALSE)
59
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  font_add_google("Roboto Condensed")
61
 
62
  is_barrel <- function(df) {
@@ -273,10 +309,18 @@ download_private_csv <- function(repo_id, filename) {
273
  stop("Failed to download dataset")
274
  }
275
  }
276
- MLB <- download_private_csv("TimStats/StatcastDataAll", "MLB.csv")
277
- AAA <- download_private_csv("TimStats/StatcastDataAll", "AAA.csv")
278
- FSLAll <- download_private_csv("TimStats/StatcastDataAll", "FSL.csv")
 
 
 
 
 
279
 
 
 
 
280
  temp_players <- MLB %>% filter(season == 2024)
281
  MLBC <- rbind(MLB,AAA,FSLAll)
282
  data <- is_barrel(MLBC) %>%
@@ -385,7 +429,7 @@ server <- function(input, output,session) {
385
  titlePanel(NULL, windowTitle = "Baseball Stats Visualization"),
386
  sidebarLayout(
387
  sidebarPanel(
388
- selectInput("szn", "Season:", c(2024, 2023, 2022, 2021, 2021)),
389
  selectInput("level", "Level:", c("MLB", "AAA", "FSL")),
390
  selectInput("type", "Player Type:", c("Batter", "Pitcher")),
391
  selectInput("player", "Player:", choices = unique(temp_players$`Batter Name`)),
 
12
  library(httr)
13
  library(bslib)
14
  library(jsonlite)
15
+ library(arrow)
16
 
17
  check_patreon_access <- function(email) {
18
  campaign_id <- Sys.getenv("PATREON_CAMPAIGN_ID")
 
58
 
59
  return(FALSE)
60
  }
61
+
62
+ download_private_parquet <- function(repo_id, filename) {
63
+ library(httr)
64
+ library(arrow)
65
+
66
+ # Create the direct download URL based on your example
67
+ url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/", filename, "?download=true")
68
+
69
+ # Create a temporary file
70
+ temp_file <- tempfile(fileext = ".parquet")
71
+
72
+ # Download directly to file
73
+ response <- GET(
74
+ url,
75
+ add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))),
76
+ write_disk(temp_file, overwrite = TRUE)
77
+ )
78
+
79
+ # Check if download was successful
80
+ if (status_code(response) == 200) {
81
+ tryCatch({
82
+ # Read the parquet file
83
+ data <- read_parquet(temp_file)
84
+ file.remove(temp_file)
85
+ return(data)
86
+ }, error = function(e) {
87
+ file.remove(temp_file)
88
+ stop(paste("Error reading parquet file:", e$message))
89
+ })
90
+ } else {
91
+ file.remove(temp_file)
92
+ stop(paste("Failed to download file. Status code:", status_code(response)))
93
+ }
94
+ }
95
+
96
  font_add_google("Roboto Condensed")
97
 
98
  is_barrel <- function(df) {
 
309
  stop("Failed to download dataset")
310
  }
311
  }
312
+ MLB <- download_private_parquet("TimStats/StatcastDataAll", "MLB.csv")
313
+ AAA <- download_private_parquet("TimStats/StatcastDataAll", "AAA.csv")
314
+ FSLAll <- download_private_parquet("TimStats/StatcastDataAll", "FSL.csv")
315
+
316
+ MLB25 <- download_private_parquet("TimStats/StatcastDataAll", "MLB25.parquet")
317
+ MLB25$level <- "MLB"
318
+ AAA25 <- download_private_parquet("TimStats/StatcastDataAll", "AAA25.parquet")
319
+ AAA25$level <- "AAA"
320
 
321
+ MLB <- rbind(MLB,MLB25)
322
+ AAA <- rbind(AAA,AAA25)
323
+
324
  temp_players <- MLB %>% filter(season == 2024)
325
  MLBC <- rbind(MLB,AAA,FSLAll)
326
  data <- is_barrel(MLBC) %>%
 
429
  titlePanel(NULL, windowTitle = "Baseball Stats Visualization"),
430
  sidebarLayout(
431
  sidebarPanel(
432
+ selectInput("szn", "Season:", c(2025, 2024, 2023, 2022, 2021, 2021)),
433
  selectInput("level", "Level:", c("MLB", "AAA", "FSL")),
434
  selectInput("type", "Player Type:", c("Batter", "Pitcher")),
435
  selectInput("player", "Player:", choices = unique(temp_players$`Batter Name`)),