-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.r
284 lines (253 loc) · 7.59 KB
/
global.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# 0. preface -----------------------------------------
# to install `albersusa`, call:
#
# ```R
# install.packages("remotes")
# library(remotes)
# install_github("hrbrmstr/albersusa")
# ```
# 1. packages ----------------------------------------
library(shiny)
library(magrittr)
library(dplyr)
library(leaflet)
library(formattable)
library(sass)
library(lubridate)
library(albersusa)
# 2. building styles ---------------------------------
style_dir <- "styles"
dir.create(style_dir, showWarnings = FALSE)
styles_path <-
paste(
style_dir,
"/styles.css",
sep = ""
)
sass(
sass_file("styles.scss"),
output = styles_path
)
addResourcePath(
prefix = style_dir,
directoryPath = style_dir
)
# 3. getting data ------------------------------------
# CSSE data is posted midnight eastern time
# the day before the current day in eastern time
# formatted to match CSSE data filenames
yesterday <-
today("America/New_York") %>%
- 1 %>%
format("%m-%d-%Y")
# download most recent data into a data frame
latest_csse_upload <- function(base_url) {
# generate url
paste(
base_url,
yesterday,
".csv",
sep = ""
) %>%
# download & parse
read.csv()
}
# names (keys for) sets of data points to be offered
# keyed by each of the sets' names in the dashboard
data_options <- c(
"deaths due to COVID-19" = "deaths",
"confirmed COVID-19 cases" = "confirmed",
"hospitalizations for COVID-19" = "hospitalizations"
)
# a named list of data frames
# ex: one frame for state-level data and one for county-level data
data_levels <- list(
# begin state-level data wrangling
"state" = {
# 1/2 - prep
# map state abbreviations to state names
state_abbreviations <- setNames(state.name, state.abb)
hospital_data <-
"https://healthdata.gov/resource/g62h-syeh.csv" %>%
read.csv() %>%
group_by(state) %>%
summarize(
hospitalizations = sum(inpatient_beds_used_covid, na.rm = TRUE)
) %>%
mutate(state = as.character(state_abbreviations[state]))
csse_data <-
"https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports_us/" %>%
latest_csse_upload()
# 2/2 - final data
full_join(
hospital_data,
csse_data,
by = c("state" = "Province_State")
) %>%
mutate(
name = state,
deaths = Deaths,
confirmed = Confirmed
) %>%
dplyr::select(
name,
deaths,
confirmed,
hospitalizations
) %>%
left_join(
usa_sf(),
.,
by = "name"
)
}, # end state-level data wrangling
# begin county-level data wrangling
"county" = {
# 1/2 - prep
# format integer fips values as 5-character fips strings
# for matching in table join
pad_fips <- . %>%
formatC(
width = 5,
flag = "0"
)
hospital_data <-
"https://healthdata.gov/resource/di4u-7yu6.csv" %>%
read.csv() %>%
mutate(
fips = pad_fips(fips),
hospitalizations = confirmed_covid_hosp_last_7_days / 7
) %>%
dplyr::select(
fips,
hospitalizations
)
# 2/2 - final data
"https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/" %>%
latest_csse_upload() %>%
filter(Country_Region == "US") %>%
mutate(
fips = pad_fips(FIPS),
deaths = Deaths,
confirmed = Confirmed
) %>%
dplyr::select(
fips,
deaths,
confirmed
) %>%
full_join(
hospital_data,
by = "fips"
) %>%
left_join(
counties_sf(),
.,
by = "fips"
)
} # end county-level data wrangling
)
# free up memory
rm(hospital_data, csse_data)
# 4. pre-generating views ----------------------------
# map projection
epsg2163 <- leafletCRS(
crsClass = "L.Proj.CRS",
code = "EPSG:2163",
proj4def = "+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs",
resolutions = 2 ^ (16:7)
)
# generate a map given a level (ex: state) & option (ex: deaths)
get_map <- function(level, option) {
data <- data_levels[[level]]
# extract data points from level data after lookup in options
data_points <-
option %>%
data_options[.] %>%
as.character() %>%
data[[.]]
color_palette <- colorNumeric("Reds", domain = data_points)
return (
# initialize map
leaflet(
options = leafletOptions(
crs = epsg2163,
zoomControl = FALSE
)
) %>%
# draw choropleth
addPolygons(
data = data,
popup = paste(
data$name,
data_points %>% comma(digits = 0) %>% as.character(),
sep = ": "
),
color = color_palette(data_points),
fillColor = color_palette(data_points),
opacity = 1,
fillOpacity = 1,
weight = 2,
) %>%
addLegend(
"bottomright",
pal = color_palette,
values = data_points,
opacity = 1,
na.label = "no data"
)
)
}
# maps generated for all level and option combinations
# resulting data structure example:
#
# ```yaml
# state:
# - deaths: get_map("state", "deaths")
# - hospitalizations: get_map("state", "hospitalizations")
# - confirmed: get_map("state, "confirmed")
# county:
# - deaths: get_map("county", "deaths")
# - hospitalizations: get_map("county", "hospitalizations")
# - confirmed: get_map("county, "confirmed")
# ```
maps <-
data_levels %>%
names() %>% # ex: c("state", "county")
set_names(., .) %>% # ex: c("state" = "state", "county" = "county")
lapply( # for each level name
function(level) {
lapply( # for each option name
names(data_options) %>% set_names(data_options),
. %>% get_map(level, .)
)
}
)
# numerical figures to be used in summary text
summary_figures <-
data_options %>%
set_names(data_options) %>% # index results by option names
lapply( # for each data option
. %>%
data_levels$state[[.]] %>% # get data points
sum(na.rm = TRUE) %>%
comma(digits = 0) %>%
tags$strong() # indicate urgency in html
)
# summary text
summaries <- list(
"deaths" = tags$p(
"In total, ",
summary_figures$deaths,
" people have died due to COVID-19 in the US."
),
"confirmed" = tags$p(
summary_figures$confirmed,
" COVID-19 cases have been documented in the US."
),
"hospitalizations" = tags$p(
"In total, COVID-19 has resulted in ",
summary_figures$hospitalizations,
" hospitalizations in the US."
)
)