
Lecture 23
Cornell University
INFO 5001 - Fall 2026
November 17, 2026
ae-21Instructions
ae-21 (repo name will be suffixed with your GitHub name).renv::restore() to install the required packages, open the Quarto document in the repo, and follow along and complete the exercises.🔓 Decrypt the .Renviron.secret → .Renviron
source("secret.R")info-5001shiny-bslib: layouts, cards, value boxes, sidebars, navigationshiny-bslib-theming: themes, colors, fonts, dark modeInstructions
.Renviron.secret (see the AE instructions), then restart R so the keys loadSys.getenv("ANTHROPIC_API_KEY") in the consolePosit Assistant stores the key in your system keychain, not in your project. The course OpenAI key works the same way.
05:00
Every Shiny app has a webpage that the user visits,
and behind this webpage there is a computer that serves this webpage by running R (or Python!).
When running your app locally, the computer serving your app is your computer.
When your app is deployed, the computer serving your app is a web server.
The chat apps from the LLM exercises (Clearbot demo, the quiz game) were Shiny apps built with {shinychat}.
Today we write one from scratch.


Source: Chapter 1 - Dataclysm, by Christian Rudder

Learn more at https://shiny.posit.co/
Modern UI toolkit for Shiny based on Bootstrap
Creation of delightful and customizable Shiny dashboards with cards, value boxes, sidebars, etc.
Use of modern versions of Bootstrap and Bootswatch

