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("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)AE 21: Building a Shiny weather app
Application exercise
Exercise 01
Exercise 02
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("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)Exercise 03
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 Forecasts",
sidebar = sidebar(
radioButtons(
"name",
"Select an airport",
choices = c(
"Raleigh-Durham",
"Houston Intercontinental",
"Denver",
"Los Angeles",
"John F. Kennedy"
)
),
selectInput(
"var",
"Select a variable",
choices = d_vars,
selected = "temp_avg"
)
),
plotOutput("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)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.