Developing an R Shiny Application with Statcast Data

Creating a Script

Necessary First Steps

library(shiny)
library(dplyr)
library(ggplot2)

NL_CY <- read.csv("NL_CY.csv")

Building the User Interface

ui <- fluidPage(
titlePanel(),
sidebarLayout(
sidebarPanel(),
mainPanel()
)
)
sidebarPanel(
selectInput(
inputId = "PitcherInput",
label = "Select Pitcher",
choices = sort(unique(NL_CY$player_name))),
dateRangeInput(
inputId = "DateRangeInput",
label = "Select Date Range",
start = min(NL_CY$game_date),
end = max(NL_CY$game_date)),
img(src = "ss_logo.png",
style = "display: block; margin-left: auto;
margin-right: auto;",
height = 150,
width = 150)
)
mainPanel(
tabsetPanel(
tabPanel("Pitch Usage - Bar Chart", br(),
plotOutput("barchart")),
tabPanel("Pitch Velocity - Box Plot", br(),
plotOutput("boxplot")),
tabPanel("Pitch Velocity Trend - Line Plot", br(),
plotOutput("lineplot"))
)
)

Writing Server Code

server <- function(input, output) {
output$plot1 <- renderPlot({
dataFilter <- reactive({})
ggplot()
})
}
output$barchart <- renderPlot({
dataFilter <- reactive({
NL_CY %>%
filter(player_name == input$PitcherInput,
between(game_date, input$DateRangeInput[1],
input$DateRangeInput[2])) %>%
group_by(pitch_name) %>%
summarize('count' = n())
})
ggplot(dataFilter(),
aes(x = reorder(pitch_name, -count),
y = count,
fill = pitch_name)) +
geom_bar(stat = "identity") +
labs(x = "Pitch Type",
y = "Count",
title = "Pitch Usage") +
theme_bw() +
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5,
face = "bold",
size = 16)) +
theme(axis.title = element_text(size = 14),
axis.text = element_text(size = 12))
}, width = 850, height = 450)

Tying It All Together

shinyApp(ui = ui, server = server)

Additional Resources

--

--

--

Driveline Baseball Operations Analyst

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Hello, I am a Developer Advocate

How FaunaDB Saves JAMstack Developers from Data Loss

Developing Automated KYC Verification

How to calculate unit economics for Kubernetes and traditional workloads

Everything You Need To Know About Kafka

GitFlow encourages Continuous Integration

10 Examples to Master Python Tuples

Retrieve and Analyze DeFi Data With Keyko’s Python Library

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sam Bornstein

Sam Bornstein

Driveline Baseball Operations Analyst

More from Medium

Jakarta Odd-Even Road Rule: Odd Chances

Cozy Collecting, Part 1: Pinging an API & Creating JSON in R

Exploratory Analysis of NYC Airbnb Data via R

Association rules mining in R