HES 505 Fall 2024: Session 28
List the necessary elements of an interactive dashboard
Outline the structure of code needed to build a flexdashboard
Build a simple interactive dashboard with spatial data
Reactive to user inputs
global
code chunk to load libraries and dataShiny
inputs and outputsOutput is now a flexdashboard (instead of html)
runtime:: shiny
allows R Shiny
to handle interactivity
global
codeCode that only needs to run once
```{r global}
# include: false
library(shiny)
library(sf)
library(tidyverse)
library(tmap)
tmap_mode("view")
cejst <- st_read("/opt/data/data/assignment01/cejst_nw.shp")
# get column codes and meanings
col_choices <- read_csv("/opt/data/data/assignment04/columns.csv") %>%
# make nicer column names for a display table
rename("Code" = "shapefile_column", "Description" = "column_name") %>%
# keep only "percentile" type columns
filter(str_detect(Code, "PFS"))
```
sidebar
R Function |
Input Type |
---|---|
selectInput |
A box with choices to select from |
sliderInput |
A slider bar |
radioButtons |
A set of radio buttons |
textInput |
A field to enter text |
numericInput |
A field to enter numbers |
checkboxInput |
A single check box |
dateInput |
A calendar to aid date selection |
dateRangeInput |
A pair of calendars for selecting a date range |
fileInput |
A file upload control wizard |
# Box with choices: which cejst column to map
selectInput("column_select", label = "Justice Marker:",
choices = col_choices$Code, selected = "DF_PFS")
# Two sliders to select the maximum and minimum values to map
sliderInput("min_threshold_adjust", label = "Minimum value:",
min = 0, max = 1, value = 0.5, step = 0.05)
sliderInput("max_threshold_adjust", label = "Maximum value:",
min = 0, max = 1, value = 1, step = 0.05)
Create a new column with title
R Function |
Output Type |
---|---|
renderPlot |
R graphics output |
renderPrint |
R printed output |
renderTable |
Data frame, matrix, other table like structures |
renderText |
Character vectors |
Specify reactive elements with input$NameOfInput
In this example, we use reactive filtering to only map cejst
tracts that meet user criteria
# renderTmap is a tmap special case of renderPlot
renderTmap({
# put reactively filtered data in tm_shape
tm_shape(subset(cejst[, input$column_select], # subset data to user's column
# use the subset in the filtering steps, selecting the column of data with [[1]]
cejst[, input$column_select][[1]] <= input$max_threshold_adjust & # data column should be less than or equal to the user's max threhold
cejst[, input$column_select][[1]] >= input$min_threshold_adjust)) + #more than or equal to the min threshold
# add the polygons filled by the user's selected column
tm_polygons(col = input$column_select)
})
Add plain text before or after code chunks
I’ve also added a nice table with the column name meanings for reference
On shinyapps.io (free; tutorial here)
On GitHub Pages (limited free pages; tutorial here)
Contact Research Computing for options