AE 21: Interactive web apps with Shiny
Application exercise
01_first-app
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)02_minmax-table
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"),
# fill in: an output placeholder for a table
____(outputId = "____")
)
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))
})
# fill in: the output id and the render*() function that matches the
# placeholder above
output$____ <- ____({
d |>
filter(name %in% ____) |>
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)03_dashboard
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)Acknowledgments
- Materials derived from posit::conf 2025 - Shiny for R Workshop by Colin Rundel and licensed under a Creative Commons Attribution 4.0 International License.