Building a climate risk dashboard - UI

Suggested answers

Application exercise
Answers
Modified

April 11, 2024

Communicating climate risk

FEMA has asked us to build an improved dashboard that visualizes the risk of climate change in the United States.

Based on your submitted designs and our skill level, we will work to implement this Shiny dashboard.

Construct a user interface

Structure the dashboard layout

Position the outputs

Tip

Review the documentation for how to display different types of data (e.g. plots, tables, value boxes). Remember that for most of the outputs, they will be generated in the server function so you won’t see them rendered in the dashboard yet.

    • climate_risk |>
        gt() |>
        opt_interactive()

Define the inputs

Tip

Use your Shiny cheatsheet and the reference documentation to find appropriate input types.

National tab

County tab

Tip

You can get all county names using

climate_sf |>
  arrange(STATEFP) |>
  pull(county)
Tip

You can get all risk measures using

# hazard types
hazard_types <- climate_risk |>
  select(contains("hazard")) |>
  colnames()

# human-readable labels
hazard_types_labels <- hazard_types |>
  str_remove(pattern = "_hazard_type_risk_index_score") |>
  make_clean_names(case = "title")

Define code context

    • setup
    • data
    • server
    • Nothing (no context required, so omit the chunk option entirely)

Suggested code

---
title: National Risk Index Counties
format: dashboard
server: shiny
---

```{r}
#| label: setup
#| context: setup
#| include: false

# load libraries
library(tidyverse)
library(scales)
library(shiny)
library(sf)
library(tigris)
library(janitor)
library(ggthemes)
library(colorspace)
library(bslib)
library(bsicons)
library(gt)

options(tigris_use_cache = TRUE)
```

```{r}
#| label: import-data
#| context: data
#| include: false

# climate risk
climate_risk <- read_csv(file = "data/National_Risk_Index_Counties_807384124455672111.csv") |>
  clean_names() |>
  mutate(
    county = str_glue("{county_name} {county_type}, {state_name}")
  ) |>
  select(
    state_county_fips_code, county,
    population_2020, building_value, agriculture_value, area_sq_mi,
    national_risk_index_score_composite, national_risk_index_rating_composite,
    expected_annual_loss_score_composite, expected_annual_loss_rating_composite,
    social_vulnerability_score, social_vulnerability_rating,
    community_resilience_score, community_resilience_rating,
    contains("type_risk_index_score")
  ) |>
  mutate(
    across(
      .cols = contains("rating"),
      .fns = \(col) fct_relevel(
        .f = col, "Very High", "Relatively High",
        "Relatively Moderate", "Relatively Low", "Very Low"
      )
    )
  ) |>
  # rename overall ratings columns for variable selection
  rename_with(
    .cols = c(contains("rating"), -contains("hazard")),
    .fn = \(col) str_remove(string = col, pattern = "_rating_composite|_rating") |>
      make_clean_names(case = "title")
  )

# import state and county boundaries
state_sf <- states(cb = TRUE, keep_zipped_shapefile = TRUE) |>
  filter(STUSPS %in% state.abb) |>
  shift_geometry()

county_sf <- counties(cb = TRUE, keep_zipped_shapefile = TRUE) |>
  filter(STUSPS %in% state.abb) |>
  shift_geometry()

# combine climate risk with county_sf
climate_sf <- left_join(
  x = county_sf,
  y = climate_risk,
  by = join_by(GEOID == state_county_fips_code)
) |>
  as_tibble() |>
  st_as_sf()
```

# National Risk Index

## {.sidebar}

```{r}
#| label: select-national-risk

varSelectInput(
  inputId = "risk_var",
  label = "Risk index",
  data = climate_risk |>
    select(`National Risk Index`, `Expected Annual Loss`, `Social Vulnerability`, `Community Resilience`)
)
```

## Column

```{r}
#| label: national-map

plotOutput(outputId = "national_map")
```

# County Details

## {.sidebar}

```{r}
#| label: select-county

county_names <- climate_sf |>
  arrange(STATEFP) |>
  pull(county)

selectizeInput(
  inputId = "county",
  label = "Selected county",
  choices = county_names,
  selected = NULL,
  options = list(
    placeholder = "Select a county",
    maxItems = 1
  )
)

# hazard types
hazard_types <- climate_risk |>
  select(contains("hazard")) |>
  colnames()

hazard_types_labels <- hazard_types |>
  str_remove(pattern = "_hazard_type_risk_index_score") |>
  make_clean_names(case = "title")
names(hazard_types) <- hazard_types_labels

checkboxGroupInput(
  inputId = "hazard_types",
  label = "Hazard types",
  choices = hazard_types,
  selected = hazard_types
)
```

## Column

### Row

```{r}
#| label: county-map

plotOutput(outputId = "county_map")
```

```{r}
#| label: county-hazards

plotOutput(outputId = "county_hazards")
```

### Row

```{r}
#| label: county-risk

value_box(
  title = "Overall risk score",
  value = textOutput("county_risk"),
  showcase = bs_icon("radioactive")
)
```

```{r}
#| label: county-expected-loss

value_box(
  title = "Expected annual loss",
  value = textOutput("county_loss"),
  showcase = bs_icon("trash")
)
```

```{r}
#| label: county-social-vulnerability

value_box(
  title = "Social vulnerability",
  value = textOutput("county_vulnerability"),
  showcase = bs_icon("cone-striped")
)
```

```{r}
#| label: county-community-resilience

value_box(
  title = "Community resilience",
  value = textOutput("county_resilience"),
  showcase = bs_icon("emoji-sunglasses")
)
```

# Data

```{r}
#| label: print-data
#| context: data

climate_risk |>
  gt() |>
  opt_interactive()
```

```{r}
#| label: server
#| context: server

# add code here
```