Exercises

Data

{palmerpenguins}

For these exercises we’ll be using the penguins data from the {palmerpenguins} R package. After installing the package, you can load it into R:

library(palmerpenguins)
penguins

Exercises

Exercise 1: Building a basic app structure

  • Open up your IDE of choice.

  • Make sure you have the {shiny} package installed.

  • Create an app.R file.

  • Add ui and server elements, as well as shiny::shinyApp(ui, server).

  • Check it works.

Solution
app.R
library(shiny)

# UI ----------------------------------------------------------------------

ui <- fluidPage()


# Server ------------------------------------------------------------------

server <- function(input, output) {
  
}


# Run app -----------------------------------------------------------------

shiny::shinyApp(ui, server)

Exercise 2: Developing a user interface

  • Inside the fluidPage() function, add a title panel, sidebar, and main panel.

  • Add a drop down menu with selectInput() to choose by Island.

  • What happens if you use checkboxGroupInput() instead?

  • Bonus: add additional user options! See: mastering-shiny.org/basic-ui.html

Solution
app.R
library(shiny)
library(palmerpenguins)

# Penguin types
species_choices <- unique(penguins$species)

# UI ----------------------------------------------------------------------

ui <- fluidPage(
  titlePanel("Penguins!"),
  sidebarLayout(

    # Sidebar with user inputs
    sidebarPanel(
      # Select variable for plot
      selectInput(
        inputId = "species",
        label = "Choose a species:",
        choices = species_choices
      )
    ),

    # Display a plot of the generated distribution
    mainPanel()
  )
)


# Server ------------------------------------------------------------------

server <- function(input, output) {

}


# Run app -----------------------------------------------------------------

shiny::shinyApp(ui, server)

Exercise 3: Adding reactive elements

  • Add a reactive() in the server to subset the data based on the user input you created.

  • Create a bar chart of number of penguins per species. plotOutput() also works for {ggplot2} plots!

  • Check your app runs.

  • Bonus: the default Shiny app styling is fine. Install and load the {shinythemes} package. Try adding theme = shinytheme("cyborg") to the UI.

  • Browse: rstudio.github.io/shinythemes

Solution
app.R
library(shiny)
library(palmerpenguins)

# Penguin types
species_choices <- unique(penguins$species)

# UI ----------------------------------------------------------------------

ui <- fluidPage(
  titlePanel("Penguins!"),
  sidebarLayout(
    
    # Sidebar with user inputs
    sidebarPanel(
      # Select variable for plot
      selectInput(
        inputId = "species",
        label = "Choose a species:",
        choices = species_choices
      )
    ),
    
    # Display a plot of the generated distribution
    mainPanel(
      plotOutput("penguinsPlot")
    )
  )
)


# Server ------------------------------------------------------------------

server <- function(input, output) {
  
  filter_data <- reactive({
    penguins[penguins$species == input$species, ]
  })
  
  output$penguinsPlot <- renderPlot({
    plot(filter_data()$bill_length_mm, filter_data()$bill_depth_mm,
         main = glue::glue("Bill length and depth of {input$species} penguins"))
  })
  
}


# Run app -----------------------------------------------------------------

shiny::shinyApp(ui, server)