Previous chapter
Shiny BasicsMovie App Exercise
Next chapter

Exercise 01: Create Movies App

Throughout the next sections we would like to create a Shiny app to display movie data from the Internet Movie Database (IMBD) and Rotten Tomatoes. The app uses the prepared dataset movies.Rdata which contains a random sample of 651 movies in the US between 1970 and 2014.

Note: All (movies) exercises are built upon each other and require you to re-use code from the previous ones. For example, Exercise 2 will require the code/solution from Exercise 1, Exercise 3 from Exercise 2 and so on. You shall run the exercises either locally (download of movies.Rdata required) or in the online workspace as indicated by the Start Workspace Button Image.

User Interface

  1. The user interface should use a sidebarLayout() containing dropboxes defined through selectInput() in the sidebarPanel() to specify the x- and y-axis of the plot. The plot (using ggplot()) is located in the mainPanel() and plots the selected variables against each other.

Tip: If you plot using ggplot() you need to use aes_string() if passing variables programmatically.

  1. The entire page should be surrounded by a fluidPage().

  2. The following variables should be used as valid choices for the selectInput component:

  • imdb_rating
  • imdb_num_votes
  • critics_score
  • audience_score
  • runtime
  1. Set audience_score as selected (default) for the y-axis and critics_score for the x-axis.

Next Steps

  1. Try to first run the exercise above based on the specifications ON YOUR OWN.
  2. If you run into issues you cannot resolve on your own check out the guided tour video in the next section.
  3. Only as a last resort check out the solution in the next section.

Guided Tour

Solution Exercise 01

User Interface

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("y", "Y-axis:", choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), selected = "audience_score"),
      selectInput("x", "X-axis:", choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), selected = "audience_score")
    ),
    mainPanel(
      plotOutput(outputId = "scatterplot")
    )
  )
)
## Error in fluidPage(sidebarLayout(sidebarPanel(selectInput("y", "Y-axis:", : could not find function "fluidPage"

Server

server <- function(input, output) {
  
  output$scatterplot <- renderPlot({
    ggplot(movies) + geom_point(aes_string(input$x, input$y))
  })

}

Exercise 02

Use the code from Exercise 01 and add a new dropdown (selectInput) menu to color the points. You can use the following parameters for selectInput:

inputId = "z"
label = "Color by:"
choices = c("title_type", "genre", "mpaa_rating", "critics_rating", "audience_rating")
selected = "mpaa_rating"
  • Use this variable in the aesthetics (aes_string) of the ggplot function as the color argument to color the points by input$z
  • Run the app in the Viewer Pane

Next Steps

  1. Try to first run the exercise above based on the specifications ON YOUR OWN.
  2. Only as a last resort check out the solution in the next section.

Solution Exercise 02

library(shiny)
library(ggplot2)
load("movies.Rdata")

# Define UI for application that plots features of movies -----------
ui <- fluidPage(
  
  # Sidebar layout with a input and output definitions --------------
  sidebarLayout(
    
    # Inputs: Select variables to plot ------------------------------
    sidebarPanel(
      
      # Select variable for y-axis ----------------------------------
      selectInput(inputId = "y", 
                  label = "Y-axis:",
                  choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), 
                  selected = "audience_score"),
      
      # Select variable for x-axis ----------------------------------
      selectInput(inputId = "x", 
                  label = "X-axis:",
                  choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), 
                  selected = "critics_score"),

      # Select variable for color -----------------------------------
      selectInput(inputId = "z", 
                  label = "Color by:",
                  choices = c("title_type", "genre", "mpaa_rating", "critics_rating", "audience_rating"),
                  selected = "mpaa_rating")
    ),
    
    # Output: Show scatterplot --------------------------------------
    mainPanel(
      plotOutput(outputId = "scatterplot")
    )
  )
)

# Define server function required to create the scatterplot ---------
server <- function(input, output) {
  
  # Create scatterplot object the plotOutput function is expecting --
  output$scatterplot <- renderPlot({
    ggplot(data = movies, aes_string(x = input$x, y = input$y,
                                     color = input$z)) +
      geom_point()
  })
}

# Run the application -----------------------------------------------
shinyApp(ui = ui, server = server)