Skip to Content
Course content

93: Practice Exercise: Building an Interactive Dashboard with Real Data

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Alright, let's get our hands dirty. We've talked about the theory of reactivity in Shiny, but it usually doesn't "click" until you're staring at a blank screen wondering why your plot isn't updating. For this session, I've got the palmerpenguins dataset loaded. It's a great little set of morphological data for three penguin species—perfect for a dashboard because it has categorical variables for filtering and numeric variables for plotting.

Getting a basic skeleton on the screen

I'll start by sketching out the simplest possible app. I want a dropdown to pick a species and a plot to show the relationship between bill length and bill depth. I'll just throw everything into a fluidPage for now. I'm not worrying about layout yet; I just want to see if the plumbing works.

library(shiny)
library(ggplot2)
library(palmerpenguins)

ui <- fluidPage(
  selectInput("species", "Choose a Species:", choices = unique(penguins$species)),
  plotOutput("penguinPlot")
)

server <- function(input, output) {
  output$penguinPlot <- renderPlot({
    ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm)) +
      geom_point()
  })
}

shinyApp(ui = ui, server = server)

I run this, and... it works, but it's useless. The plot shows up, but when I change the species in the dropdown, nothing happens. The plot stays static. Why? Because I've called the penguins dataframe directly inside renderPlot. I told Shiny to draw the whole dataset, and I never actually told it to look at input$species. I'm basically ignoring the user.

The "Aha!" moment with reactivity

Now I need to filter the data based on that input. My first instinct is usually to just put the filter right inside the renderPlot function. Let's try that.

server <- function(input, output) {
  output$penguinPlot <- renderPlot({
    # Filter the data based on the UI input
    filtered_data <- penguins[penguins$species == input$species, ]
    
    ggplot(filtered_data, aes(x = bill_length_mm, y = bill_depth_mm)) +
      geom_point(color = "steelblue") +
      labs(title = paste("Analyzing", input$species))
  })
}

That works. Now the plot updates. But here's the problem: as this app grows, I'm probably going to want that filtered data in three different places—maybe a plot, a summary table, and a text description. If I put the filtering logic inside renderPlot, I'd have to copy-paste that filtering line into every single output function. That's a maintenance nightmare.

This is where reactive() comes in. Instead of filtering inside the output, I'll create a reactive expression. Think of it as a "smart" variable that only recalculates when its dependencies (the input) change.

Structuring for scale

Let's refactor this. I'll pull the filtering logic out into its own reactive block. One thing to watch out for here: remember that reactive expressions are functions, so when I call them later, I have to use parentheses—filtered_data(), not filtered_data.

server <- function(input, output) {
  # This is the 'source of truth' for our filtered data
  filtered_data <- reactive({
    penguins[penguins$species == input$species, ]
  })

  output$penguinPlot <- renderPlot({
    # Notice the parentheses! We are calling the reactive expression.
    ggplot(filtered_data(), aes(x = bill_length_mm, y = bill_depth_mm)) +
      geom_point(aes(color = island)) + 
      theme_minimal() +
      labs(title = paste("Bill Dimensions for", input$species))
  })
}

I added color = island to the aesthetic mapping just to see if it helps separate the data. Now, the flow is much cleaner: input$species triggers filtered_data(), which in turn triggers renderPlot. If I add a table output tomorrow, I just call filtered_data() again without redefining the filter logic. It's a much more professional way to handle data flow in R.




📋 Practical Task

Build the Palmer Penguins Species Explorer

Your task is to expand the dashboard we started. Instead of just a plot, you need to create a more comprehensive exploration tool using the palmerpenguins dataset.

  • UI Requirements:
    • A sidebarLayout with a sidebar containing two inputs: a selectInput for species and a selectInput for island.
    • A main panel containing both a plotOutput and a tableOutput.
  • Server Requirements:
    • Create a single reactive() expression that filters the penguins dataset by both the selected species and the selected island.
    • The renderPlot should display a scatter plot of flipper_length_mm vs body_mass_g, colored by species.
    • The renderTable should display the first 10 rows of the filtered dataset.

Challenge: Ensure that the table only shows the columns species, island, bill_length_mm, and body_mass_g to keep the dashboard clean.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.