DS4PS


This lab introduces the R Shiny package, which allows you to create dynamic graphics that ask for input from the user and change based upon the selection.

This lab builds on the previous lab, but you will now add the drop-down menu similar to the original NYT version, and highlight season statistics from that team (the yellow line).



Dynamic Graphics

Shiny allows you to build interactive components in graphics or dashboards. The terms “dynamic” or “interactive” mean specifically that a set of widgets are used to gather user input, and that input changes the analysis or visualization accordingly.

You can try it out using the Shiny Demo file or the example from the Shiny tutorial:

When you include “runtime: shiny” in the header, you do not need to load the shiny package in your document setup steps.

Note that running shiny creates an object called input which stores the values from your input widgets. Whatever you assign for the input widget ID will be the address for the value, which you can then access at input$your_id.

Shiny automatically creates an object called “input” for you. When you create a widget, shiny will perform the folowing assignment implicitly in the background:

Before the user selects a value from the dropdown menu, the “selected=” argument specifies the default value. In this case, input$my_widget_id would store the value “B” by default. When the user makes a new selection, that value is updated with whatever option the user selected.

In the rest of your code, you can then reference the user input value at:


Adding a Team Trend to the Graph

The solutions for last week’s lab include an example of how you can add a specific team to the graph. It generalizes the team by using a generic team.name variable that stores the value.

Note that in the subsequent code instead of using the specific team name for data steps and graphing functions it references the placeholder variable team.name. This enables you to change all of the team name references in one place, simplifying your code.

After you build your widget you can make the team selection dynamic by linking the user selection to your generic team.name variable.



Lab Instructions

This lab requires the following packages:

Note that you need to install packages before starting your lab. You cannot include an install commands inside of your RMD file.

You will likely use these packages as well:

Use the following template to complete this lab:

RMD Template



Step (1)

In the provided template, place your code from last week into the code chunk with the renderPlot() function:

Step (2)

Create a user-input widget on the left side bar by adding the proper values to these arguments:

Note that “choices=” will determine the teams visible in the drop-down box. You will be selecting from baseball team names.

  • Use the variable “name” in the dataset instead of teamID since it is more user-friendly.
  • Find a reasonable way to select specific teams so that you are only using 10-25 team names in the drop-down menu (see below for hints).
  • Combine your preferred teams into a character vector: choices=c(“team1”,“team2”,…,“teamN”).

Step (3)

Similar to the NYT graphic, add the team trend line to the graphic in orange.

Note that you would add a single team to the graphic something like this:

This code will also go inside the renderPlot() function in Step 1. You first need to replace the specific team name with the generalized input value from your selectInput() widget.



Notes on identifying team unique names.

Note there are 139 unique team names in the dataset:

## [1] "Boston Red Stockings"    "Chicago White Stockings"
## [3] "Cleveland Forest Citys"  "Fort Wayne Kekiongas"   
## [5] "New York Mutuals"        "Philadelphia Athletics"
## [1] 139

You do NOT want to use “choices=Teams$name” for the argument because each team name is repeated multiple times. This drop-down box would then have several thousand choices.

Instead select 10-25 teams, and include their full name in the choices list only once.

You might favor teams that have longer trend lines. The Cincinnati Reds and Pittsburgh Pirates have been in the dataset from the beginning, and the rest of the teams have been added along the way.

This table reports the total number of seasons they have each appeared:

name n
Cincinnati Reds 126
Pittsburgh Pirates 126
Philadelphia Phillies 125
St. Louis Cardinals 117
Chicago White Sox 116
Detroit Tigers 116
Chicago Cubs 114
Boston Red Sox 109
New York Yankees 104
Cleveland Indians 102

Some of these professional baseball teams you might not recognize because they only lasted for a short time:

name n
Rockford Forest Citys 1
Seattle Pilots 1
St. Louis Perfectos 1
St. Louis Red Stockings 1
St. Paul White Caps 1
Toledo Blue Stockings 1
Toledo Maumees 1
Washington Blue Legs 1
Washington Statesmen 1
Wilmington Quicksteps 1



Submission Instructions

Submit your RMD file but no HTML file this time. When you include “runtime: shiny” in your header, it launches a real-time app instead of generating an HTML file that can be shared.

Remember to: