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(
theme = bs_theme(),
title = "Weather Data",
sidebar = sidebar(
selectInput(
"region",
"Select a region",
choices = c("West", "Midwest", "Northeast", "South")
),
selectInput(
"name",
"Select an airport",
choices = c()
),
selectInput(
"var",
"Select a variable",
choices = d_vars,
selected = "temp_avg"
)
),
card(
card_header(
textOutput("title")
),
card_body(
plotOutput("plot")
)
),
uiOutput("valueboxes")
)
server <- function(input, output, session) {
bslib::bs_themer()
observe({
updateSelectInput(
session,
"name",
choices = d |>
distinct(region, name) |>
filter(region == input$region) |>
pull(name)
)
})
output$valueboxes <- renderUI({
clean <- function(x) {
round(x, 1) |> paste("°C")
}
layout_columns(
value_box(
title = "Average Temp",
value = mean(d_city()$temp_avg, na.rm = TRUE) |> clean(),
showcase = bsicons::bs_icon("thermometer-half"),
theme = "success"
),
value_box(
title = "Minimum Temp",
value = min(d_city()$temp_min, na.rm = TRUE) |> clean(),
showcase = bsicons::bs_icon("thermometer-snow"),
theme = "primary"
),
value_box(
title = "Maximum Temp",
value = max(d_city()$temp_max, na.rm = TRUE) |> clean(),
showcase = bsicons::bs_icon("thermometer-sun"),
theme = "danger"
)
)
})
output$title <- renderText({
names(d_vars)[d_vars == input$var]
})
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() +
theme_minimal()
})
}
thematic::thematic_shiny()
shinyApp(ui = ui, server = server)Building a Shiny weather app (III)
Application exercise
Exercise 06
Exercise 07
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(
theme = bs_theme() |>
bs_theme_update(
base_font = font_google("Roboto Condensed"),
font_scale = 1.2,
`enable-shadows` = TRUE,
preset = "cerulean"
),
title = "Weather Data",
sidebar = sidebar(
selectInput(
"region",
"Select a region",
choices = c("West", "Midwest", "Northeast", "South")
),
selectInput(
"name",
"Select an airport",
choices = c()
),
selectInput(
"var",
"Select a variable",
choices = d_vars,
selected = "temp_avg"
)
),
card(
card_header(
textOutput("title")
),
card_body(
plotOutput("plot")
)
),
uiOutput("valueboxes")
)
server <- function(input, output, session) {
observe({
updateSelectInput(
session,
"name",
choices = d |>
distinct(region, name) |>
filter(region == input$region) |>
pull(name)
)
})
output$valueboxes <- renderUI({
clean <- function(x) {
round(x, 1) |> paste("°C")
}
layout_columns(
value_box(
title = "Average Temp",
value = mean(d_city()$temp_avg, na.rm = TRUE) |> clean(),
showcase = bsicons::bs_icon("thermometer-half"),
theme = "success"
),
value_box(
title = "Minimum Temp",
value = min(d_city()$temp_min, na.rm = TRUE) |> clean(),
showcase = bsicons::bs_icon("thermometer-snow"),
theme = "primary"
),
value_box(
title = "Maximum Temp",
value = max(d_city()$temp_max, na.rm = TRUE) |> clean(),
showcase = bsicons::bs_icon("thermometer-sun"),
theme = "danger"
)
)
})
output$title <- renderText({
names(d_vars)[d_vars == input$var]
})
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() +
theme_minimal()
})
}
thematic::thematic_shiny()
shinyApp(ui = ui, server = server)Exercise 08
library(tidyverse)
library(shiny)
library(bslib)
library(sf)
# Import data ----------------------------------------------------------------
# FEMA National Risk Index scores for every US county
climate_risk <- read_rds(here::here("data/climate-risk.rds"))
# county boundaries
county_sf <- st_read(here::here("data/counties.geojson"), quiet = TRUE)
# combine risk scores with county boundaries
climate_sf <- county_sf |>
left_join(
y = climate_risk,
by = join_by(GEOID == state_county_fips_code)
)
# hazard type columns, with human-readable names
hazard_types <- climate_risk |>
select(ends_with("_hazard_type_risk_index_score")) |>
colnames()
names(hazard_types) <- hazard_types |>
str_remove("_hazard_type_risk_index_score") |>
str_replace_all("_", " ") |>
str_to_title()
# Define UI -----------------------------------------------------------------
ui <- page_fillable()
# Define server ---------------------------------------------------------------
server <- function(input, output, session) {}
# Run the app
shinyApp(ui = ui, server = server)Exercise 09
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 Data",
sidebar = sidebar(
selectInput(
"region",
"Select a region",
choices = c("West", "Midwest", "Northeast", "South")
),
selectInput(
"name",
"Select an airport",
choices = c()
),
),
card(
card_header(
textOutput("title"),
popover(
bsicons::bs_icon("gear", title = "Settings"),
selectInput(
"var",
"Select a variable",
choices = d_vars,
selected = "temp_avg"
)
),
class = "d-flex justify-content-between align-items-center"
),
card_body(
plotOutput("plot")
),
full_screen = TRUE
),
uiOutput("valueboxes")
)
server <- function(input, output, session) {
observe({
updateSelectInput(
session,
"name",
choices = d |>
distinct(region, name) |>
filter(region == input$region) |>
pull(name)
)
})
output$valueboxes <- renderUI({
clean <- function(x) {
round(x, 1) |> paste("°C")
}
layout_columns(
value_box(
title = "Average Temp",
value = mean(d_city()$temp_avg, na.rm = TRUE) |> clean(),
showcase = bsicons::bs_icon("thermometer-half"),
theme = "success"
),
value_box(
title = "Minimum Temp",
value = min(d_city()$temp_min, na.rm = TRUE) |> clean(),
showcase = bsicons::bs_icon("thermometer-snow"),
theme = "primary"
),
value_box(
title = "Maximum Temp",
value = max(d_city()$temp_max, na.rm = TRUE) |> clean(),
showcase = bsicons::bs_icon("thermometer-sun"),
theme = "danger"
)
)
})
output$title <- renderText({
names(d_vars)[d_vars == input$var]
})
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() +
theme_minimal()
})
}
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.