Learn more at https://rstudio.github.io/bslib
is now the officially recommended way to build Shiny apps.
camelCasesnake_casefluidPage() + sidebarLayout() from {shiny}. Both work; we use the {bslib} versions.Every Shiny app has two main components:
A UI (user interface) which controls how the app looks
and a server function which controls how the app works.
# A tibble: 23,376 × 17
id name airport_code country state region longitude latitude date temp_avg temp_min
<dbl> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <date> <dbl> <dbl>
1 72202 Miami KMIA US FL South -80.3 25.8 2022-01-01 24.2 21.1
2 72202 Miami KMIA US FL South -80.3 25.8 2022-01-02 24.4 21.7
3 72202 Miami KMIA US FL South -80.3 25.8 2022-01-03 22.7 17.8
4 72202 Miami KMIA US FL South -80.3 25.8 2022-01-04 20.4 16.7
5 72202 Miami KMIA US FL South -80.3 25.8 2022-01-05 22.9 19.4
6 72202 Miami KMIA US FL South -80.3 25.8 2022-01-06 21.4 16.7
7 72202 Miami KMIA US FL South -80.3 25.8 2022-01-07 23.6 20.6
# ℹ 23,369 more rows
# ℹ 6 more variables: temp_max <dbl>, precip <dbl>, snow <dbl>, wind_direction <dbl>,
# wind_speed <dbl>, air_press <dbl>
💾 _demos/01_first-app.R
library(tidyverse)
library(shiny)
library(bslib)
d <- read_csv(here::here("data/weather.csv"))
ui <- page_sidebar(
title = "Temperatures at Major Airports",
sidebar = sidebar(
radioButtons(
inputId = "name",
label = "Select an airport",
choices = c(
"Hartsfield-Jackson Atlanta",
"Raleigh-Durham",
"Denver",
"Los Angeles",
"John F. Kennedy"
)
)
),
plotOutput(outputId = "plot")
)
server <- function(input, output, session) {
output$plot <- renderPlot({
d |>
filter(name %in% input$name) |>
ggplot(mapping = aes(x = date, y = temp_avg)) +
geom_line()
})
}
shinyApp(ui = ui, server = server)01_first-appInstructions
Open _exercises/01_first-app.R and run it with the Run App button. Pick a few airports to check it works.
Then modify the code:
weather.csv (e.g. "Ithaca"). What happens?radioButtons() with selectInput(multiple = TRUE) or checkboxGroupInput(). Does the plot still make sense with more than one airport? How could you fix it?06:00
| Function | Outputs |
|---|---|
plotOutput() |
plot |
tableOutput() |
table |
uiOutput() |
Shiny UI element |
textOutput() |
text |
*Output() \(\rightarrow\) render*()| Output function | Render function |
|---|---|
plotOutput() |
renderPlot({}) |
tableOutput() |
renderTable({}) |
uiOutput() |
renderUI({}) |
textOutput() |
renderText({}) |
The *Output() goes in the ui. The matching render*() goes in the server(), assigned to output$<id>.
💾 _demos/01_first-app.R
library(tidyverse)
library(shiny)
library(bslib)
d <- read_csv(here::here("data/weather.csv"))
ui <- page_sidebar(
title = "Temperatures at Major Airports",
sidebar = sidebar(
radioButtons(
inputId = "name",
label = "Select an airport",
choices = c(
"Hartsfield-Jackson Atlanta",
"Raleigh-Durham",
"Denver",
"Los Angeles",
"John F. Kennedy"
)
)
),
plotOutput(outputId = "plot")
)
server <- function(input, output, session) {
output$plot <- renderPlot({
d |>
filter(name %in% input$name) |>
ggplot(mapping = aes(x = date, y = temp_avg)) +
geom_line()
})
}
shinyApp(ui = ui, server = server)Our inputs and outputs are defined by the elements in our ui.
💾 _demos/01_first-app.R
library(tidyverse)
library(shiny)
library(bslib)
d <- read_csv(here::here("data/weather.csv"))
ui <- page_sidebar(
title = "Temperatures at Major Airports",
sidebar = sidebar(
radioButtons(
inputId = "name",
label = "Select an airport",
choices = c(
"Hartsfield-Jackson Atlanta",
"Raleigh-Durham",
"Denver",
"Los Angeles",
"John F. Kennedy"
)
)
),
plotOutput(outputId = "plot")
)
server <- function(input, output, session) {
output$plot <- renderPlot({
d |>
filter(name %in% input$name) |>
ggplot(mapping = aes(x = date, y = temp_avg)) +
geom_line()
})
}
shinyApp(ui = ui, server = server)The reactive logic is defined in our server() function - {shiny} takes care of figuring out what depends on what.
💾 _demos/02_add-input.R
library(tidyverse)
library(shiny)
library(bslib)
d <- read_csv(here::here("data/weather.csv"))
d_vars <- c(
"Average temp" = "temp_avg",
"Min temp" = "temp_min",
"Max temp" = "temp_max",
"Total precip" = "precip",
"Snow depth" = "snow",
"Wind direction" = "wind_direction",
"Wind speed" = "wind_speed",
"Air pressure" = "air_press"
)
ui <- page_sidebar(
title = "Weather at Major Airports",
sidebar = sidebar(
radioButtons(
inputId = "name",
label = "Select an airport",
choices = c(
"Raleigh-Durham",
"Houston Intercontinental",
"Denver",
"Los Angeles",
"John F. Kennedy"
)
),
selectInput(
inputId = "var",
label = "Select a variable",
choices = d_vars,
selected = "temp_avg"
)
),
plotOutput(outputId = "plot")
)
server <- function(input, output, session) {
output$plot <- renderPlot({
d |>
filter(name %in% input$name) |>
ggplot(mapping = aes(x = date, y = .data[[input$var]])) +
geom_line() +
labs(title = str_c(input$name, " - ", input$var))
})
}
shinyApp(ui = ui, server = server).data pronouninput$var holds a column name as a string (e.g. "temp_avg"). .data[[ ]] from {rlang} lets {dplyr} and {ggplot2} use that string as a column.
More information: .data and .env pronouns
With these additions, what does our reactive graph look like now?
02_minmax-tableInstructions
_exercises/02_minmax-table.R is demo 02 plus the code for a table of the min and max average temperature for each year. Fill in the ____ blanks to add it to the app:
ui: the output placeholder for a table, with the id minmaxserver(): the matching output$ id and render*() functionRun the app and switch airports. Does the table update? Does it change when you pick a new variable? Why or why not?
05:00
reactive()Our solution repeats the filtering of d to the selected airport, once for each output.
Not a big deal here, but it adds up in larger apps: more code to keep in sync, and the same work done twice.
💾 _solutions/02_minmax-table.R
library(tidyverse)
library(shiny)
library(bslib)
d <- read_csv(here::here("data/weather.csv"))
d_vars <- c(
"Average temp" = "temp_avg",
"Min temp" = "temp_min",
"Max temp" = "temp_max",
"Total precip" = "precip",
"Snow depth" = "snow",
"Wind direction" = "wind_direction",
"Wind speed" = "wind_speed",
"Air pressure" = "air_press"
)
ui <- page_sidebar(
title = "Weather at Major Airports",
sidebar = sidebar(
radioButtons(
inputId = "name",
label = "Select an airport",
choices = c(
"Raleigh-Durham",
"Houston Intercontinental",
"Denver",
"Los Angeles",
"John F. Kennedy"
)
),
selectInput(
inputId = "var",
label = "Select a variable",
choices = d_vars,
selected = "temp_avg"
)
),
plotOutput(outputId = "plot"),
# new output placeholder
tableOutput(outputId = "minmax")
)
server <- function(input, output, session) {
output$plot <- renderPlot({
d |>
filter(name %in% input$name) |>
ggplot(mapping = aes(x = date, y = .data[[input$var]])) +
geom_line() +
labs(title = str_c(input$name, " - ", input$var))
})
# new render function: depends on input$name only
output$minmax <- renderTable({
d |>
filter(name %in% input$name) |>
mutate(year = year(date) |> as.integer()) |>
summarize(
`min avg temp` = min(temp_avg),
`max avg temp` = max(temp_avg),
.by = year
)
})
}
shinyApp(ui = ui, server = server)reactive()💾 _demos/03_reactive.R
library(tidyverse)
library(shiny)
library(bslib)
d <- read_csv(here::here("data/weather.csv"))
d_vars <- c(
"Average temp" = "temp_avg",
"Min temp" = "temp_min",
"Max temp" = "temp_max",
"Total precip" = "precip",
"Snow depth" = "snow",
"Wind direction" = "wind_direction",
"Wind speed" = "wind_speed",
"Air pressure" = "air_press"
)
ui <- page_sidebar(
title = "Weather at Major Airports",
sidebar = sidebar(
selectInput(
inputId = "name",
label = "Select airports",
choices = sort(unique(d$name)),
selected = "Denver",
multiple = TRUE
),
selectInput(
inputId = "var",
label = "Select a variable",
choices = d_vars,
selected = "temp_avg"
)
),
plotOutput(outputId = "plot"),
tableOutput(outputId = "minmax")
)
server <- function(input, output, session) {
# reactive conductor: filter once, use in both outputs
d_city <- reactive({
# stop quietly until at least one airport is selected
req(input$name)
d |>
filter(name %in% input$name)
})
output$plot <- renderPlot({
d_city() |>
ggplot(mapping = aes(x = date, y = .data[[input$var]], color = name)) +
geom_line() +
labs(color = NULL)
})
output$minmax <- renderTable({
d_city() |>
mutate(year = year(date) |> as.integer()) |>
summarize(
`min avg temp` = min(temp_avg),
`max avg temp` = max(temp_avg),
.by = c(name, year)
)
})
}
shinyApp(ui = ui, server = server)d_city is a reactive conductor: it sits between sources (inputs) and endpoints (outputs).
It depends on upstream inputs and returns a value that one or more downstream outputs (or other conductors) use.
Like a function in an R script, it helps you
reactive() tipsWritten like render*() functions, but assigned to a name instead of output$
react_obj(), not react_obj
Think of react_obj as a function that returns the current value
Forgetting the () causes the error
## Error: object of type 'closure' is not subsettableOnly usable inside reactive contexts (render*(), reactive(), observe())
## Error: Operation not allowed without an active reactive context.req()With selectInput(multiple = TRUE), the user can deselect every airport. Without protection, the app warns and draws empty output:
req() checks that a value is truthy and quietly stops the reactive if it isn’t.
A value is truthy if it is:
NULLFALSEcharacter(0), numeric(0), etc.)""NA
{bslib} provides modern UI components from Bootstrap, the same library that styles Quarto HTML documents.
Rectangular containers with borders and padding that group related information.
Display a key value with a label, often a dashboard’s headline metrics.
A value box’s value can be an output, rendered on the server like any other.
More layouts: https://shiny.posit.co/r/layouts/
shiny-bslib skill tells Posit Assistant to use modern {bslib} patterns instead of legacy fluidPage() codereactive() called d_city…”d_city reactive”03_dashboardInstructions
_exercises/03_dashboard.R is demo 03. Ask Posit Assistant to turn it into a {bslib} dashboard with:
Then audit the result:
d_city(), or filter d again?08:00
d_city()?fluidPage(), renderUI() for every box)?ui (how it looks) and a server() (how it works)render*() codereactive() conductors remove repetition and req() guards against incomplete inputs