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){
  Teams.recent = startYear &
                           yearID <= endYear)
  Teams.recent$ <-
      Teams.recent[, variable] / Teams.recent[, "G"]
  print(ggplot(Teams.recent, aes(yearID, +
          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)


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 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:


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):



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):

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

6 responses

  1. Read my mind – would love to start using Shiny effectively. One question: which of the examples did you build off of?

  2. Tony, I think I started with the tutorial at and then started making modifications to their example to get started. Although I don’t have a great understanding of the new functions, it all seems pretty straightforward.

  3. Okay thank you

  4. I’m back with an updated question! Appreciate your help. When you uploaded to the Shiny server, did you use a non-traditional OS to do so? Quite surprised that the tool doesn’t support Windows or Mac.

  5. Tony, I had no problems uploading to the Shiny server from my mac. But the availability of the server may have recently changed, so I’m not sure about the current setup.

  6. Jim,

    I took a little different approach to look at individual players, as well as the league average for each statistic.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: