-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.R
76 lines (55 loc) · 2.52 KB
/
app.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Packages ----------------------------------------------------------------
library(tidyverse)
library(janitor)
library(albersusa)
library(sf)
library(tigris)
library(r2d3maps)
library(rmapshaper)
library(shiny)
library(shinycssloaders)
# County geometries -------------------------------------------------------
cty_sf <- counties_sf("longlat") # from albersusa package
plot(st_geometry(cty_sf)) # checking geometries, looks good
cty_sf <- ms_simplify(cty_sf) # make the geometries simpler for faster rendering
# Data ---------------------------------------------------------------------
# CDC's social vulnerability index variables: https://svi.cdc.gov/Documents/Data/2018_SVI_Data/SVI2018Documentation.pdf
svi <- read_csv("svi_county.csv") %>%
clean_names() %>% # make everything snake_case
filter(across(where(is.numeric), ~. >= 0)) # because NA's are coded as -Inf
# join data to geometries
cty_sf_joined <- cty_sf %>% geo_join(svi, by_sp = "fips", by_df = "fips")
# UI ----------------------------------------------------------------------
ui <- navbarPage("Fun with r2d3map", id="nav",
tabPanel(
"Interactive map",
withSpinner(d3Output(outputId = "mymap", width = "900px", height = "500px"))),
tabPanel("Explore the data",
DT::dataTableOutput("table"))
)
# Server ------------------------------------------------------------------
server <- function(input, output) {
# map panel
output$mymap <- renderD3({
d3_map(shape = cty_sf_joined, projection = "Albers") %>%
add_labs(caption = "Viz: Asmae Toumi | Data: CDC") %>%
add_continuous_breaks(var = "rpl_theme1", palette = "Reds") %>%
add_legend(title = "Socioeconomic Vulnerability (percentile)") %>%
add_tooltip("<b>{location}</b>: {rpl_theme1}")
})
# data panel
output$table <- DT::renderDataTable({
DT::datatable(svi, rownames = F, filter = 'top',
extensions = c('Buttons', 'FixedHeader', 'Scroller'),
options = list(pageLength = 15, lengthChange = F,
fixedHeader = TRUE,
dom = 'lfBrtip',
list('copy', 'print', list(
extend = 'collection',
buttons = c('csv', 'excel', 'pdf'),
text = 'Download'
))
))
})
}
shinyApp(ui = ui, server = server)