Monthly Archives: January, 2014

Visualizing Baseball History using Shiny

Baseball has a fascinating history and one can quickly look at the history of the rates of different offensive events by graphing Lahman team data using the ggplot2 package. I’ll use this to illustrate the use of the RStudio shiny package — this allows one to easily write gui web interfaces for R work.

I start with a function baseball.history to graph the number of team runs scored per game against season. The inputs are the time frame (startYear and endYear), the variable of interest (variable) and a parameter controlling the smoothness of a smoothing curve. Here’s the function with an example.

baseball.history <-
  function(startYear, endYear, variable, smooth.fraction=0.3){
  require(Lahman)
  require(ggplot2)
  Teams.recent = startYear &
                           yearID <= endYear)
  Teams.recent$stat.game <-
      Teams.recent[, variable] / Teams.recent[, "G"]
  print(ggplot(Teams.recent, aes(yearID, stat.game)) +
          geom_point() +
          geom_smooth(size=2, color="red", method="loess",
                      span=smooth.fraction) +
          xlab("YEAR") + ylab("RATE PER GAME PER TEAM") )
}

baseball.history(1901, 2012, "R", smooth.fraction=0.5)

shiny1

It is interesting that run scoring has remained pretty constant throughout baseball history, although run scoring seemed to peak recently during the “Steriods Era”.

Suppose I want to construct a friendly user interface for this graph where the user can select (1) the offensive measure to graph (H, R, HR, etc), (2) the time interval to consider (maybe we want to focus on home run hitting in the modern era after 1960) and (3) the smoothness of the smooth (sometimes we want to adjust the value used in the loess smoother).

I am not a Shiny expert, but it was easy to modify one of the tutorial examples for my purpose. Here is what I did. In the working directory, create a new folder that I called history with two R files ui.R and server.R . You can see these files at https://gist.github.com/bayesball/8293777 Essentially, the file ui.R defines the user interface (sliders, menus, etc), and the file server.R contains the R code to do the work.

I’ll illustrate three ways of sharing a shiny web application.  Once you have the two files ui.R and server.R created in the folder history and you have already installed the shiny package, then all you do to run this application is to enter:

library(shiny)
runApp("history")

I have already shared these files on github gist.  A second way to run this application is to install the devtools package and run my code by the runGist function (I’ve added a snapshot of what you’ll see):

library(devtools)
shiny::runGist(8293777)

shiny2

Even easier, a third way is to visit the web application that I uploaded to the Shiny server (one needs to set up an account with RStudio):

http://bayesball.shinyapps.io/baseballhistory/

Anyway, I think Shiny looks like a very nice way to share R work, and I think I’ll be using it for some of my classes.  RStudio has good documentation for the shiny package at http://www.rstudio.com/shiny/.

Advertisements