-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
385 lines (321 loc) · 14.6 KB
/
server.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
function(input, output, session){
# menu_home -------------------------
output$home_total_case <- renderValueBox({
valueBox(
value = sum_cum %>% filter(name == "All") %>% pull(cumul_cases_new),
subtitle = "Total number of COVID-19 cases",
color = "yellow",
icon = icon("virus")
)
}) ## end of home_total_case
output$home_total_death <- renderValueBox({
valueBox(
value = sum_cum %>% filter(name == "All") %>% pull(cumul_deaths_new),
subtitle = "Total number of deaths due to COVID-19",
color = "red",
icon = icon("skull-crossbones")
)
}) ## end of home_total_death
output$home_total_vac <- renderValueBox({
valueBox(
value = sum_cum %>% filter(name == "All") %>% pull(cumul_daily_full),
subtitle = "Total number of fully vaccination",
color = "green",
icon = icon("syringe")
)
}) ## end of home_total_vac
output$home_total_case_adjust <- renderValueBox({
valueBox(
value = formatC(sum_cum %>% filter(name == "All") %>% pull(cumul_cases_new_adjust), format = "f", digits = 1),
subtitle = "Adjusted number of COVID-19 cases",
color = "yellow",
icon = icon("virus")
)
}) ## end of home_total_case_adjust
output$home_total_death_adjust <- renderValueBox({
valueBox(
value = formatC(sum_cum %>% filter(name == "All") %>% pull(cumul_deaths_new_adjust), format = "f", digits = 1),
subtitle = "Adjusted number of deaths",
color = "red",
icon = icon("skull-crossbones")
)
}) ## end of home_total_death_adjust
output$home_total_vac_adjust <- renderValueBox({
valueBox(
value = formatC(sum_cum %>% filter(name == "All") %>% pull(cumul_daily_full_adjust), format = "f", digits = 1),
subtitle = "Adjusted number of fully vaccination",
color = "green",
icon = icon("syringe")
)
}) ## end of home_total_vac_adjust
output$home_recent_trend <- renderPlotly({
dt_daily_state_last7 <- dt_daily_state %>%
select(state, date, !!as.symbol(input$home_recent_stat)) %>%
rename(values_from = !!as.symbol(input$home_recent_stat)) %>%
group_by(state) %>%
filter(row_number() > (n() - 7)) %>%
pivot_wider(id_cols = state, values_from = values_from, names_from = date) %>%
ungroup() %>%
arrange(desc(state))
plt_case <- plot_ly(
x = colnames(dt_daily_state_last7 %>% select(-state)), y = dt_daily_state_last7 %>% pull(state), z = as.matrix(dt_daily_state_last7 %>% select(-state)),
type = "heatmap", colors = if(input$home_recent_stat == "daily_full_adjust"){"YlGn"}else{"OrRd"}
)
}) ## end of home_recent_stat
# menu_case -------------------------
## Adjusted daily incidence by states
output$home_plt_case_state <- renderPlotly({
plot_ly(
data = dt_daily_state %>% filter(state %in% input$home_case_state),
x = ~date, y = ~cases_new_adjust, color = ~state,
type = "bar", barmode = "stack"
) %>%
add_trace(
x = ~date, y = ~cases_new_adjust_ma7, color = ~state,
type = "scatter", mode = "lines", name = ~paste("MA-7:", state)
) %>%
layout(
yaxis = list(title = "Daily incidence case per 100K population"),
xaxis = list(title = "Date", rangeslider = list(type = "date")),
legend = list(title = list(text = "<b> States </b>"), orientation = "h", y = 100)
)
}) ## end of home_plt_case_state
## Adjusted daily incidence and not-fully vaccination rate by states
output$home_plt_case_vac_state <- renderPlotly({
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "Daily incidence case per 100K population",
automargin = TRUE
)
plot_ly(
data = dt_daily_state %>% filter(state %in% input$home_case_state),
x = ~date, y = ~cumul_not_full_adjust, color = ~state, legendgroup = ~state,
type = "scatter", mode = "lines", name = ~paste("Not fully-vaccinated:", state), line = list(dash = "dash")
) %>%
add_trace(
x = ~date, y = ~cases_new_adjust_ma7, color = ~state,
type = "scatter", mode = "lines", name = ~paste("MA (7-days):", state), line = list(dash = "solid")
) %>%
add_trace(
x = ~date, y = ~cases_new_adjust, color = ~state,
type = "bar", barmode = "stack", yaxis = "y2", name = ~paste("Cases:", state)
) %>%
layout(
xaxis = list(title = "Date", rangeslider = list(type = "date")),
yaxis = list(title = "Not fully-vaccinated population per 100K population"), yaxis2 = ay,
legend = list(title = list(text = "<b> States </b>"), orientation = "h", y = 100)
)
}) ## end of home_plt_case_vac_state
## Adjusted daily incidence and death rate by states
output$home_plt_case_death_state <- renderPlotly({
plot_ly(
data = dt_daily_state %>% filter(state %in% input$home_case_state),
x = ~date, y = ~deaths_new_adjust, color = ~state,
type = "bar", barmode = "stack"
) %>%
add_trace(
x = ~date, y = ~deaths_new_adjust_ma7, color = ~state,
type = "scatter", mode = "lines", name = ~paste("MA-7:", state)
) %>%
layout(
yaxis = list(title = "Daily death cases per 1M population"), xaxis = list(title = "Date", rangeslider = list(type = "date")),
legend = list(title = list(text = "<b> States </b>"), orientation = "h", y = 100)
)
}) ## end of home_plt_case_death_state
## Adjusted daily incidence and death rate by states
output$home_plt_death_vac_state <- renderPlotly({
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "Daily death cases per 1M population",
automargin = TRUE
)
plot_ly(
data = dt_daily_state %>% filter(state %in% input$home_case_state),
x = ~date, y = ~cumul_not_full_adjust, color = ~state, legendgroup = ~state,
type = "scatter", mode = "lines", name = ~paste("Not fully-vaccinated:", state), line = list(dash = "dash")
) %>%
add_trace(
x = ~date, y = ~deaths_new_adjust_ma7, color = ~state,
type = "scatter", mode = "lines", name = ~paste("MA (7-days):", state), line = list(dash = "solid")
) %>%
add_trace(
x = ~date, y = ~deaths_new_adjust, color = ~state,
type = "bar", barmode = "stack", yaxis = "y2", name = ~paste("Deaths:", state)
) %>%
layout(
xaxis = list(title = "Date", rangeslider = list(type = "date")),
yaxis = list(title = "Not fully-vaccinated population per 100K population"), yaxis2 = ay,
legend = list(title = list(text = "<b> States </b>"), orientation = "h", y = 100)
)
}) ## end of home_plt_case_death_state
## Heatmap by states
output$home_plt_heatmap_state <- renderLeaflet({
nc_state <- nc %>%
left_join(sum_cum %>% filter(name != "All")) %>%
rename(index = !!as.symbol(input$home_cumul))
leaflet(nc_state) %>%
setView(lng = 108, lat = 4, zoom = 6.5) %>%
# addTiles() %>%
addPolygons(
color = "#444444", weight = 1, smoothFactor = 0.5,
opacity = 1.0, fillOpacity = 0.5,
fillColor = ~colorNumeric("YlOrRd", index)(index),
highlightOptions = highlightOptions(color = "white", weight = 2, bringToFront = TRUE),
popup = ~paste("State:", name, ", Statistics:", format(round(index, digits = 1), nsmall = 1)) ## Popup info
) %>%
addLegend(
"topright", pal = colorNumeric("YlOrRd", nc_state$index), values = ~index,
title = "Intensity",
opacity = 1
)
}) ## end of home_plt_heatmap_state
# menu_hru -------------------------
## Heatmap by states
output$hru_plt_heatmap_state <- renderLeaflet({
nc_state <- nc %>%
left_join(dt_daily_state %>%
rename(name = state) %>%
filter(name != "All") %>%
group_by(name) %>%
filter(row_number() == n()-1)) %>%
rename(index = !!as.symbol(input$hru_cumul))
leaflet(nc_state) %>%
setView(lng = 108, lat = 4, zoom = 6.5) %>%
# addTiles() %>%
addPolygons(
color = "#444444", weight = 1, smoothFactor = 0.5,
opacity = 1.0, fillOpacity = 0.5,
fillColor = ~colorNumeric("RdYlGn", index)(index),
highlightOptions = highlightOptions(color = "white", weight = 2, bringToFront = TRUE),
popup = ~paste("State:", name, ", Statistics:", format(round(index, digits = 1), nsmall = 1)) ## Popup info
) %>%
addLegend(
"topright", pal = colorNumeric("RdYlGn", nc_state$index), values = ~index,
title = "Intensity (%)",
opacity = 1
)
}) ## end of home_plt_heatmap_state
output$hru_plt_beds_state <- renderPlotly({
plot_ly(
data = dt_daily_state %>% filter(state %in% input$hru_resource_state),
x = ~date, y = ~beds_covid_adjust, color = ~state, name = ~paste("COVID beds:", state),
type = "scatter", mode = "lines"
) %>%
add_trace(
x = ~date, y = ~beds_noncrit_adjust, color = ~state,
type = "scatter", mode = "lines", line = list(dash = "dash"), name = ~paste("Non-COVID beds:", state)
) %>%
layout(
yaxis = list(title = "Daily beds per 100K population"), xaxis = list(title = "Date", rangeslider = list(type = "date")),
legend = list(title = list(text = "<b> States </b>"), orientation = "h", y = 100)
)
}) ## end of hru_plt_beds_state
output$hru_plt_icu_state <- renderPlotly({
plot_ly(
data = dt_daily_state %>% filter(state %in% input$hru_resource_state),
x = ~date, y = ~beds_icu_total_adjust, color = ~state, name = ~paste("Total ICU:", state),
type = "scatter", mode = "lines"
) %>%
add_trace(
x = ~date, y = ~beds_icu_covid_adjust, color = ~state,
type = "scatter", mode = "lines", line = list(dash = "dash"), name = ~paste("COVID ICU:", state)
) %>%
layout(
yaxis = list(title = "Daily beds per 100K population"), xaxis = list(title = "Date", rangeslider = list(type = "date")),
legend = list(title = list(text = "<b> States </b>"), orientation = "h", y = 100)
)
}) ## end of hru_plt_icu_state
output$hru_plt_vent_state <- renderPlotly({
plot_ly(
data = dt_daily_state %>% filter(state %in% input$hru_resource_state),
x = ~date, y = ~vent_adjust, color = ~state,
type = "bar", barmode = "stack"
) %>%
layout(
yaxis = list(title = "Ventilators per 100K population"), xaxis = list(title = "Date", rangeslider = list(type = "date")),
legend = list(title = list(text = "<b> States </b>"), orientation = "h", y = 100)
)
}) ## end of hru_plt_vent_state
output$hru_plt_pkrc_state <- renderPlotly({
plot_ly(
data = dt_daily_state %>% filter(state %in% input$hru_resource_state),
x = ~date, y = ~pkrc_beds_adjust, color = ~state,
type = "bar", barmode = "stack"
) %>%
layout(
yaxis = list(title = "PKRC beds per 100K population"), xaxis = list(title = "Date", rangeslider = list(type = "date")),
legend = list(title = list(text = "<b> States </b>"), orientation = "h", y = 100)
)
}) ## end of hru_plt_pkrc_state
output$hru_plt_beds_avl_state <- renderPlotly({
plot_ly(
data = dt_daily_state %>% filter(state %in% input$hru_resource_state),
x = ~date, y = ~cumul_remain_bed_rate, color = ~state,
type = "bar", barmode = "stack"
) %>%
layout(
yaxis = list(title = "Porportion of resources available (%)"), xaxis = list(title = "Date", rangeslider = list(type = "date")),
legend = list(title = list(text = "<b> States </b>"), orientation = "h", y = 100)
)
}) ## end of hru_plt_icu_avl_state
output$hru_plt_icu_avl_state <- renderPlotly({
plot_ly(
data = dt_daily_state %>% filter(state %in% input$hru_resource_state),
x = ~date, y = ~remain_icu_covid_rate, color = ~state,
type = "bar", barmode = "stack"
) %>%
layout(
yaxis = list(title = "Porportion of resources available (%)"), xaxis = list(title = "Date", rangeslider = list(type = "date")),
legend = list(title = list(text = "<b> States </b>"), orientation = "h", y = 100)
)
}) ## end of hru_plt_icu_avl_state
output$hru_plt_vent_avl_state <- renderPlotly({
plot_ly(
data = dt_daily_state %>% filter(state %in% input$hru_resource_state),
x = ~date, y = ~remain_vent_covid_rate, color = ~state,
type = "bar", barmode = "stack"
) %>%
layout(
yaxis = list(title = "Porportion of resources available (%)"), xaxis = list(title = "Date", rangeslider = list(type = "date")),
legend = list(title = list(text = "<b> States </b>"), orientation = "h", y = 100)
)
}) ## end of hru_plt_vent_avl_state
output$hru_plt_pkrc_avl_state <- renderPlotly({
plot_ly(
data = dt_daily_state %>% filter(state %in% input$hru_resource_state),
x = ~date, y = ~cumul_remain_pkrc_rate, color = ~state,
type = "bar", barmode = "stack"
) %>%
layout(
yaxis = list(title = "Porportion of resources available (%)"), xaxis = list(title = "Date", rangeslider = list(type = "date")),
legend = list(title = list(text = "<b> States </b>"), orientation = "h", y = 100)
)
}) ## end of hru_plt_[krc_avl_state
# menu_cluster -------------------------
output$cluster_plt_case <- renderPlotly({
data <- dt_cluster %>%
rename(cases = !!as.symbol(input$cluster_case)) %>%
{if(input$cluster_status != "all") filter(., status == input$cluster_status) else .} %>%
{if(input$cluster_state != "All") filter(., grepl(str_replace_all(input$cluster_state, "[[:punct:]]", ""), state)) else .} %>%
group_by(category) %>%
summarise(cumul = sum(cases, na.rm = TRUE)) %>%
ungroup() %>%
mutate(cumul_prop = cumul*100 /sum(cumul))
plot_ly(
data = data, labels = ~category, values = ~cumul, type = "pie",
textposition = "outside", textinfo = "label", insidetextfont = list(color = "#FFFFFF"),
marker = list(line = list(color = "#FFFFFF", width = 1)),
hoverinfo = "text", text = ~paste(cumul, "cases,", format(round(cumul_prop, digits = 1), nsmall = 1), "%")
) %>%
layout(
showlegend = TRUE,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
xyaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
legend = list(title = list(text = "<b> Category </b>"))
)
})
}