-
Notifications
You must be signed in to change notification settings - Fork 101
/
server.R
804 lines (725 loc) · 34.1 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
# Developed by California COVID Modeling Team
# Copyright 2024, State of California, Department of Public Health
#
# Mugdha Thakur, PhD.
# California Department of Public Health
#
# Phoebe Lu, MPH
# California Department of Public Health
#
# Tomas Leon, PhD
# California Department of Public Health
#
# John Pugliese, PhD.
# California Department of Public Health
#
# Jason Vargo, PhD.
# California Department of Public Health
#
# Latest Version: Released 1/18/2024
#
# Alpha Version : Released 6/8/2020
#
##################################################
##### A gift from California with love. ##########
#### “Together, all things are possible.” ######
###################### -- Cesar Chavez ###########
##################################################
# Define server logic required to draw a histogram
server <- function(input, output, session) {
#### Carousel Navigation ####
shinyjs::onclick("nowcast_img", updateTabsetPanel(session, inputId="navbar", selected= "Nowcasts"))
shinyjs::onclick("forecast_img", updateTabsetPanel(session, inputId="navbar", selected= "Forecasts"))
shinyjs::onclick("epimodel_img", updateTabsetPanel(session, inputId="navbar", selected= "Scenarios"))
### Nowcasts of R Effective ####
#Data Prep
rt.ts <- reactive({
ensemble_df <- nowcasts %>%
group_by(date) %>%
summarize( n_model = length(value), value = median(value, na.rm=T)) %>%
filter(n_model > 2) %>%
select(-n_model) %>%
mutate(model = "Ensemble",
location_level = "state",
location = state_name,
metric = "reff") %>%
ungroup()
return(bind_rows(nowcasts, ensemble_df) %>% filter(!is.na(value)))
})
#Value Boxes
output$mean.rt.box <- renderValueBox({
df <- rt.ts() %>%
filter(model == "Ensemble")
cdt <- max(df$date)
current.rt <- round(df$value[which(df$date == cdt)], digits = 2)
valueBox(current.rt, subtitle = paste0(ifelse(current.rt >= 1.4,
"Spread of COVID-19 is very likely increasing",
ifelse(current.rt < 1.4 & current.rt >= 1.1,
"Spread of COVID-19 may be increasing",
ifelse(current.rt < 1.1 & current.rt >= 0.9,
"Spread of COVID-19 is likely stable",
"Spread of COVID-19 is likely decreasing"
)
)
)
),
color = "blue"
) #End valuBox
})
observeEvent(input$Rt_explain, {
sendSweetAlert(
session = session,
title = "What does a R-eff of this size mean?",
text = HTML("<p>If the R-effective is greater than 1, COVID-19 will spread <b>exponentially</b>. If R-effective is less than 1, COVID-19 will spread more slowly and cases will decline. The higher the value of R-effective, the faster an epidemic will progress.
The following graph illustrates the change in growth as R-effective increases.</p>
<img src='reff_cuml_infection.jpg' alt='Infections increase faster with larger values of R-effective' width='450px'/>
<p><a href='https://www.cebm.net/covid-19/when-will-it-be-over-an-introduction-to-viral-reproduction-numbers-r0-and-re/' target='_blank'>Source: CEBM</a></p>"
),
html = TRUE,
type = NULL
)
})
output$hilo_rt.box <- renderUI({
df <- rt.ts()
df <- df %>% filter(date < Sys.Date()) %>%
pivot_wider(names_from = model, values_from = value) %>%
filter(!is.na(Ensemble)) %>%
arrange(date) %>%
slice(n()) %>%
select(-date, -location, -location_level, -metric)
rt.min <- as.numeric( min(df, na.rm = T) )
rt.max <- as.numeric( max(df, na.rm = T) )
name.min <- names(which.min(as.list(df)))
name.max<- names(which.max(as.list(df)))
tagList(valueBox( paste0( round(rt.min,digits = 2)," - ", round(rt.max,digits = 2)) , paste0(name.min," - ",name.max), color = "navy", width = 12) )
})
#Graph
output$rt.plot <- renderPlotly({
df <- rt.ts() %>% filter(date < Sys.Date() & date >= (Sys.Date() - 80) )
p <- plot_ly(df %>% filter(model != "Ensemble"),
hoverinfo = 'text') %>%
add_trace(x =~date,
y =~value,
name =~model,
type = 'scatter',
mode = "lines",
line = list( dash = 'dot', opacity = 0.5),
text = paste0(df$date[which(df$model != "Ensemble")],
"<br>",
df$model[which(df$model != "Ensemble")], " estimated R-eff: ", round(df$value[which(df$model != "Ensemble")], digits=2)
)) %>%
add_trace(data = df %>% filter(model == "Ensemble"),
x = ~date,
y =~value,
name = "Ensemble",
type = 'scatter',
mode = "lines",
hoverinfo = 'text',
line = list(color = '#2b8cbe', width = 5),
text = paste0(df$date[which(df$model == "Ensemble")],
"<br>",
"Mean estimated R-eff: ", round(df$value[which(df$model == "Ensemble")], digits=2),
"<br>",
ifelse(round(df$value[which(df$model == "Ensemble")], digits=2) >= 1.4,
"Spread of COVID-19 is very likely increasing",
ifelse(round(df$value[which(df$model == "Ensemble")], digits=2) < 1.4 & round(df$value[which(df$model == "Ensemble")], digits=2) >= 1.1,
"Spread of COVID-19 may be increasing",
ifelse(round(df$value[which(df$model == "Ensemble")], digits=2) < 1.1 & round(df$value[which(df$model == "Ensemble")], digits=2) >= 0.9,
"Spread of COVID-19 is likely stable",
"Spread of COVID-19 is likely decreasing"
)
)
)
)
) %>%
layout(
title = NULL,
xaxis = list(title = NULL, showgrid = FALSE, zeroline = FALSE ),
yaxis = list(title = "R-Eff", showline = TRUE, showgrid = FALSE, zeroline = FALSE ),
margin = list(l = 100),
showlegend = TRUE,
shapes = list(
type = "line",
x0 = 0,
x1 = 1,
xref = "paper",
y0 = 1,
y1 = 1,
yref = "y",
line = list(color = "gray50", dash= "dash", opacity = 0.3))
)
return(p)
})
#Downloadable file of Statewide Reff Values
output$dlRt <- downloadHandler(
filename = function() { paste("R_eff_Nowcasts_",Sys.Date(),'.csv', sep='') },
content = function(file) {
# Title
t <- c(paste("R-Effective Model and Ensemble Time Series", sep = ""),"","","","","","")
#Subtitle
tt <- c(paste("Communicable diseases Assessment Tool - Downloaded on",Sys.Date(), sep = " "),"","","","","","")
#Column labels
l <- names(rt.ts())
df <- rt.ts() %>%
filter(date < Sys.Date() & date > Sys.Date() -80) %>%
mutate(value = round(value, 2))
df[] <- lapply(df, as.character)
#Source
s <- c("Please see the Technical Notes tab of the application for data sources.","","","","","","")
p <- c(paste0("Prepared by: ",state_name," Department of Public Health"),"","","","","","")
dlm <- rbind(t, tt, l, df, s, p)
write.table(dlm, file, row.names = F, col.names = F, quote = F, na= "NA", sep = ",")
})
#### County Rt Nowcasts ####
# #Data Prep
# county.rt <- reactive({
# progress <- Progress$new()
# # Make sure it closes when we exit this reactive, even if there's an error
# on.exit(progress$close())
# progress$set(message = "Gathering R Effective Nowcasts", value = 0)
#
# c <- names(canfipslist[match(input$select.county.rt,canfipslist)])
# cnty <- input$select.county.rt
#
# progress$inc(3/4)
# # out <- lapply(cnty[1], function(x) get_can_cnty(x))
# out <- filter(can.county.observed, fips == cnty)
#
# # cnty.rt <- do.call("rbind",out)
# cnty.rt <- out %>% select(date,RtIndicator) %>% as.data.frame() #,RtIndicatorCI90
# cnty.rt$date <- as.Date(cnty.rt$date)
# progress$inc(1/4)
#
# df <- xts(cnty.rt[,-1],cnty.rt$date)
# if( c %in% unique(gu.cnty$subregion)) {
# cnty.gu <- gu.cnty %>% filter(subregion == c) %>% select(date, r_values_mean)
# gu.xts <- xts(cnty.gu[,-1],cnty.gu$date)
# df <- merge(df,gu.xts)
# }
#
# # if (c %in% unique(ucla_cnty_rt$county) ) { cnty.ucla <- ucla_cnty_rt %>% filter(county == c) %>% select(date, Rt)
# # ucla.xts <- xts(cnty.ucla[,-1],cnty.ucla$date)
# # df <- merge(df,ucla.xts)
# # }
#
# if (ncol(df) > 1) {df$mean.proj <- rowMeans(df[,1:ncol(df)], na.rm = TRUE)}
#
# df <- as.data.table(df) %>% as.data.frame() %>% filter(index < Sys.Date())
#
# return(df)
#
# })
#
# #Graph
# output$county.rt.plot <- renderPlotly({
#
# df <- county.rt()
#
# c <- names(canfipslist[match(input$select.county.rt,canfipslist)])
#
# #df$ymin <- df$RtIndicator - (df$RtIndicatorCI90)
# #df$ymax <- df$RtIndicator + (df$RtIndicatorCI90)
#
# p <- plot_ly(df,
# x = df[[1]],
# y = df[[2]],
# name = "COVIDActNow",
# type = 'scatter',
# mode = "lines",
# line = list(color="blue", dash = 'dot', opacity = 0.5),
# hoverinfo = 'text',
# text = paste0(df[[1]],
# "<br>",
# "COVIDActNow estimated Reff: ", round(df[[2]], digits=2) )
# )
# if (c %in% unique(gu.cnty$subregion) ) {p <- p %>% add_trace(x = df[[1]],
# y = df[["gu.xts"]],
# name = "covid19-projections.com",
# type = 'scatter',
# mode = "lines",
# line = list(color="red", dash = 'dot', opacity = 0.5),
# hoverinfo = 'text',
# text = paste0(df[[1]],
# "<br>",
# "covid19-projections.com estimated Reff: ", round(df[["gu.xts"]], digits=2) )
# )
# }
# # if (c %in% unique(ucla_cnty_rt$county) ) {p <- p %>% add_trace(x = df[[1]],
# # y = df[["ucla.xts"]],
# # name = "UCLA",
# # type = 'scatter',
# # mode = "lines",
# # line = list(color="grey", dash = 'dot', opacity = 0.5),
# # hoverinfo = 'text',
# # text = paste0(df[[1]],
# # "<br>",
# # "UCLA estimated Reff: ", round(df[["ucla.xts"]], digits=2) )
# # )
# # }
# if (ncol(df) > 2) {p <- p %>% add_trace(x = df[[1]],
# y = df[["mean.proj"]],
# name = "Mean Reff",
# type = 'scatter',
# mode = "lines",
# hoverinfo = 'text',
# text = paste0(df[[1]],
# "<br>",
# "Mean estimated Reff: ", round(df[["mean.proj"]], digits=2),
# "<br>",
# ifelse(round(df[["mean.proj"]], digits=2) >= 1.4,
# "Spread of COVID-19 is very likely increasing",
# ifelse(round(df[["mean.proj"]], digits=2) < 1.4 & round(df[["mean.proj"]], digits=2) >= 1.1,
# "Spread of COVID-19 may be increasing",
# ifelse(round(df[["mean.proj"]], digits=2) < 1.1 & round(df[["mean.proj"]], digits=2) >= 0.9,
# "Spread of COVID-19 is likely stable",
# "Spread of COVID-19 is likely decreasing"
# )
# )
# )
# ),
# inherit = FALSE,
# line = list(color = '#2b8cbe', width = 5),
# linetype = "solid"
# )
# }
# # add_ribbons(x = df[[1]],
# # ymax = df[[5]],
# # ymin = df[[4]],
# # opacity = 0.5,
# # inherit = TRUE,
# # line = list(color = '#2b8cbe' ),
# # fillcolor = '#2b8cbe',
# # name = '90% CI'
# # ) %>%
# p <- p %>% layout( legend = list(orientation = 'h'),
# title = as.character(counties[match(input$select.county.rt, counties$fips),"county"]),
# xaxis = list(title = NULL, showgrid = FALSE, zeroline = FALSE ),
# yaxis = list(title = "R-Eff", showline = TRUE, showgrid = FALSE, zeroline = FALSE ),
# margin = list(l = 100),
# showlegend = TRUE,
# shapes = list(
# type = "line",
# x0 = 0,
# x1 = 1,
# xref = "paper",
# y0 = 1,
# y1 = 1,
# yref = "y",
# line = list(color = "gray50", dash= "dash", opacity = 0.3)
# )
# )
#
# return(p)
#
# })
#Download file of individual COUNTY Reff Values
# output$dlRt.indv.cnty <- downloadHandler(
#
# filename = function() { paste("Rt_Nowcasts_",names(canfipslist[match(input$select.county.rt,canfipslist)]),"_",Sys.Date(),'.csv', sep='') },
#
# content = function(file) {
#
# c <- names(canfipslist[match(input$select.county.rt,canfipslist)])
# # Title
# t <- c(paste("R-Effective County Model Time Series for ",c, sep = ""),"","","","")
# #Subtitle
# tt <- c(paste("COVID Assessment Tool - Downloaded on",Sys.Date(), sep = " "),"","","","")
#
# df <- county.rt() %>% as.data.frame()
# if ( ncol(df) > 2 ) { df[,2:ncol(df)] <- lapply(df[,2:ncol(df)],function(x) round(x,2)) } else { df[,2] <- round(df[,2],2) }
# df[is.na(df)] <- 0
# df[] <- lapply(df, as.character)
#
# #Column labels
#
# l <- c("Date","COVIDActNow")
#
# if ( c %in% unique(gu.cnty$subregion) ) { l <- c(l, c("covid19-projections.com")) }
# if ( c %in% unique(ucla_cnty_rt$county) ) { l <- c(l, c("UCLA")) }
# if ( length(l) > 2 ) { l <- c(l, c("Mean Reff") ) }
#
# #Source
# s <- c("Please see the Technical Notes tab of the application for data sources.","","","","")
# p <- c(paste0("Prepared by: ",state_name," Department of Public Health"),"","","","")
#
# dlm <- rbind(t, tt, l, df, s, p)
# write.table(dlm, file, row.names = F, col.names = F, quote = F, na= "NA", sep = ",")
# })
#
#Download file of ALL COUNTY 7-day average Reff Values
# output$dlRt.cnty <- downloadHandler(
#
# filename = function() { paste("Rt_Nowcasts_7DayAvg_Counties",Sys.Date(),'.csv', sep='') },
#
# content = function(file) {
#
# # Title
# t <- c(paste("R-Effective 7 Day Averages for Counties", sep = ""),"","","","")
# #Subtitle
# tt <- c(paste("COVID Assessment Tool - Downloaded on",Sys.Date(), sep = " "),"","","","")
#
# df <- cnty.7.day.rt %>% as.data.frame()
# if ( ncol(df) > 2 ) { df[,2:ncol(df)] <- lapply(df[,2:ncol(df)],function(x) round(x,2)) } else { df[,2] <- round(df[,2],2) }
# df[is.na(df)] <- 0
# df[] <- lapply(df, as.character)
#
# #Column labels
#
# l <- c("County","COVIDActNow - 7 Day Avg", "LL", "UL")
#
# #Source
# s <- c("Please see the Technical Notes tab of the application.","","","","")
# p <- c(paste0("Prepared by: ",state_name," Department of Public Health"),"","","","")
# u <- c("Source: COVIDActNow - https://blog.covidactnow.org/modeling-metrics-critical-to-reopen-safely/","","","","")
#
# dlm <- rbind(t, tt, l, df, s, p, u)
# write.table(dlm, file, row.names = F, col.names = F, quote = F, na= "NA", sep = ",")
# })
#
#
#### Hospitalization Projections ####
#Data Prep
hosp.proj.ts <- reactive({
#Select data
min_hosp <- min(forecasts$date)
hosp_actuals <- covid_actuals %>% select(date, weekly_admits) %>% mutate(date=as.Date(date, format="%Y-%m-%d")) %>% filter(lubridate::wday(date, label=TRUE)=="Sat")
hosp_forecasts <- forecasts %>% select(date,hosp_admits, model) %>% filter(forecasts$state == state_name) %>% as.data.frame() %>% filter(lubridate::wday(date, label=TRUE)=="Sat")
#Filter data for plot display
arima.hosp.proj <- hosp_forecasts %>% filter(model=="arima") %>% filter(min_hosp <= date & date <= Sys.Date() + 30)
holts.hosp.proj <- hosp_forecasts %>% filter(model=="holts") %>% filter(min_hosp <= date & date <= Sys.Date() + 30)
dampedholts.hosp.proj <- hosp_forecasts %>% filter(model=="dampedholts") %>% filter(min_hosp <= date & date <= Sys.Date() + 30)
nnetar.hosp.proj <- hosp_forecasts %>% filter(model=="nnetar") %>% filter(min_hosp <= date & date <= Sys.Date() + 30)
#Combine projections and calculate median
##Combine all forecast model estimate rows
median.proj <- rbind(arima.hosp.proj, holts.hosp.proj, dampedholts.hosp.proj, nnetar.hosp.proj)
##Group by forecast date and calculate median for each date
median.proj <- median.proj %>%
group_by(date) %>%
mutate(median=median(hosp_admits), model="median") %>%
filter(row_number()==1) %>%
ungroup() %>%
select(date, median, model)
##Convert to dataframe for conversion to time series object (xts)
median.proj <- as.data.frame(median.proj)
#Convert to time series objects
covid.xts <- xts(hosp_actuals[, c("weekly_admits")], as.Date(hosp_actuals$date, format="%Y-%m-%d"))
arima.xts <- xts(arima.hosp.proj[, c("hosp_admits")], arima.hosp.proj$date)
holts.xts <- xts(holts.hosp.proj[, c("hosp_admits")], holts.hosp.proj$date)
dampedholts.xts <- xts(dampedholts.hosp.proj[, c("hosp_admits")], dampedholts.hosp.proj$date)
nnetar.xts <- xts(nnetar.hosp.proj[, c("hosp_admits")], nnetar.hosp.proj$date)
median.xts <- xts(median.proj[, c("median")], median.proj$date)
#Merge datasets
df <- merge(covid.xts, arima.xts, holts.xts, dampedholts.xts, nnetar.xts, median.xts)
# df <- merge(hosp_actuals, arima.hosp.proj, holts.hosp.proj, dampedholts.hosp.proj, nnetar.hosp.proj)
# df$mean.proj <- rowMeans(df[, c("arima.xts", "holts.xts", "dampedholts.xts", "nnetar.xts")], na.rm=T)
# df$mean.proj <- ifelse(!is.na(df$covid.xts), NA, df$mean.proj) #comment out so ensemble line is aligned
#Line types for plotly plots
df$period <- ifelse(!is.na(df$covid.xts), "solid", "dot")
df$type <- ifelse(!is.na(df$covid.xts), "Est.", "Proj.")
df <- as.data.frame(df)
df$date <- as.Date(row.names(df), format="%Y-%m-%d")
# can.hosp.proj <- can.state.observed %>% select(date, hospitalBedsRequired) %>% filter(min_hosp <= date & date <= Sys.Date() + 30)
# IHME.hosp.proj <- IHME %>% select(date, allbed_mean) %>% filter(min_hosp <= date & date <= Sys.Date() + 30)
# mobs.hosp.proj <- mobs %>% select(2,8) %>% filter(min_hosp <= date & date <= Sys.Date() + 30)
# mit.hosp.proj <- mit %>% select(11,7) %>% filter(min_hosp <= date & date <= Sys.Date() + 30)
# covid.xts <- xts(hosp[,-1],hosp$Most.Recent.Date)
# can.proj.xts <- xts(can.hosp.proj[,-1],can.hosp.proj$date)
# ihme.proj.xts <- xts(IHME.hosp.proj[,-1],IHME.hosp.proj$date)
# mobs.proj.xts <- xts(mobs.hosp.proj[,-1],mobs.hosp.proj$date)
# mit.proj.xts <- xts(mit.hosp.proj[,-1],mit.hosp.proj$date)
# df <- merge(covid.xts,can.proj.xts,ihme.proj.xts,mobs.proj.xts,mit.proj.xts)
#
# df$mean.proj <- rowMeans(df[,2:5], na.rm = TRUE)
# df$mean.proj <- ifelse(!is.na(df$covid.xts), NA, df$mean.proj)
#
# df <- as.data.table(df) %>% as.data.frame()
return(df)
})
#Value Boxes
output$actual.hosp.box <- renderValueBox({
cdt <- max(covid_actuals$date)
current.hosp <- covid_actuals[which(covid_actuals$date==cdt), "weekly_admits"]
# current.hosp <- as.character(covid_actuals[which(covid_actuals$date == cdt & covid_actuals$state == state_name),"hosp_admits"])
valueBox(# "Actuals Go Here",
format(as.numeric(current.hosp), big.mark = ","),
paste0("Actuals: ",cdt), color = "black")
})
output$mean.proj.hosp.box <- renderUI({
cdt <- max(forecasts[which(forecasts$date <= Sys.Date()+30),]$date)
# cdt.ihme <- max( IHME[which(IHME$date <= Sys.Date() + 30),]$date )
mean.proj <- hosp.proj.ts() %>% slice(n()) %>% select(6)
valueBox(format(round(mean.proj, digits = 0), big.mark = ","), paste0("Mean 30-day forecast through ", cdt), color = "blue", width = 12)
})
#Graphs
output$hosp.proj.plot <- renderPlotly({
df <- hosp.proj.ts()
cdt <- max(covid_actuals$date)
p <- plot_ly(df,
hoverinfo = 'text') %>%
add_trace(x=df$date,
y = df$covid.xts,
name = "Actuals",
type = 'scatter',
mode = "lines+markers",
hoverinfo = 'text',
text = paste0(df$date,
"<br>",
"Actual Hospitalization: ", format(round(df$covid.xts,0), big.mark = ",") ),
line = list(color = "black"),
marker = list(color = "black", symbol= "circle")) %>%
add_trace(x = df$date,
y = df$arima.xts,
name = "ARIMA",
type = 'scatter',
mode = "lines",
inherit = TRUE,
line = list(color="orange"),
linetype = I("dot"),
hoverinfo = 'text',
text = paste0(df$date,
"<br>",
"ARIMA Estimate: ", format(round(df$arima.xts,0), big.mark = ","))) %>%
add_trace(x = df$date,
y = df$holts.xts,
name = "Holts",
type = 'scatter',
mode = "lines",
inherit = TRUE,
line = list(color="navy"),
linetype = I("dot"),
hoverinfo = 'text',
text = paste0(df$date,
"<br>",
"Holts Estimate: ", format(round(df$holts.xts,0), big.mark = ","))) %>%
add_trace(x = df$date,
y = df$dampedholts.xts,
name = "Damped Holts",
type = 'scatter',
mode = "lines",
inherit = TRUE,
line = list(color="red"),
linetype = I("dot"),
hoverinfo = 'text',
text = paste0(df$date,
"<br>",
"Damped Holts Estimate: ", format(round(df$nnetar.xts,0), big.mark = ","))) %>%
add_trace(x = df$date,
y = df$nnetar.xts,
name = "NNETAR",
type = 'scatter',
mode = "lines",
inherit = TRUE,
line = list(color="green"),
linetype = I("dot"),
hoverinfo = 'text',
text = paste0(df$date,
"<br>",
"NNETAR Estimate: ", format(round(df$nnetar.xts,0), big.mark = ","))) %>%
add_trace(x = df$date,
y = df$median.xts,
name = "Ensemble",
type = 'scatter',
mode = "lines",
inherit = TRUE,
linetype = I("solid"),
hoverinfo = 'text',
text = paste0(df$date,
"<br>",
"Ensemble Projection: ", format(round(df$median.xts,0), big.mark = ",") ),
line = list(color = '#2b8cbe', width = 5)
) %>%
layout(
title = NULL,
xaxis = list(title = NULL, showline = TRUE, showgrid = FALSE, zeroline = FALSE),
yaxis = list(title = "Weekly COVID-19 Hospital Admissions", showline = TRUE, showgrid = FALSE, zeroline = FALSE ),
margin = list(l = 100),
showlegend = TRUE,
shapes = list(type = "line",
y0 = 0,
y1 = 1,
yref = "paper",
x0 = cdt,
x1 = cdt,
line = list(color = "black", dash = 'dash')
)
)
return(p)
})
#Download file of Statewide Hospitalization Forecasts
output$dlhosp <- downloadHandler(
filename = function() { paste("Hospital_Forecasts_",Sys.Date(),'.csv', sep='') },
content = function(file) {
# Title
t <- c(paste("Statewide Hospitalization Forecasts", sep = ""),"","","","","","")
#Subtitle
tt <- c(paste("Communicable diseases Assessment Tool - Downloaded on",Sys.Date(), sep = " "),"","","","","","")
#Column labels
l <- c("Date","Actuals", "ARIMA","Holts","Damped Holts","NNETAR","Ensemble")
df <- hosp.proj.ts()%>% as.data.frame()
df <- df %>% select(date, names(df)[1:6])
df[,2:7] <- lapply(df[,2:7],function(x) round(x,2))
df[is.na(df)] <- 0
df[] <- lapply(df, as.character)
#Source
s <- c("Please see the Technical Notes tab of the application for data sources.","","","","","","")
p <- c("Prepared by: California Department of Public Health - COVID Modeling Team","","","","","","")
dlm <- rbind(t, tt, l, df, s, p)
write.table(dlm, file, row.names = F, col.names = F, quote = F, na= "NA", sep = ",")
})
#### County Hospitalization Projections ####
#Data Prep
# county.hosp <- reactive({
# progress <- Progress$new()
# # Make sure it closes when we exit this reactive, even if there's an error
# on.exit(progress$close())
# progress$set(message = "Gathering Hospitalization Forecasts", value = 0)
#
# cnty <- input$select.county.hosp
# progress$inc(3/4)
# # out <- lapply(cnty[1], function(x) get_can_cnty(x))
# # cnty.hosp <- do.call("rbind",out)
# out <- filter(can.county.observed, fips == cnty)
#
# cnty.hosp <- out %>% select(date,hospitalBedsRequired) %>% as.data.frame()
# progress$inc(1/4)
# return(cnty.hosp)
#
# })
#
##################################
##### COUNTS OF COUNTY BEDS #####
##### Add to global #####
##################################
# fc.cnty.beds <- reactive({
# c <- names(canfipslist[match(input$select.county.hosp,canfipslist)])
#
# if (c %in% cnty.beds[,1] == TRUE) {
# beds <- c(cnty.beds[which(cnty.beds$COUNTY == c),9])
# } else {
# beds <- c(NA)
# }
# })
# hosp.proj.cnty.ts <- reactive({
#
# c <- names(canfipslist[match(input$select.county.hosp,canfipslist)])
# min_hosp <- min(covid$Most.Recent.Date)
# hosp <- covid %>% select(Most.Recent.Date,COVID.19.Positive.Patients) %>% filter(covid$County.Name == c) %>% as.data.frame()
# can.hosp.proj <- county.hosp() %>% select(date, hospitalBedsRequired) %>% filter(min_hosp <= date & date <= Sys.Date() + 30)
#
# covid.xts <- xts(hosp[,-1],hosp$Most.Recent.Date)
# can.proj.xts <- xts(can.hosp.proj[,-1],can.hosp.proj$date)
#
# df <- merge(covid.xts,can.proj.xts)
#
# df <- as.data.table(df) %>% as.data.frame()
#
# df$period <- ifelse(!is.na(df$covid.xts), "solid", "dot")
# return(df)
#
# })
#
# #Value Boxes
# output$actual.cnty.hosp.box <- renderValueBox({
# c <- names(canfipslist[match(input$select.county.hosp,canfipslist)])
# cdt <- max(covid$Most.Recent.Date)
# current.hosp <- as.character(covid[which(covid$Most.Recent.Date == cdt & covid$County.Name == c),"COVID.19.Positive.Patients"])
# valueBox( "Counts/Beds Here",
# #paste0(format(as.numeric(current.hosp), big.mark = ","),"/",
# # #format(as.numeric(fc.cnty.beds()), big.mark = ",")
# # ),
# paste0("Actuals / Total Beds: ",cdt),
# color = "black")
# })
#
# output$mean.cnty.proj.hosp.box <- renderValueBox({
#
# cdt.ihme <- max( IHME[which(IHME$date <= Sys.Date() + 30),]$date )
# mean.proj <- hosp.proj.cnty.ts() %>% slice(n()) %>% select(3)
# valueBox( format(round(mean.proj, digits = 0), big.mark = ","),
# paste0("30-Day Forecast through ", cdt.ihme), color = "blue")
#
# })
#
# #Graph
# output$county.hosp.plot <- renderPlotly({
#
# df <- hosp.proj.cnty.ts()
#
# cdt <- max(df[which(!is.na(df$covid.xts)),1])
#
# today <- list(type = "line",
# y0 = 0,
# y1 = 1,
# yref = "paper",
# x0 = cdt,
# x1 = cdt,
# line = list(color = "black", dash = 'dash') )
#
# p <- plot_ly(df,
# hoverinfo = 'text') %>%
# add_trace(x = df[[1]],
# y = df[[2]],
# name = "Actuals",
# type = 'scatter',
# mode = "lines+markers",
# hoverinfo = 'text',
# text = paste0(df[[1]],
# "<br>",
# "Actual Hospitalization (PLACEHOLDER DATA - PLEASE REPLACE!!): ", format(df[[2]], big.mark = ",") ),
# line = list(color = "black"),
# marker = list(color = "black", symbol= "circle")
# ) %>%
# add_trace(x = df[[1]],
# y = df[[3]],
# name = "COVIDActNow - Proj.",
# type = 'scatter',
# mode = "lines",
# inherit = TRUE,
# line = list(color="orange"),
# linetype = ~I(period),
# hoverinfo = 'text',
# text = paste0(df[[1]],
# "<br>",
# "COVIDActNow Estimate: ", format(df[[3]], big.mark = ",") )
#
# ) %>%
# layout(
# title = as.character(counties[match(input$select.county.hosp, counties$fips),"county"]),
# xaxis = list(title = NULL, showline = TRUE, showgrid = FALSE, zeroline = FALSE ),
# yaxis = list(title = "Hospitalziations", showline = TRUE, showgrid = FALSE, zeroline = FALSE),
# margin = list(l = 100),
# showlegend = TRUE,
# shapes = list(today)
# )
# return(p)
#
# })
#
# #Download file of COUNTY Hospitalization Forecasts
# output$dlhosp.cnty <- downloadHandler(
#
# filename = function() { paste("Hospital_Forecasts_for_",names(canfipslist[match(input$select.county.hosp,canfipslist)]),Sys.Date(),'.csv', sep='') },
#
# content = function(file) {
#
# c <- names(canfipslist[match(input$select.county.hosp,canfipslist)])
# # Title
# t <- c(paste("Hospitalization Forecasts for ",c, sep = ""),"","","","","","")
# #Subtitle
# tt <- c(paste("COVID Assessment Tool - Downloaded on",Sys.Date(), sep = " "),"","","","","","")
# #Column labels
# l <- c("Date","Actuals", "COVIDActNow")
#
# df <- hosp.proj.cnty.ts()[,1:3] %>% as.data.frame()
# df[,2:3] <- lapply(df[,2:3],function(x) round(x,2))
# df[is.na(df)] <- 0
# df[] <- lapply(df, as.character)
#
# #Source
# s <- c("Please see the Technical Notes tab of the application for data sources.","","","","","","")
# p <- c(paste0("Prepared by: ",state_name," Department of Public Health"),"","","","","","")
#
# dlm <- rbind(t, tt, l, df, s, p)
# write.table(dlm, file, row.names = F, col.names = F, quote = F, na= "NA", sep = ",")
# })
} # End Server