TimStats commited on
Commit
760ab48
·
verified ·
1 Parent(s): 00452ef

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +105 -27
app.R CHANGED
@@ -6,6 +6,37 @@ library(caret)
6
  library(shinythemes)
7
  library(ggplot2)
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  calculate_timstuff <- function(data) {
10
  data_matrix <- as.matrix(data)
11
  mod <- xgb.load('TimStuff2.model')
@@ -22,18 +53,32 @@ ui <- fluidPage(
22
  sidebarLayout(
23
  sidebarPanel(
24
  width = 4,
25
- radioButtons("phand", "Hand", choices = c("R", "L"), inline = TRUE),
26
- numericInput("velo", "Velocity (mph)", value = 95, min = 70, max = 110, step = 1),
27
- numericInput("ivb", "Induced Vertical Break (in.) (Pitcher's Perspective)", value = 16, min = -30, max = 30, step = 0.1),
28
- numericInput("hb", "Horizontal Break (in.) (Pitcher's Perspective)", value = 6, min = -25, max = 25, step = 0.1),
29
- numericInput("spinrate", "Spin Rate (rpm)", value = 2300, min = 1000, max = 3500, step = 10),
30
- numericInput("ext", "Extension (ft)", value = 6.3, min = 5, max = 8, step = 0.1),
31
- numericInput("spinaxisdiff", "Spin Axis Difference (°) (Spin Axis - Inferred Spin Axis)", value = 0, min = -180, max = 180, step = 1),
32
- numericInput("x0", "Horizontal Release Point (ft) (Catcher's Perspective)", value = -1.5, min = -5, max = 5, step = 0.1),
33
- numericInput("z0", "Vertical Release Point (ft)", value = 5, min = 3, max = 8, step = 0.1),
34
- #numericInput("primvelo", "Average Velocity of Primary Pitch", value = 95, min = 70, max = 110, step = 1),
35
- #numericInput("primIVB", "Average IVB of Primary Pitch", value = 16, min = -30, max = 30, step = 0.1),
36
- #numericInput("primHB", "Average HB of Primary Pitch", value = 6, min = -25, max = 25, step = 0.1),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  selectInput("feature_to_vary", "Select Feature to Vary:",
38
  choices = c("Velocity" = "start_speed",
39
  "Induced Vertical Break" = "IVB",
@@ -55,22 +100,55 @@ ui <- fluidPage(
55
  )
56
  )
57
 
58
- server <- function(input, output) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  reactive_data <- reactive({
60
- data.frame(
61
- ishandL = ifelse(input$phand == "L", 1, 0),
62
- start_speed = input$velo,
63
- IVB = input$ivb,
64
- HB = input$hb,
65
- EAA = input$ext / 6.3,
66
- x0 = input$x0,
67
- z0 = input$z0,
68
- spin_rate = input$spinrate,
69
- SADiff = input$spinaxisdiff
70
- #primary_speed = input$primvelo - input$velo,
71
- #primary_IVB = input$primIVB - input$ivb,
72
- #primary_HB = input$primHB - input$hb
73
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  })
75
 
76
  output$stuff <- renderText({
 
6
  library(shinythemes)
7
  library(ggplot2)
8
 
9
+ # Add sample pitcher data - you'll want to replace this with your actual data
10
+ # pitcher_data <- data.frame(
11
+ # pitcher_name = c("Pitcher A", "Pitcher A", "Pitcher B", "Pitcher B"),
12
+ # pitch_name = c("Fastball", "Slider", "Fastball", "Curveball"),
13
+ # velo = c(95, 85, 92, 78),
14
+ # ivb = c(16, 2, 15, -5),
15
+ # hb = c(6, 2, 5, -8),
16
+ # spinrate = c(2300, 2500, 2200, 2400),
17
+ # ext = c(6.3, 6.2, 6.5, 6.4),
18
+ # spinaxisdiff = c(0, 5, -2, 8),
19
+ # x0 = c(-1.5, -1.6, -1.4, -1.3),
20
+ # z0 = c(5, 4.8, 5.2, 5.1),
21
+ # phand = c("R", "R", "L", "L")
22
+ # )
23
+ MLB24T <- read.csv("MLB24T.csv", header = TRUE, check.names = FALSE, fileEncoding = "UTF-8")
24
+
25
+ pitcher_data <- MLB24T %>%
26
+ group_by(`Pitcher Name`,pitch_name,phand) %>%
27
+ summarise(
28
+ "start_speed" = mean(start_speed,na.rm = TRUE),
29
+ "IVB" = mean(IVB,na.rm = TRUE),
30
+ "HB" = mean(HB,na.rm = TRUE) ,
31
+ "EAA" = mean(EAA,na.rm = TRUE) ,
32
+ "x0" = mean(x0,na.rm = TRUE) ,
33
+ "z0" = mean(z0, na.rm = TRUE) ,
34
+ "spin_rate" = mean(spin_rate,na.rm = TRUE),
35
+ "SADiff" = mean(SADiff,na.rm = TRUE)
36
+ )
37
+
38
+
39
+
40
  calculate_timstuff <- function(data) {
41
  data_matrix <- as.matrix(data)
42
  mod <- xgb.load('TimStuff2.model')
 
53
  sidebarLayout(
54
  sidebarPanel(
55
  width = 4,
56
+
57
+ # Add pitcher selection
58
+ selectInput("pitcher_select", "Select Pitcher",
59
+ choices = c("Custom Input", unique(pitcher_data$`Pitcher Name`))),
60
+
61
+ # Add pitch type selection - will be dynamically updated based on pitcher
62
+ conditionalPanel(
63
+ condition = "input.pitcher_select != 'Custom Input'",
64
+ selectInput("pitch_select", "Select Pitch Type",
65
+ choices = NULL) # Will be updated in server
66
+ ),
67
+
68
+ # Original inputs wrapped in conditional panel
69
+ conditionalPanel(
70
+ condition = "input.pitcher_select == 'Custom Input'",
71
+ radioButtons("phand", "Hand", choices = c("R", "L"), inline = TRUE),
72
+ numericInput("velo", "Velocity (mph)", value = 95, min = 70, max = 110, step = 1),
73
+ numericInput("ivb", "Induced Vertical Break (in.) (Pitcher's Perspective)", value = 16, min = -30, max = 30, step = 0.1),
74
+ numericInput("hb", "Horizontal Break (in.) (Pitcher's Perspective)", value = 6, min = -25, max = 25, step = 0.1),
75
+ numericInput("spinrate", "Spin Rate (rpm)", value = 2300, min = 1000, max = 3500, step = 10),
76
+ numericInput("ext", "Extension (ft)", value = 6.3, min = 5, max = 8, step = 0.1),
77
+ numericInput("spinaxisdiff", "Spin Axis Difference (°)", value = 0, min = -180, max = 180, step = 1),
78
+ numericInput("x0", "Horizontal Release Point (ft)", value = -1.5, min = -5, max = 5, step = 0.1),
79
+ numericInput("z0", "Vertical Release Point (ft)", value = 5, min = 3, max = 8, step = 0.1)
80
+ ),
81
+
82
  selectInput("feature_to_vary", "Select Feature to Vary:",
83
  choices = c("Velocity" = "start_speed",
84
  "Induced Vertical Break" = "IVB",
 
100
  )
101
  )
102
 
103
+ server <- function(input, output, session) {
104
+
105
+ # Update pitch selections based on pitcher
106
+ observe({
107
+ if(input$pitcher_select != "Custom Input") {
108
+ pitcher_pitches <- pitcher_data %>%
109
+ filter(`Pitcher Name` == input$pitcher_select) %>%
110
+ pull(pitch_name) %>%
111
+ unique()
112
+
113
+ updateSelectInput(session, "pitch_select",
114
+ choices = pitcher_pitches)
115
+ }
116
+ })
117
+
118
+ # Reactive values for all inputs
119
  reactive_data <- reactive({
120
+ if(input$pitcher_select == "Custom Input") {
121
+ # Use manual inputs
122
+ data.frame(
123
+ ishandL = ifelse(input$phand == "L", 1, 0),
124
+ start_speed = input$velo,
125
+ IVB = input$ivb,
126
+ HB = input$hb,
127
+ EAA = input$ext / 6.3,
128
+ x0 = input$x0,
129
+ z0 = input$z0,
130
+ spin_rate = input$spinrate,
131
+ SADiff = input$spinaxisdiff
132
+ )
133
+ } else {
134
+ # Use selected pitcher data
135
+ pitch_data <- pitcher_data %>%
136
+ filter(`Pitcher Name` == input$pitcher_select,
137
+ pitch_name == input$pitch_select) %>%
138
+ select(-`Pitcher Name`, -pitch_name)
139
+
140
+ data.frame(
141
+ ishandL = ifelse(pitch_data$phand == "L", 1, 0),
142
+ start_speed = pitch_data$start_speed,
143
+ IVB = pitch_data$IVB,
144
+ HB = pitch_data$HB,
145
+ EAA = pitch_data$EAA,
146
+ x0 = pitch_data$x0,
147
+ z0 = pitch_data$z0,
148
+ spin_rate = pitch_data$spin_rate,
149
+ SADiff = pitch_data$SADiff
150
+ )
151
+ }
152
  })
153
 
154
  output$stuff <- renderText({