Skip to content

Instantly share code, notes, and snippets.

@bayesball
Last active August 29, 2015 13:56
Show Gist options
  • Save bayesball/9053425 to your computer and use it in GitHub Desktop.
Save bayesball/9053425 to your computer and use it in GitHub Desktop.
Shiny application to plot a baseball hitter's career trajectory
# setup work
library(Lahman)
library(dplyr)
# create new data frame Batting.new by
# collapsing over stint variable
Batting.new <- summarise(group_by(Batting, playerID, yearID),
AB = sum(AB),
H = sum(H),
X2B = sum(X2B),
X3B = sum(X3B),
HR = sum(HR),
SB = sum(SB),
BB = sum(BB),
SO = sum(SO),
HBP = sum(HBP),
SF = sum(SF),
SH = sum(SH))
# recode SF and SH values to 0
myrecode <- function(data, var){
data[, var] <- ifelse(is.na(data[, var]), 0, data[, var])
data
}
Batting.new <- myrecode(Batting.new, "SF")
Batting.new <- myrecode(Batting.new, "SH")
# define plate appearance variable
Batting.new$PA <- with(Batting.new, AB + BB + HBP + SF + SH)
# add age variable
Master$birthyear <- with(Master,
ifelse(birthMonth >= 7, birthYear + 1, birthYear))
Batting.new <- merge(Batting.new,
Master[, c("playerID", "nameFirst", "nameLast", "birthyear")],
by="playerID")
Batting.new$Age <- with(Batting.new, yearID - birthyear)
# add first and last years variables
C.Years <- summarise(group_by(Batting, playerID),
fYear=min(yearID),
lYear=max(yearID))
Batting.new <- merge(Batting.new, C.Years, by="playerID")
# Define server logic required to plot various variables
# against year
shinyServer(function(input, output) {
# Compute the forumla text in a reactive function since it is
# shared by the output$caption and output$mpgPlot functions
formulaText <- reactive({
firstlast <- unlist(strsplit(input$playername," "))
playerids <- unique(subset(Batting.new,
nameFirst==firstlast[1] &
nameLast==firstlast[2])$playerID)
d <- subset(Batting.new, playerID==playerids[input$num])
paste(input$variable,"/",input$denom,
"Trajectory of ", input$playername,
d$fYear[1], "-", d$lYear[1])
})
output$caption <- renderText({
formulaText()
})
output$mpgPlot <- renderPlot({
require(ggplot2)
firstlast <- unlist(strsplit(input$playername," "))
playerids <- unique(subset(Batting.new,
nameFirst==firstlast[1] &
nameLast==firstlast[2])$playerID)
d <- subset(Batting.new, playerID==playerids[input$num])
d$Rate <- d[, input$variable] / d[, input$denom]
print(ggplot(d, aes(Age, Rate)) +
geom_point(size=5, color="red") +
geom_smooth(method="loess", size=3) +
theme(axis.text = element_text(size = rel(2))) +
theme(axis.title = element_text(size = rel(2))) +
theme(plot.title = element_text(size = rel(2)))
)
}, width=600, height=500)
})
library(shiny)
shinyUI(pageWithSidebar(
# Application title
headerPanel("Career Trajectories"),
# Sidebar with controls to select the variable to plot against mpg
# and to specify whether outliers should be included
sidebarPanel(
textInput("playername", "Player Name:", value = "Mickey Mantle"),
numericInput("num", "Number:", 1),
selectInput("variable", "Variable:",
list("Home Run Rate" = "HR",
"Batting Average" = "H",
"Strikeout Rate" = "SO",
"Doubles Rate" = "X2B",
"Triples Rate" = "X3B",
"Walk Rate" = "BB",
"Hit By Pitch Rate" = "HBP"
)),
selectInput("denom", "Denominator:",
list("At Bats" = "AB",
"Plate Appearances" = "PA"
))
),
# Show the caption and plot of the requested variable against mpg
mainPanel(
h3(textOutput("caption")),
plotOutput("mpgPlot")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment