Skip to content

Instantly share code, notes, and snippets.

@bayesball
Last active August 29, 2015 13:56
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/8855992 to your computer and use it in GitHub Desktop.
Save bayesball/8855992 to your computer and use it in GitHub Desktop.
Shiny application to graph batting trajectories of baseball sluggers
library(shiny)
library(Lahman)
library(ggplot2)
# preliminary work
Master$birthyear <- with(Master,
ifelse(birthMonth >= 7, birthYear + 1, birthYear))
Batting <- merge(Batting,
Master[, c("playerID", "nameFirst", "nameLast", "birthyear")],
by="playerID")
Batting$Age <- with(Batting, yearID - birthyear)
# 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({
L <- subset(Master, playerID==input$player)$nameLast
F <- subset(Master, playerID==input$player)$nameFirst
paste(input$variable, "Trajectory of ", paste(F, L))
})
output$caption <- renderText({
formulaText()
})
output$mpgPlot <- renderPlot({
d <- subset(Batting, playerID==input$player)
d$stat <- d[, input$variable] / d[, "AB"]
myplot <- ggplot(d, aes(Age, stat)) +
geom_point(size=5, color="red") +
ylab(paste(input$variable, "RATE")) +
theme(axis.text = element_text(size = rel(2))) +
theme(axis.title = element_text(size = rel(3)))
if(input$smooth=="loess")
myplot <- myplot + geom_smooth(size=3, method="loess") else
myplot <- myplot +
stat_smooth(method = "lm", size = 3, formula = y ~ x + I(x ^ 2))
print(myplot)
}, width=600, height=500)
})
library(shiny)
shinyUI(pageWithSidebar(
# Application title
headerPanel("Career Trajectories of 500 Home Run Club"),
# Sidebar with controls to select the variable to plot against mpg
# and to specify whether outliers should be included
sidebarPanel(
selectInput("player", "Player:",
list("Hank Aaron" = "aaronha01",
"Ernie Banks" = "bankser01",
"Barry Bonds" = "bondsba01",
"Jimmie Foxx" = "foxxji01",
"Ken Griffey Jr" = "griffke02",
"Reggie Jackson" = "jacksre01",
"Harmon Killebrew" = "killeha01",
"Mickey Mantle" = "mantlmi01",
"Eddie Mathews" = "matheed01",
"Willie Mays" = "mayswi01",
"Willie McCovey" = "mccovwi01",
"Mark McGwire" = "mcgwima01",
"Eddie Murray" = "murraed02",
"Mel Ott" = "ottme01",
"Rafael Palmeiro" = "palmera01",
"Manny Ramirez" = "ramirma02",
"Frank Robinson" = "robinfr02",
"Alex Rodriguez" = "rodrial01",
"Babe Ruth" = "ruthba01",
"Mike Schmidt" = "schmimi01",
"Gary Sheffield" = "sheffga01",
"Sammy Sosa" = "sosasa01",
"Frank Thomas" = "thomafr04",
"Jim Thome" = "thomeji01",
"Ted Williams" = "willite01")),
selectInput("variable", "Variable:",
list("Home Run Rate" = "HR",
"Batting Average" = "H",
"Strikeout Rate" = "SO",
"Doubles Rate" = "X2B",
"Triples Rate" = "X3B",
"Walk Rate" = "BB")),
radioButtons("smooth", "Smoothing type:",
list("Loess" = "loess",
"Quadratic" = "lm"))
),
# 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