Skip to Content
Course content

217: Capstone: Building a Public Health Surveillance Dashboard

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

Listen, before we dive into the code for this capstone, I want to clear something up. I've seen a lot of developers approach their first dashboard by thinking: "A dashboard is just a collection of a few ggplot2 graphs put on a webpage."

If that were true, you'd just be making a website with screenshots. In a real public health surveillance context—say, tracking a spike in Influenza-like Illnesses (ILI) across several counties—that approach fails the moment a user interacts with the data. If you put your data filtering logic directly inside your renderPlot function, R will re-calculate that entire filter every single time any single element on the page changes. When your dataset grows from a few hundred rows to a few hundred thousand, your dashboard will lag, freeze, and eventually crash. That's not a tool; it's a liability.

The Mistake: Treating Reactivity as a Plotting Detail

When people treat the dashboard as a "gallery," they write code that looks like this:

# The "Slow" Way
output$casePlot <- renderPlot({
  # Filtering happens INSIDE the render function
  filtered_data <- all_cases %>% 
    filter(region == input$region_select, 
           date >= input$date_range[1], 
           date <= input$date_range[2])
  
  ggplot(filtered_data, aes(x = date, y = cases)) + geom_line()
})

Now, imagine you have three different plots and a summary table all using that same filtered data. In the "Slow Way," R performs that filter() operation four separate times every time the user moves the date slider. It's redundant and wasteful. I've seen this crash production servers during actual health crises because the developer didn't account for the computational overhead of redundant filtering.

The Correction: Building a Reactive Data Pipeline

The secret to a professional surveillance dashboard is the reactive() expression. Think of it as a "cached" version of your data that only updates when its specific dependencies change. You create a single source of truth for your filtered data, and then your plots simply "consume" that truth.

Here is how we're actually going to structure the capstone project:

# The "Professional" Way
# 1. Create a reactive expression for the filtered dataset
filtered_data <- reactive({
  # This only runs when input$region_select or input$date_range changes
  all_cases %>% 
    filter(region == input$region_select, 
           date >= input$date_range[1], 
           date <= input$date_range[2])
})

# 2. The plots now just call the reactive expression as a function
output$casePlot <- renderPlot({
  ggplot(filtered_data(), aes(x = date, y = cases)) + geom_line()
})

output$summaryTable <- renderTable({
  filtered_data() %>% summarize(total = sum(cases))
})

Notice the parentheses in filtered_data()? That's the tell-tale sign of a reactive object. By separating the data processing from the data visualization, the dashboard stays snappy. For a public health official trying to identify an outbreak in real-time, those few seconds of latency can be the difference between a useful tool and a discarded one.

In this capstone, we aren't just plotting lines; we are building a pipeline. We'll be using bslib to give it a modern, "government-grade" look, and we'll be implementing observeEvent to handle specific alerts—like triggering a warning notification when the case count exceeds a predefined epidemiological threshold.




📋 Practical Task

Implementing a Region-Specific Outbreak Alert Trigger in the Surveillance Dashboard

You have been provided with a basic Shiny app skeleton that tracks disease cases. Currently, the app displays a plot and a table, but it doesn't proactively warn the user if a region has entered an "outbreak" state (defined as more than 500 cases in the selected time window).

Your Task:

  • Create a reactive expression that calculates the total sum of cases for the currently selected region and date range.
  • Add a uiOutput element to the UI named outbreak_alert.
  • Implement a renderUI function on the server side that checks the total cases. If the total is greater than 500, it should return a div with a red background and the text "WARNING: Outbreak Threshold Exceeded!". If it is below 500, it should return an empty div or a green "Stable" message.
  • Ensure that the alert updates instantly when the region or date range is changed.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.