AE 22: Building a Shiny weather app

Suggested answers

Application exercise
Answers
Modified

April 21, 2026

Exercise 04

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(
    selectInput(
      "region",
      label = "Select a region",
      choices = c("West", "Midwest", "Northeast", "South")
    ),
    selectInput(
      "name",
      label = "Select an airport",
      choices = c()
    ),
    selectInput(
      "var",
      label = "Select a variable",
      choices = d_vars,
      selected = "temp_avg"
    )
  ),
  plotOutput("plot"),
  tableOutput("minmax")
)

server <- function(input, output, session) {
  observe({
    updateSelectInput(
      session,
      "name",
      choices = d |>
        distinct(region, name) |>
        filter(region == input$region) |>
        pull(name)
    )
  })

  d_city <- reactive({
    req(input$name)

    d |>
      filter(name %in% input$name)
  })

  output$plot <- renderPlot({
    d_city() |>
      ggplot(mapping = aes(x = date, y = .data[[input$var]])) +
      geom_line() +
      labs(title = str_c(input$name, "-", input$var))
  })

  output$minmax <- renderTable({
    d_city() |>
      mutate(
        year = year(date) |> as.integer()
      ) |>
      summarize(
        `min avg temp` = min(temp_min),
        `max avg temp` = max(temp_max),
        .by = year
      )
  })
}

shinyApp(ui = ui, server = server)

Exercise 05

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(
    selectInput(
      "region",
      "Select a region",
      choices = c(),
    ),
    selectInput(
      "name",
      "Select an airport",
      choices = c()
    ),
    selectInput(
      "var",
      "Select a variable",
      choices = d_vars,
      selected = "temp"
    ),
    hr(),
    fileInput("upload", "Upload a file", accept = ".csv")
  ),
  plotOutput("plot")
)

server <- function(input, output, session) {
  d <- reactive({
    req(input$upload)
    ext = tools::file_ext(input$upload$datapath)

    validate(
      need(ext == "csv", "Please upload a csv file")
    )

    read_csv(input$upload$datapath)
  })

  d_city <- reactive({
    req(input$name)
    req(d())

    d() |>
      filter(name %in% input$name)
  })

  observe({
    updateSelectInput(
      inputId = "name",
      choices = d() |>
        filter(region == input$region) |>
        pull(name) |>
        unique() |>
        sort()
    )
  })

  observe({
    updateSelectInput(
      inputId = "region",
      choices = d() |>
        pull(region) |>
        unique() |>
        sort()
    )
  }) |>
    bindEvent(d())

  output$plot <- renderPlot({
    d_city() |>
      ggplot(mapping = aes(x = date, y = .data[[input$var]])) +
      ggtitle(input$var) +
      geom_line() +
      theme_minimal()
  })
}

shinyApp(ui = ui, server = server)

Acknowledgments