TimStats commited on
Commit
ca5bcef
·
verified ·
1 Parent(s): ba55467

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +82 -160
app.R CHANGED
@@ -1,5 +1,5 @@
1
  library(shiny)
2
- library(shinydashboard)
3
  library(shinyWidgets)
4
  library(ggplot2)
5
  library(MASS) # For kde2d function
@@ -60,7 +60,7 @@ heatMap <- function(data, gtitle, concen) {
60
  panel.background = element_rect(fill = "#333333", color = NA),
61
  text = element_text(color = "white"),
62
  panel.grid = element_blank(),
63
- plot.title = element_text(hjust = 0.5) # Center the title
64
  )
65
  }
66
 
@@ -68,133 +68,85 @@ mlb <- download_private_csv("TimStats/MiLBStatcast", paste0("MLB", "24T", ".csv"
68
  aaa <- download_private_csv("TimStats/MiLBStatcast", paste0("AAA", "24T", ".csv"))
69
  fsl <- download_private_csv("TimStats/MiLBStatcast", paste0("FSL", "24T", ".csv"))
70
 
71
- # ... (previous library imports and functions remain the same)
72
-
73
- ui <- dashboardPage(
74
- skin = "black", # Use the black skin for a darker theme
75
- dashboardHeader(title = "Player Analysis Dashboard"),
76
- dashboardSidebar(
77
- sidebarMenu(
78
- menuItem("Player Selection", tabName = "player", icon = icon("user"))
79
- )
80
- ),
81
- dashboardBody(
82
- tags$head(
83
- tags$style(HTML("
84
- .content-wrapper, .right-side {
85
- background-color: #333333;
86
- }
87
- .box {
88
- background-color: #444444;
89
- border-top: none;
90
- color: #ffffff;
91
- }
92
- .box-header {
93
- color: #ffffff;
94
- }
95
- .form-control {
96
- background-color: #e0e0e0;
97
- color: #333333;
98
- border: 1px solid #777777;
99
- }
100
- .selectize-input {
101
- background-color: #e0e0e0;
102
- color: #333333;
103
- }
104
- .selectize-dropdown {
105
- background-color: #e0e0e0;
106
- color: #333333;
107
- }
108
- .selectize-dropdown-content {
109
- background-color: #e0e0e0;
110
- color: #333333;
111
- }
112
- .selectize-dropdown .active {
113
- background-color: #b0b0b0;
114
- color: #333333;
115
- }
116
- label {
117
- color: #ffffff;
118
- }
119
- .irs-min, .irs-max, .irs-from, .irs-to, .irs-single {
120
- color: #333333;
121
- background-color: #e0e0e0;
122
- }
123
- .irs-bar {
124
- border-top: 1px solid #428bca;
125
- border-bottom: 1px solid #428bca;
126
- background: #428bca;
127
- }
128
- .irs-line {
129
- background: #e0e0e0;
130
- border: 1px solid #777777;
131
- }
132
- .daterangepicker {
133
- background-color: #e0e0e0;
134
- color: #333333;
135
- }
136
- .daterangepicker .calendar-table {
137
- background-color: #e0e0e0;
138
- }
139
- .daterangepicker td.active, .daterangepicker td.active:hover {
140
- background-color: #007bff;
141
- color: #ffffff;
142
- }
143
- .daterangepicker td.available:hover, .daterangepicker th.available:hover {
144
- background-color: #b0b0b0;
145
- }
146
- .daterangepicker .calendar-table .next span, .daterangepicker .calendar-table .prev span {
147
- color: #333333;
148
- }
149
- .daterangepicker .drp-selected {
150
- color: #333333;
151
- }
152
- "))
153
- ),
154
- tabItems(
155
- tabItem(tabName = "player",
156
- fluidRow(
157
- column(width = 3,
158
- box(width = NULL, title = "Pick Player Here", status = "primary",
159
- selectInput("league", "Select League:", choices = c("MLB","AAA","FSL")),
160
- selectInput("player", "Select Player:", choices = c("")),
161
- sliderInput("concen", "Heatmap Bandwidth", value = 1, step = 0.05, min = .5, max = 1.5)
162
- )
163
- ),
164
- column(width = 4,
165
- box(width = NULL, title = "Filters for Graph 1", status = "info",
166
- selectInput("hand1", "Batter Handedness", choices = c("All Batters","LHH","RHH")),
167
- selectInput("pitch1", "Pitch:", choices = c("")),
168
- dateRangeInput("date1", "Date Range:", start = "2024-02-09", end = "2024-09-05")
169
- )
170
- ),
171
- column(width = 4,
172
- box(width = NULL, title = "Filters for Graph 2", status = "success",
173
- selectInput("hand2", "Batter Handedness", choices = c("All Batters","LHH","RHH")),
174
- selectInput("pitch2", "Pitch:", choices = c("")),
175
- dateRangeInput("date2", "Date Range:", start = "2024-02-09", end = "2024-09-05")
176
- )
177
- )
178
- ),
179
- fluidRow(
180
- column(width = 7,
181
- box(width = NULL, title = "Combined Heatmap", status = "warning",
182
- plotOutput("combined_graph", height = "600px")
183
- )
184
- )
185
- )
186
  )
187
  )
188
  )
189
  )
190
 
191
-
192
  server <- function(input, output, session) {
193
-
194
  pname <- reactiveVal()
195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  observeEvent(input$league, {
197
- req(input$league) # Ensure league input is available
198
  tryCatch({
199
  if(input$league == "MLB"){
200
  t <- mlb
@@ -242,47 +194,17 @@ server <- function(input, output, session) {
242
  })
243
 
244
  output$combined_graph <- renderPlot({
245
-
246
- t <- pname()
247
-
248
- # Function to filter data based on inputs
249
- filter_data <- function(hand, pitch, date_range) {
250
- data <- if(hand == "All Batters") {
251
- t %>% filter(`Pitcher.Name` == input$player)
252
- } else if(hand == "LHH") {
253
- t %>% filter(bhand == "L", `Pitcher.Name` == input$player)
254
- } else {
255
- t %>% filter(bhand == "R", `Pitcher.Name` == input$player)
256
- }
257
-
258
- data %>%
259
- filter(pitch_name == pitch) %>%
260
- filter(between(as.Date(date), date_range[1], date_range[2]))
261
- }
262
-
263
- graph1_data <- filter_data(input$hand1, input$pitch1, input$date1)
264
- graph2_data <- filter_data(input$hand2, input$pitch2, input$date2)
265
-
266
- title1 <- paste0(input$player, " ", input$pitch1, " vs. ", input$hand1, "\n", input$date1[1], " to ", input$date1[2])
267
- title2 <- paste0(input$player, " ", input$pitch2, " vs. ", input$hand2, "\n", input$date2[1], " to ", input$date2[2])
268
-
269
- plot1 <- heatMap(graph1_data, title1, input$concen)
270
- plot2 <- heatMap(graph2_data, title2, input$concen)
271
-
272
- combined_plot <- plot1 + plot2 +
273
- plot_layout(ncol = 2) +
274
- plot_annotation(
275
- title = paste0(input$player, " Pitch Comparison"),
276
- theme = theme(
277
- plot.title = element_text(hjust = 0.5, size = 20, color = "white"),
278
- plot.background = element_rect(fill = "#333333", color = NA),
279
- plot.margin = margin(0, 0, 0, 0)
280
- )
281
- ) &
282
- theme(plot.margin = margin(0, 0, 0, 0))
283
-
284
- return(combined_plot)
285
  }, bg = "#333333", width = 800)
 
 
 
 
 
 
 
 
 
286
  }
287
 
288
  shinyApp(ui = ui, server = server)
 
1
  library(shiny)
2
+ library(bslib)
3
  library(shinyWidgets)
4
  library(ggplot2)
5
  library(MASS) # For kde2d function
 
60
  panel.background = element_rect(fill = "#333333", color = NA),
61
  text = element_text(color = "white"),
62
  panel.grid = element_blank(),
63
+ plot.title = element_text(hjust = 0.5)
64
  )
65
  }
66
 
 
68
  aaa <- download_private_csv("TimStats/MiLBStatcast", paste0("AAA", "24T", ".csv"))
69
  fsl <- download_private_csv("TimStats/MiLBStatcast", paste0("FSL", "24T", ".csv"))
70
 
71
+ ui <- page_fluid(
72
+ theme = bs_theme(bg = "#333333", fg = "#ffffff", primary = "#428bca"),
73
+
74
+ card(
75
+ card_header("Player Selection"),
76
+ layout_sidebar(
77
+ sidebar = sidebar(
78
+ selectInput("league", "Select League:", choices = c("MLB","AAA","FSL")),
79
+ selectInput("player", "Select Player:", choices = c("")),
80
+ sliderInput("concen", "Heatmap Bandwidth", value = 1, step = 0.05, min = .5, max = 1.5)
81
+ ),
82
+ card(
83
+ layout_columns(
84
+ col_widths = c(6, 6),
85
+ card(
86
+ selectInput("hand1", "Batter Handedness", choices = c("All Batters","LHH","RHH")),
87
+ selectInput("pitch1", "Pitch:", choices = c("")),
88
+ dateRangeInput("date1", "Date Range:", start = "2024-02-09", end = "2024-09-05")
89
+ ),
90
+ card(
91
+ selectInput("hand2", "Batter Handedness", choices = c("All Batters","LHH","RHH")),
92
+ selectInput("pitch2", "Pitch:", choices = c("")),
93
+ dateRangeInput("date2", "Date Range:", start = "2024-02-09", end = "2024-09-05")
94
+ )
95
+ )
96
+ ),
97
+ card(
98
+ downloadButton("download", "Download Plot", class = "btn-primary mb-3"),
99
+ plotOutput("combined_graph", height = "600px")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  )
101
  )
102
  )
103
  )
104
 
 
105
  server <- function(input, output, session) {
 
106
  pname <- reactiveVal()
107
 
108
+ plot_data <- reactive({
109
+ t <- pname()
110
+
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 %>%
122
+ filter(pitch_name == pitch) %>%
123
+ filter(between(as.Date(date), date_range[1], date_range[2]))
124
+ }
125
+
126
+ graph1_data <- filter_data(input$hand1, input$pitch1, input$date1)
127
+ graph2_data <- filter_data(input$hand2, input$pitch2, input$date2)
128
+
129
+ title1 <- paste0(input$player, " ", input$pitch1, " vs. ", input$hand1, "\n", input$date1[1], " to ", input$date1[2])
130
+ title2 <- paste0(input$player, " ", input$pitch2, " vs. ", input$hand2, "\n", input$date2[1], " to ", input$date2[2])
131
+
132
+ plot1 <- heatMap(graph1_data, title1, input$concen)
133
+ plot2 <- heatMap(graph2_data, title2, input$concen)
134
+
135
+ plot1 + plot2 +
136
+ plot_layout(ncol = 2) +
137
+ plot_annotation(
138
+ title = paste0(input$player, " Pitch Comparison"),
139
+ theme = theme(
140
+ plot.title = element_text(hjust = 0.5, size = 20, color = "white"),
141
+ plot.background = element_rect(fill = "#333333", color = NA),
142
+ plot.margin = margin(0, 0, 0, 0)
143
+ )
144
+ ) &
145
+ theme(plot.margin = margin(0, 0, 0, 0))
146
+ })
147
+
148
  observeEvent(input$league, {
149
+ req(input$league)
150
  tryCatch({
151
  if(input$league == "MLB"){
152
  t <- mlb
 
194
  })
195
 
196
  output$combined_graph <- renderPlot({
197
+ plot_data()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  }, bg = "#333333", width = 800)
199
+
200
+ output$download <- downloadHandler(
201
+ filename = function() {
202
+ paste0(input$player, "_pitch_comparison.png")
203
+ },
204
+ content = function(file) {
205
+ ggsave(file, plot = plot_data(), width = 12, height = 8, dpi = 300)
206
+ }
207
+ )
208
  }
209
 
210
  shinyApp(ui = ui, server = server)