-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
93: Practice Exercise: Building an Interactive Dashboard with Real Data
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
sidebarLayoutwith a sidebar containing two inputs: aselectInputfor species and aselectInputfor island. - A main panel containing both a
plotOutputand atableOutput.
- A
- Server Requirements:
- Create a single
reactive()expression that filters thepenguinsdataset by both the selected species and the selected island. - The
renderPlotshould display a scatter plot offlipper_length_mmvsbody_mass_g, colored by species. - The
renderTableshould display the first 10 rows of the filtered dataset.
- Create a single
Challenge: Ensure that the table only shows the columns species, island, bill_length_mm, and body_mass_g to keep the dashboard clean.
There are no comments for now.