title | subtitle | author | job | framework | highlighter | hitheme | widgets | mode | knit |
---|---|---|---|---|---|---|---|---|---|
MLB Run Scoring |
A Shiny web app |
Martin Monkman / 2015-01-14 |
io2012 |
highlight.js |
tomorrow |
selfcontained |
slidify::knit2slides |
- Analysis
- Storyboard
- Development
- Publishing
(repeat steps 2-4 ...)
--- .class #id
Already undertaken some static analysis ... see my blog
bayesball.blogspot.com under the label "run scoring"
Using:
- Lahman database package
- R analytic and graphic packages
- dplyr
- ggplot2
--- .class #id
[INSERT IMAGE OF PAPER DRAWING]
--- .class #id
Incremental!
One page at a time, and then looping back to add features (I am by no means finished!)
--- .class #id
Using dplyr to create summary table
MLB_RPG <- Teams %>%
filter(yearID > 1900, lgID != "FL") %>%
group_by(yearID) %>%
summarise(R=sum(R), RA=sum(RA), G=sum(G)) %>%
mutate(leagueRPG=R/G, leagueRAPG=RA/G)
tail(MLB_RPG)
## Source: local data frame [6 x 6]
##
## yearID R RA G leagueRPG leagueRAPG
## 1 2008 22585 22585 4856 4.650947 4.650947
## 2 2009 22419 22419 4860 4.612963 4.612963
## 3 2010 21308 21308 4860 4.384362 4.384362
## 4 2011 20808 20808 4858 4.283244 4.283244
## 5 2012 21017 21017 4860 4.324486 4.324486
## 6 2013 20255 20255 4862 4.165981 4.165981
--- .class #id
MLBRPG <- ggplot(MLB_RPG, aes(x=yearID, y=leagueRPG)) +
geom_point() + xlim(1901, 2013) + ylim(3, 6) + xlab("year") + ylab("runs per game") +
ggtitle(paste("Major League Baseball: runs per team per game 1901-2013"))
MLBRPG
--- .class #id
The user interface: ui.r
- defines page layout
- contains the instructions for the widgets
The server file: server.r
- where the "analysis" is written, e.g. the plot instructions
--- .class #id
The user interface: ui.r
library(shiny)
# Define UI layout using "fluidPage"
shinyUI(fluidPage(
# Application title
titlePanel("MLB run scoring trends"),
# Sidebar with a slider input for the number of bins
sidebarLayout(
sidebarPanel(
h3("this is the sidebar")
),
# Show a plot of run scoring trends
mainPanel(
plotOutput("MLBRPG")
)
)
))
--- .class #id
The server file: server.r
# package load
library(shiny)
library(dplyr)
library(reshape)
library(ggplot2)
#
library(Lahman)
# load the Lahman data table "Teams", filter
data(Teams)
# select a sub-set of teams from 1901 [the establishment of the American League] forward to most recent year
MLB_RPG <- Teams %>%
filter(yearID > 1900, lgID != "FL") %>%
group_by(yearID) %>%
summarise(R=sum(R), RA=sum(RA), G=sum(G)) %>%
mutate(leagueRPG=R/G, leagueRAPG=RA/G)
#
shinyServer(function(input, output) {
output$plot_MLBtrend <- renderPlot({
MLBRPG <- ggplot(MLB_RPG, aes(x=yearID, y=leagueRPG)) +
geom_point() + xlim(1901, 2013) + ylim(3, 6) + xlab("year") + ylab("runs per game") +
ggtitle(paste("Major League Baseball: runs per team per game 1901-2013"))
MLBRPG
})
})
--- .class #id
slidifyUI(fluidPage(
# Application title
titlePanel("MLB run scoring trends"),
# Sidebar with a slider input for the number of bins
sidebarLayout(
sidebarPanel(
h3("this is the sidebar")
),
# Show a plot of run scoring trends
mainPanel(
plotOutput("MLBRPG")
)
)
))
## Error in eval(expr, envir, enclos): could not find function "slidifyUI"
# package load
library(shiny)
## Warning: package 'shiny' was built under R version 3.1.2
library(dplyr)
library(reshape)
library(ggplot2)
#
library(Lahman)
# load the Lahman data table "Teams", filter
data(Teams)
# select a sub-set of teams from 1901 [the establishment of the American League] forward to most recent year
MLB_RPG <- Teams %>%
filter(yearID > 1900, lgID != "FL") %>%
group_by(yearID) %>%
summarise(R=sum(R), RA=sum(RA), G=sum(G)) %>%
mutate(leagueRPG=R/G, leagueRAPG=RA/G)
#
shinyServer(function(input, output) {
output$plot_MLBtrend <- renderPlot({
MLBRPG <- ggplot(MLB_RPG, aes(x=yearID, y=leagueRPG)) +
geom_point() + xlim(1901, 2013) + ylim(3, 6) + xlab("year") + ylab("runs per game") +
ggtitle(paste("Major League Baseball: runs per team per game 1901-2013"))
MLBRPG
})
})
--- .class #id
Reactive values - passed between the two files ...
... click a button on the screen, the value is captured in the ui.r file, passed back to the server.r file, where it is used to recalculate the output, which is passed back to the ui.r file and displayed on the screen
Example: the trend line
ui.r code -- create a checkbox that will return the value "trendlineselect"
checkboxInput("trendlineselect", label = h4("Add a trend line"), value = FALSE),
server.r code -- evaluates "trendlineselect", and if it's true, add the "stat_smooth" (trendline) option to the chart
if (input$trendlineselect == TRUE) {
MLBRPG <- MLBRPG +
stat_smooth(method=loess,
span=input$trendline_sen_sel,
level=as.numeric(input$trendline_conf_sel))
}
--- .class #id
--- .class #id
--- .class #id
--- .class #id
--- .class #id
RStudio will host your Shiny app
Here's mine: https://monkmanmh.shinyapps.io/MLBrunscoring_shiny/
--- .class #id
github repository "MLBrunscoring_shiny"
-30-