TimStats commited on
Commit
9dffb92
·
verified ·
1 Parent(s): 0ad5dfc

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +73 -5
app.R CHANGED
@@ -7,6 +7,7 @@ library(httr)
7
  library(bslib)
8
  library(rtabulator)
9
  library(purrr)
 
10
 
11
  download_private_csv <- function(repo_id, filename) {
12
  url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/", filename)
@@ -65,16 +66,83 @@ download_private_csv <- function(repo_id, filename) {
65
  }
66
  }
67
 
68
- MLB25 <- download_private_csv("TimStats/StatcastDataAll", "MLB25.csv")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  MLB25$level <- "MLB"
70
- AAA25 <- download_private_csv("TimStats/StatcastDataAll", "AAA25.csv")
71
  AAA25$level <- "AAA"
72
  #ST <- read.csv("SpringT25.csv", header = TRUE, check.names = FALSE, fileEncoding = "UTF-8")
73
 
74
  #names(ST)
75
- MLB <- download_private_csv("TimStats/StatcastDataAll", "MLB.csv")
76
- AAA <- download_private_csv("TimStats/StatcastDataAll", "AAA.csv")
77
- FSL <- download_private_csv("TimStats/StatcastDataAll", "FSL.csv")
78
  MLB <- rbind(MLB,MLB25)
79
  AAA <- rbind(AAA,AAA25)
80
 
 
7
  library(bslib)
8
  library(rtabulator)
9
  library(purrr)
10
+ library(arrow)
11
 
12
  download_private_csv <- function(repo_id, filename) {
13
  url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/", filename)
 
66
  }
67
  }
68
 
69
+ download_private_parquet <- function(repo_id, filename) {
70
+ library(httr)
71
+ library(arrow)
72
+
73
+ url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/", filename)
74
+ response <- GET(url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
75
+
76
+ if (status_code(response) == 200) {
77
+ # First check if it's an LFS pointer
78
+ content_raw <- content(response, "raw")
79
+ content_text <- rawToChar(content_raw)
80
+
81
+ # Check if this is an LFS pointer
82
+ if (grepl("^version https://git-lfs.github.com/spec/", content_text)) {
83
+ # Extract the oid (hash) from the pointer
84
+ oid_line <- grep("oid sha256:", strsplit(content_text, "\n")[[1]], value = TRUE)
85
+ oid <- gsub("oid sha256:", "", oid_line)
86
+ oid <- trimws(oid)
87
+
88
+ # Construct the LFS content URL
89
+ lfs_url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/.git/lfs/objects/",
90
+ substr(oid, 1, 2), "/", substr(oid, 3, 4), "/", oid)
91
+
92
+ # Get the actual content from LFS storage
93
+ lfs_response <- GET(lfs_url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
94
+
95
+ if (status_code(lfs_response) == 200) {
96
+ content_raw <- content(lfs_response, "raw")
97
+ } else {
98
+ # Alternative LFS URL format
99
+ lfs_url <- paste0("https://huggingface.co/datasets/", repo_id, "/lfs/resolve/main/", filename, "?download=true")
100
+ lfs_response <- GET(lfs_url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
101
+
102
+ if (status_code(lfs_response) == 200) {
103
+ content_raw <- content(lfs_response, "raw")
104
+ } else {
105
+ stop(paste("Failed to download LFS content. Status code:", status_code(lfs_response)))
106
+ }
107
+ }
108
+ }
109
+
110
+ # Write the raw content to a temporary file
111
+ temp_file <- tempfile(fileext = ".parquet")
112
+ writeBin(content_raw, temp_file)
113
+
114
+ # Read the parquet file using arrow
115
+ tryCatch({
116
+ data <- read_parquet(temp_file)
117
+
118
+ # Clean up - remove the temporary file
119
+ if (file.exists(temp_file)) {
120
+ file.remove(temp_file)
121
+ }
122
+
123
+ return(data)
124
+ }, error = function(e) {
125
+ # Clean up in case of error
126
+ if (file.exists(temp_file)) {
127
+ file.remove(temp_file)
128
+ }
129
+ stop(paste("Error reading parquet file:", e$message))
130
+ })
131
+ } else {
132
+ stop(paste("Failed to download dataset. Status code:", status_code(response)))
133
+ }
134
+ }
135
+
136
+ MLB25 <- download_private_parquet("TimStats/StatcastDataAll", "MLB25.parquet")
137
  MLB25$level <- "MLB"
138
+ AAA25 <- download_private_parquet("TimStats/StatcastDataAll", "AAA25.parquet")
139
  AAA25$level <- "AAA"
140
  #ST <- read.csv("SpringT25.csv", header = TRUE, check.names = FALSE, fileEncoding = "UTF-8")
141
 
142
  #names(ST)
143
+ MLB <- download_private_parquet("TimStats/StatcastDataAll", "MLB.parquet")
144
+ AAA <- download_private_parquet("TimStats/StatcastDataAll", "AAA.parquet")
145
+ FSL <- download_private_parquet("TimStats/StatcastDataAll", "FSL.parquet")
146
  MLB <- rbind(MLB,MLB25)
147
  AAA <- rbind(AAA,AAA25)
148