TimStats commited on
Commit
b439491
·
verified ·
1 Parent(s): e340a7e

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +19 -14
app.R CHANGED
@@ -10,14 +10,19 @@ library(patchwork) # For combining plots
10
 
11
  # [Previous functions remain the same: download_private_csv, heatMap, and data loading]
12
  download_private_csv <- function(repo_id, filename) {
13
- url <- paste0("https://huggingface.co/spaces/", repo_id, "/resolve/main/", filename)
14
- cat(url)
15
- response <- GET(url)
16
 
17
  if (status_code(response) == 200) {
18
  content <- content(response, "text")
19
  con <- textConnection(content)
20
- data <- read.csv(con)
 
 
 
 
 
 
21
  close(con)
22
  return(data)
23
  } else {
@@ -65,22 +70,22 @@ heatMap <- function(data, gtitle, concen) {
65
  )
66
  }
67
 
68
- mlb <- download_private_csv("TimStats/MiLBStatcast", paste0("MLB", "24T", ".csv"))
69
- aaa <- download_private_csv("TimStats/MiLBStatcast", paste0("AAA", "24T", ".csv"))
70
- fsl <- download_private_csv("TimStats/MiLBStatcast", paste0("FSL", "24T", ".csv"))
71
 
72
  ui <- page_fluid(
73
  theme = bs_theme(bg = "#333333", fg = "#ffffff", primary = "#428bca"),
74
 
75
  card(
76
- card_header("Player Analysis Dashboard"),
77
  card(
78
  layout_columns(
79
  col_widths = c(4, 4, 4),
80
  card(
81
  selectInput("league", "Select League:", choices = c("MLB","AAA","FSL")),
82
  selectInput("player", "Select Player:", choices = c("")),
83
- sliderInput("concen", "Heatmap Bandwidth", value = 1, step = 0.05, min = .5, max = 1.5)
84
  ),
85
  card(
86
  selectInput("hand1", "Batter Handedness", choices = c("All Batters","LHH","RHH")),
@@ -111,11 +116,11 @@ server <- function(input, output, session) {
111
  # Function to filter data based on inputs
112
  filter_data <- function(hand, pitch, date_range) {
113
  data <- if(hand == "All Batters") {
114
- t %>% filter(`Pitcher.Name` == input$player)
115
  } else if(hand == "LHH") {
116
- t %>% filter(bhand == "L", `Pitcher.Name` == input$player)
117
  } else {
118
- t %>% filter(bhand == "R", `Pitcher.Name` == input$player)
119
  }
120
 
121
  data %>%
@@ -160,7 +165,7 @@ server <- function(input, output, session) {
160
  session = session,
161
  inputId = "player",
162
  label = "Select Player:",
163
- choices = unique(t$`Pitcher.Name`),
164
  selected = NULL
165
  )
166
  }, error = function(e) {
@@ -173,7 +178,7 @@ server <- function(input, output, session) {
173
  tryCatch({
174
  t <- pname()
175
  d <- t |>
176
- filter(`Pitcher.Name` == input$player)
177
  updateSelectInput(
178
  session = session,
179
  inputId = "pitch1",
 
10
 
11
  # [Previous functions remain the same: download_private_csv, heatMap, and data loading]
12
  download_private_csv <- function(repo_id, filename) {
13
+ url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/", filename)
14
+ response <- GET(url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
 
15
 
16
  if (status_code(response) == 200) {
17
  content <- content(response, "text")
18
  con <- textConnection(content)
19
+
20
+ # Try different read options
21
+ data <- read.csv(con,
22
+ header = TRUE,
23
+ check.names = FALSE, # This prevents R from modifying column names
24
+ fileEncoding = "UTF-8",
25
+ stringsAsFactors = FALSE)
26
  close(con)
27
  return(data)
28
  } else {
 
70
  )
71
  }
72
 
73
+ mlb <- download_private_csv("TimStats/StatcastDataAll", "MLB.csv")
74
+ aaa <- download_private_csv("TimStats/StatcastDataAll", "AAA.csv")
75
+ fsl <- download_private_csv("TimStats/StatcastDataAll", "FSL.csv")
76
 
77
  ui <- page_fluid(
78
  theme = bs_theme(bg = "#333333", fg = "#ffffff", primary = "#428bca"),
79
 
80
  card(
81
+ card_header("2020-2024 Filterable Pitcher Heatmaps"),
82
  card(
83
  layout_columns(
84
  col_widths = c(4, 4, 4),
85
  card(
86
  selectInput("league", "Select League:", choices = c("MLB","AAA","FSL")),
87
  selectInput("player", "Select Player:", choices = c("")),
88
+ sliderInput("concen", "Heatmap Bandwidth Factor", value = .75, step = 0.05, min = .5, max = 1.5)
89
  ),
90
  card(
91
  selectInput("hand1", "Batter Handedness", choices = c("All Batters","LHH","RHH")),
 
116
  # Function to filter data based on inputs
117
  filter_data <- function(hand, pitch, date_range) {
118
  data <- if(hand == "All Batters") {
119
+ t %>% filter(`Pitcher Name` == input$player)
120
  } else if(hand == "LHH") {
121
+ t %>% filter(bhand == "L", `Pitcher Name` == input$player)
122
  } else {
123
+ t %>% filter(bhand == "R", `Pitcher Name` == input$player)
124
  }
125
 
126
  data %>%
 
165
  session = session,
166
  inputId = "player",
167
  label = "Select Player:",
168
+ choices = unique(t$`Pitcher Name`),
169
  selected = NULL
170
  )
171
  }, error = function(e) {
 
178
  tryCatch({
179
  t <- pname()
180
  d <- t |>
181
+ filter(`Pitcher Name` == input$player)
182
  updateSelectInput(
183
  session = session,
184
  inputId = "pitch1",