Skip to content

Instantly share code, notes, and snippets.

@bayesball
Created May 14, 2021 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayesball/17c41a2c98209ea211b0cf83a8c135aa to your computer and use it in GitHub Desktop.
Save bayesball/17c41a2c98209ea211b0cf83a8c135aa to your computer and use it in GitHub Desktop.
sample shiny app
library(shiny)
library(readr)
library(dplyr)
library(ggplot2)
sc_pitcher_2019 <- read_delim(
"https://raw.githubusercontent.com/bayesball/ShinyBaseball/main/data/sc_pitcher_2019.txt",
delim = " ")
ui <- fluidPage(
column(4, wellPanel(
textInput(inputId = "pid",
label = "Pitcher MLBAM Id:",
value = "605400"),
checkboxGroupInput(inputId = "pitch_type",
label = "Pitch Type:",
choices = c("CH", "CU", "EP", "FC",
"FF", "FO", "FS", "FT",
"KC", "KN", "SI", "SL"),
selected = "FF",
inline = TRUE),
checkboxGroupInput(inputId = "count",
label = "Count:",
choices = c("0-0", "1-0", "0-1", "2-0",
"1-1", "0-2", "3-0", "2-1", "1-2",
"3-1", "2-2", "3-2"),
selected = "0-0",
inline = TRUE)
)),
column(8,
plotOutput(
outputId = "plot",
height = '540px')
)
)
server <- function(input, output, session) {
output$plot <- renderPlot({
req(length(input$count) > 0 &
nchar(input$pid) > 0 &
length(input$pitch_type) > 0)
add_zone <- function(color = "black"){
topKzone <- 3.5
botKzone <- 1.6
inKzone <- -0.85
outKzone <- 0.85
kZone <- data.frame(
x=c(inKzone, inKzone, outKzone, outKzone, inKzone),
y=c(botKzone, topKzone, topKzone, botKzone, botKzone)
)
geom_path(aes(.data$x, .data$y),
data=kZone, lwd = 1, color = color)
}
sc_new <- filter(sc_pitcher_2019,
Count %in% input$count,
pitch_type %in% input$pitch_type,
pitcher == as.numeric(input$pid))
ggplot() +
geom_point(data = sc_new,
aes(plate_x, plate_z),
size = 0.8, color = "red") +
add_zone() +
coord_equal() +
xlim(-2.5, 2.5) +
ylim(0, 5) +
facet_grid(Count ~ pitch_type) +
labs(title = paste("2019 Pitcher", input$pid))
}, res = 96)
}
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment