-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
1738 lines (1512 loc) · 64.9 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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
shinyServer(function(input, output, session) {
# update dropdowns -----------------------------------------------------------
geo_list <- reactive({
req(input$level)
if(tolower(input$level) == "region") {
list <- dplyr::distinct(df_filt_by_date(), region) %>% rename(names = region)
} else if(tolower(input$level) == "country") {
list <- df_filt_by_date() %>%
dplyr::distinct(subregion_1)%>%
rename(names = subregion_1)
} else if(tolower(input$level) == "sub-national") {
if(!is.null(input$country)){
df <- filter(df_filt_by_date(), subregion_1 == input$country)
} else {
df <- df_filt_by_date()
}
list <- df %>%
dplyr::distinct(subregion_2) %>%
rename(names = subregion_2)
} else if(tolower(input$level) == "demonstration site"){
list <- demo_sites()
}
list <- filter(list, names != "TOTAL") %>% arrange(names)
dplyr::pull(list, names)
})
output$geography <- renderUI({
pickerInput(
"geography",
p(if_else(input$level == "Sub-national" & input$country == "India", "State",
if_else(input$level == "Demonstration site", "County/City",
"Geography")),
class = "input-label"),
choices = sort(geo_list()),
selected = ifelse(!is.null(input$geography), input$geography, "United States"),
options = list(`live-search` = TRUE, `dropup-auto` = FALSE)
)
})
output$country <- renderUI({
req(input$level)
pickerInput(
"country",
label = p("Country", class = "input-label"),
choices = sort(subnat_countries[subnat_countries != "TOTAL"]),
selected = ifelse(!is.null(input$country), input$country, subnat_countries[subnat_countries != "TOTAL"][[1]]),
options = list(`live-search` = TRUE, `dropup-auto` = FALSE)
)
})
district_list <- reactive({
if(is.null(input$country)){
"TOTAL"
} else if ((input$level != "Sub-national" | input$country != "India")) {
"TOTAL"
} else {
list <- filter(df_filt_by_date(),
subregion_2 == input$geography,
subregion_1 == input$country) %>%
dplyr::distinct(subregion_3) %>%
rename(names = subregion_3) %>%
arrange(names)
dplyr::pull(list, names)
}
})
output$district <- renderUI({
pickerInput(
"district",
p("District", class = "input-label"),
selected = "TOTAL",
choices = sort(district_list()),
options = list(`live-search` = TRUE, `dropup-auto` = FALSE))
})
output$change_over <- renderUI({
pickerInput(
inputId = "change_over",
p("Show change over", class = "input-label"),
choices = TIME_LEVELS,
selected ="Three months",
options = list(`live-search` = TRUE)
)
})
output$downloadPDF <- downloadHandler(
filename = "COVID-19 Testing Dashboard Data Sources.pdf",
content = function(file){
file.copy("www/2022-02-11 data sources description.pdf", file)
}
)
# update data based on changing inputs ---------------------------------------
# KPI and behavior info only: modify info text based on region of selected page
output$kpi_header <- renderUI({
req(nrow(df_selected())>0)
region <- df_selected() %>% dplyr::select(region) %>% unique()
if (region == "India") {
kpi_text <- kpi_text_india
}else if (region == "United States") {
kpi_text <- kpi_text_usa
}else if (region == "Africa") {
kpi_text <- kpi_text_africa
}else {
kpi_text <- kpi_text_other
}
sectionHeader("Healthcare capacity and equity", info = kpi_text, up = TRUE)
})
start_date <- reactive({
req(input$date, input$change_over)
get_start_date(input$date, input$change_over)
})
observe({
req(!start_date() %in% DATE_RANGE)
shinyalert("Note:", paste0(input$change_over, " of data prior to ", input$date, " not available."), type = "warning")
})
output$display_date <- renderUI({
req(input$level != "Demonstration site")
HTML(paste("<h4 style = 'font-size: 16px; z-index:1000; padding:4px;'><b>",
"Most recent available data as of",
paste0(gsub(" 0", " ", format(as.Date(input$date), "%b %d")),
","),
lubridate::year(input$date),
"</b></h4>"
))
})
output$dpmtitle <- renderUI({
req(!is.na(input$geography) & !is.na(input$level))
geo_title_create(one_country_regions, input$level, input$geography,
"Deaths per million per day (7-day avg.",
" of available country data",
end_text = ")")
})
output$cfrtitle <- renderUI({
req(!is.na(input$geography) & !is.na(input$level))
geo_title_create(one_country_regions, input$level, input$geography,
"Case fatality ratio (30-day avg.",
" of available country data",
end_text = ")")
})
output$cvititle <- renderUI({
req(!is.na(input$geography) & !is.na(input$level))
geo_title_create(one_country_regions, input$level, input$geography,
"2020 COVID Vulnerability Index",
" (Average of available countries' annual data)")
})
output$ehsititle <- renderUI({
req(!is.na(input$geography) & !is.na(input$level))
geo_title_create(one_country_regions, input$level, input$geography,
"2019 Essential Health Services Index",
" (Average of available countries' annual data)")
})
output$dtp3title <- renderUI({
req(!is.na(input$geography) & !is.na(input$level))
geo_title_create(one_country_regions, input$level, input$geography,
"2019 DTP3 vaccination (%",
", Average of available countries' annual data",
end_text = ")")
})
output$tprtitle <- renderText({
req(!is.na(input$geography) & !is.na(input$level))
geo_title_create(one_country_regions, input$level, input$geography,
"Test positivity rate (7-day avg.",
" of available country data",
end_text = ")")
})
output$casetitle <- renderText({
req(!is.na(input$geography) & !is.na(input$level))
geo_title_create(one_country_regions, input$level, input$geography,
"New cases per million per day (7-day avg.",
" of available country data",
end_text = ")")
})
output$testtitle <- renderText({
req(!is.na(input$geography) & !is.na(input$level))
geo_title_create(one_country_regions, input$level, input$geography,
"Tests conducted per million per day (7-day avg.",
" of available country data",
end_text = ")")
})
output$lmictitle <- renderText({
req(!is.na(input$geography) & !is.na(input$level))
geo_title_create(one_country_regions, input$level, input$geography,
"Proportion vaccinated in all low- and middle-income countries globally (all ages, at least one dose",
"",
end_text = ")")
})
output$vpeopletitle <- renderText({
req(!is.na(input$geography) & !is.na(input$level))
if (input$geography == "United States" |
(input$level == "Sub-national" & input$country == "United States")) {
text <- "Proportion vaccinated (full population, all ages"
}
else {
text <- "Proportion vaccinated (all ages"
}
geo_title_create(one_country_regions, input$level, input$geography,
text,
", assumes 0 vaccinations for countries with no reported data",
end_text = ")")
})
output$bipoc_pct_vacc_esttitle <- renderText({
req(!is.na(input$geography) & !is.na(input$level))
geo_title_create(one_country_regions, input$level, input$geography,
"Proportion BIPOC vaccinated (ages 5+, at least one dose), estimated")
})
output$demo_vpeopletitle <- renderText({
req(!is.na(input$geography) & !is.na(input$level))
req(df_demo_selected())
if (unique(df_demo_selected()$county) == "Baltimore") geo_label <- "city"
else geo_label <- "county"
geo_title_create(one_country_regions, input$level, input$geography,
paste0("Proportion fully vaccinated in ", unique(df_demo_selected()$county) , " ", geo_label, " (full population, all ages, at least one dose)"),
end_text = "")
})
output$bipoc_vacc_state_title <- renderText({
req(input$level, input$geography, demo_state())
geo_title_create(one_country_regions, input$level, input$geography,
paste0("Proportion of BIPOC population vaccinated in ", demo_state() , " (ages 5+): county data not available"),
end_text = "")
})
output$race_state_title <- renderText({
req(input$level, input$geography, demo_state())
geo_title_create(one_country_regions, input$level, input$geography,
paste0("Proportion vaccinated in ", demo_state() , " by race/ethnicity (ages 5+), estimated: county data not available"),
end_text = "")
})
output$vacc_accept_msa_title <- renderText({
req(input$level, input$geography, demo_state(), df_demo_selected())
shiny::validate(
need(nrow(df_demo_selected()) > 0,
"")
)
metro_long <- unique(df_demo_selected()$metro_long)
# show time series title ending with "over time" if there
# are multiple data points or if there are no valid data points
# to display
if (line_bool_demo() | nrow(df_demo_valid_dates()) == 0) {
# time series
text <- get_line_bar_title(TRUE, NULL, vac_map_census, input$level, input$geography, metro_long = metro_long)
} else{
# bar chart
df_last_day <- get_last_day_data(df_demo_valid_dates())
text <- get_line_bar_title(FALSE, df_last_day, vac_map_census, input$level, input$geography, metro_long = metro_long)
}
text
})
output$inptitle <- renderUI({
shiny::validate(
need(nrow(df_selected() %>% filter(!is.na(tpr))) > 0,
"")
)
p("Inpatient beds occupied",
style = sub_chart_title_style)
})
output$icutitle <- renderUI({
shiny::validate(
need(nrow(df_selected() %>% filter(!is.na(tpr))) > 0,
"")
)
p("ICU beds occupied",
style = sub_chart_title_style)
})
vax_elig_title <- reactive({
req(input$level, input$geography)
geo_title_create(one_country_regions, input$level, input$geography,
"Proportion who would accept a Covid-19 vaccine",
" (of available country data)")
})
output$vaxacctitle <- renderText({
req(!is.na(input$geography) & !is.na(input$level))
vax_elig_title()
})
output$vaxacctitleregional <- renderText({
req(!is.na(input$geography) & !is.na(input$level))
vax_elig_title()
})
output$vaxeligtitle <- renderText({
req(!is.na(input$geography) & !is.na(input$level))
paste0(geo_title_create(one_country_regions, input$level, input$geography,
"Vaccine eligibility",
" (of available country data)"),
" as of ", vax_elig_date())
})
output$vaxhesttitle <- renderText({
req(!is.na(input$geography) & !is.na(input$level))
geo_title_create(one_country_regions, input$level, input$geography,
"Main reasons driving vaccine hesitancy
(reported by unvaccinated people who are not \"definitely\" planning to get vaccinated",
", of available country data",
end_text = ")")
})
output$strucbartitle <- renderText({
req(!is.na(input$geography) & !is.na(input$level))
geo_title_create(one_country_regions, input$level, input$geography,
"Structural barriers to vaccination (reported by the vaccinated and those trying to get vaccinated",
", of available country data",
end_text = ")")
})
# update data based on changing inputs ---------------------------------------
df_filt_by_date <- reactive({
req(input$date, input$change_over)
filter_dashboard_dates(df_dashboard, input$date, input$change_over)
})
df_demo_filt_by_date <- reactive({
req(input$date, input$change_over)
filter_dashboard_dates(df_demo_site, input$date, input$change_over)
})
demo_sites <- reactive({
req(df_demo_filt_by_date())
df_demo_filt_by_date() %>%
dplyr::distinct(subregion_3) %>%
rename(names = subregion_3)
})
df_demo_selected <- reactive({
req(input$level, input$geography, demo_sites(),
input$level == "Demonstration site",
input$geography %in% (demo_sites() %>% pull())
)
df_demo_filt_by_date() %>% filter(subregion_3 == input$geography)
})
demo_state <- reactive({
req(df_demo_selected())
df_demo_selected() %>% dplyr::select(subregion_2) %>% unique() %>% pull()
})
df_selected <- reactive({
req(input$level, input$geography)
if(input$level == "Demonstration site"){
req(df_demo_selected())
}
get_df_selected(input$level, input$geography, input$country,
input$district, df_filt_by_date(), demo_state())
})
df_valid_dates <- reactive({
req(df_selected())
survey_conducted <- df_selected() %>%
filter(!is.na(recent_behav_date)) %>%
dplyr::select(all_of(vac_metrics), all_of(beh_metrics), recent_behav_date) %>%
unique()
filtered <- survey_conducted %>%
filter(recent_behav_date >= start_date(), recent_behav_date <= input$date)
if(nrow(filtered) == 0){
get_last_day_data(survey_conducted)
} else {
filtered
}
})
df_demo_valid_dates <- reactive({
req(df_demo_selected())
survey_conducted <- df_demo_selected() %>%
filter(!is.na(recent_behav_date)) %>%
dplyr::select(vaccine_accept, recent_behav_date) %>%
unique()
filtered <- survey_conducted %>%
filter(recent_behav_date >= start_date(), recent_behav_date <= input$date)
if(nrow(filtered) == 0){
get_last_day_data(survey_conducted)
} else {
filtered
}
})
line_bool <- reactive({
req(df_valid_dates(), input$level, input$geography)
get_line_bool(df_valid_dates(), input$level, input$geography)
})
line_bool_demo <- reactive({
req(df_demo_valid_dates(), input$level, input$geography)
get_line_bool(df_demo_valid_dates(), input$level, input$geography)
})
# reactive values ------------------------------------------------------------
test_threshold1 <- reactive({
# determine the threshold for a "good" level of tests
# based on the level of cases in the selected date and geography.
val <- df_selected() %>% filter(date == input$date) %>% pull(cases_per_mil)
max(val*test_threshold_case_multiplier, test_threshold_minimum, na.rm = TRUE)
})
test_high_threshold <- reactive({
# determine the threshold for a "very bad" level of
# tests based on the level of cases in the selected date
# and geography, uses the threshold defined in the global
val <- df_selected() %>% filter(date == input$date) %>% pull(cases_per_mil)
max(val*test_threshold_multiplier_low, test_threshold_minimum, na.rm = TRUE)
})
test_threshold2 <- reactive({
#adding a value in the middle to add a new category
mean(test_threshold1(),test_high_threshold())
})
# dynamic text ---------------------------------------------------------------
output$display_geo <- renderText({
# display the geography, unless a specific district is selected
# in which case display the district
if(input$level == "Demonstration site"){
paste(input$geography, demo_state(), sep=", ")
} else if (is.null(input$district)) {
input$geography
} else if (input$district == "TOTAL") {
input$geography
} else {
input$district
}
})
output$demo_display_geo <- renderText({
# display the geography, unless a specific district is selected
# in which case display the district
if(input$level == "Demonstration site"){
paste(input$geography, demo_state(), sep=", ")
} else if (is.null(input$district)) {
input$geography
} else if (input$district == "TOTAL") {
input$geography
} else {
input$district
}
})
output$display_pop <- renderText({
if(input$level == "Demonstration site"){
req(nrow(df_demo_selected()) > 0)
pop_data <- df_demo_selected() %>%
mutate(display_population = population)
} else {
req(nrow(df_selected()) > 0)
pop_data <- df_selected()
}
pop <- pop_data %>%
filter(date == input$date) %>%
pull(if_else((input$level == "Region" & !(input$geography %in% one_country_regions)),
display_population,
population))
shiny::validate(need(!is.na(pop), "") # display nothing if we are missing population for the geography
)
paste("Population", format(round(pop/100000)/10, big.mark=",", scientific = FALSE), "million")
})
output$demo_display_pop <- renderText({
if(input$level == "Demonstration site"){
req(nrow(df_demo_selected()) > 0)
pop_data <- df_demo_selected() %>%
mutate(display_population = population)
} else {
req(nrow(df_selected()) > 0)
pop_data <- df_selected()
}
pop <- pop_data %>%
filter(date == input$date) %>%
pull(if_else((input$level == "Region" & !(input$geography %in% one_country_regions)),
display_population,
population))
shiny::validate(need(!is.na(pop), "") # display nothing if we are missing population for the geography
)
paste("Population", format(round(pop/100000)/10, big.mark=",", scientific = FALSE), "million")
})
output$as_of <- renderText({
"As of"
})
output$sah_display_date <- renderText( {
req(nrow(df_recent_sah()) > 0)
display_date(input$date, df_recent_sah()$date[1])
})
output$restr_gath_display_date <- renderText( {
req(nrow(df_recent_restr_gath()) > 0)
display_date(input$date, df_recent_restr_gath()$date[1])
})
output$school_closing_display_date <- renderText( {
req(nrow(df_recent_school_closing()) > 0)
display_date(input$date, df_recent_school_closing()$date[1])
})
output$req_mask_display_date <- renderText( {
req(nrow(df_recent_req_mask()) > 0)
display_date(input$date, df_recent_req_mask()$date[1])
})
output$display_change <- renderText({
req(input$change_over)
if (input$change_over != "Since pandemic began") {
paste("Change over", tolower(input$change_over))
} else {
paste("Change", tolower(input$change_over))
}
})
output$recent_date <- renderText({
req(df_selected(), input$date)
# if no data, display today's date
if(nrow(filter(df_selected(), !is.na(deaths_per_mil))) == 0)
display_date(today, input$date)
# else display recent date with valid data
else get_nmiss_date(df_selected(), deaths_per_mil, today)
})
output$deaths_val <- renderText({
req(nrow(df_selected()) > 0)
format(
get_nmiss_val(df_selected(), deaths_per_mil),
big.mark=",", digits =2, scientific = FALSE
)
})
output$cfr_pct <- renderText({
shiny::validate(
need(nrow(df_selected() %>% filter(!is.na(cfr))) > 0,
paste("Insufficient CFR data for", input$geography, "in this time period."))
)
get_nmiss_trend(df_selected(), cfr)
})
output$cfr_val <- renderText({
shiny::validate(
need(nrow(df_selected() %>% filter(!is.na(cfr))) > 0,
paste("Insufficient CFR data for", input$geography, "in this time period."))
)
df <- filter(df_selected(),(as.numeric(date - ymd(input$date)) %% 7) == 0)
scales::percent(get_nmiss_val(df, cfr), accuracy = .1)
})
output$deaths_pct <- renderText({
req(nrow(df_selected()) > 0)
paste(get_nmiss_trend(df_selected(), deaths_per_mil))
})
# policy indicators ----------------------------------------------------------
df_recent_sah <- reactive({
req(nrow(df_selected()) > 0, input$date, input$level, input$geography)
get_recent_policy(df_selected(), df_dashboard, "sah", input$date, input$level, input$geography)
})
output$sah <- renderUI({
req(input$level, input$geography, df_recent_sah(), input$level != "Demonstration site")
get_policy_icon(df_recent_sah(), "sah", df_recent_sah()$date[1], input$level, input$geography)
})
df_recent_restr_gath <- reactive({
req(nrow(df_selected()) > 0, input$date, input$level, input$geography)
get_recent_policy(df_selected(), df_dashboard, "restr_gath", input$date, input$level, input$geography)
})
output$restr_gath <- renderUI({
req(input$level, input$geography, df_recent_restr_gath(), input$level != "Demonstration site")
get_policy_icon(df_recent_restr_gath(), "restr_gath", df_recent_restr_gath()$date[1], input$level, input$geography)
})
df_recent_school_closing <- reactive({
req(nrow(df_selected()) > 0 , input$date, input$level, input$geography)
get_recent_policy(df_selected(), df_dashboard, "school_closing", input$date, input$level, input$geography)
})
output$school_closing <- renderUI({
req(input$level, input$geography, df_recent_school_closing(), input$level != "Demonstration site")
get_policy_icon(df_recent_school_closing(), "school_closing", df_recent_school_closing()$date[1], input$level, input$geography)
})
df_recent_req_mask <- reactive({
req(nrow(df_selected()) > 0, input$date, input$level, input$geography)
get_recent_policy(df_selected(), df_dashboard, "req_mask", input$date, input$level, input$geography)
})
output$req_mask <- renderUI({
req(input$level, input$geography, df_recent_req_mask(), input$level != "Demonstration site")
get_policy_icon(df_recent_req_mask(), "req_mask", df_recent_req_mask()$date[1], input$level, input$geography)
})
# figures --------------------------------------------------------------------
duration_dates <- reactive({
case_when(
input$change_over == "Two weeks" ~ weeks(2),
input$change_over == "One month" ~ months(1),
input$change_over == "Three months" ~ months(3),
input$change_over == "Six months" ~ months(6),
input$change_over == "Twelve months" ~ months(12),
input$change_over == "Eighteen months" ~ months(18),
input$change_over == "Since the pandemic began" ~ as.period(ymd(input$date) - ymd("2020-03-01"))
)
})
output$death_trend <- renderPlotly({
shiny::validate(
need(nrow(df_selected() %>% filter(!is.na(deaths_per_mil))) > 0,
paste("Insufficient death data for", input$geography, "in this time period."))
)
trend_only(df_selected(),
deaths_per_mil,
date_range = c(ymd(input$date) %m-% duration_dates(), ymd(input$date))
)
})
output$cfr_trend <- renderPlotly({
df_weekly <- filter(df_selected(),
# limit to data every 7 days from the end date
(as.numeric(date - ymd(input$date)) %% 7) == 0 )
shiny::validate(
need(nrow(df_weekly %>% filter(!is.na(cfr))) > 1,
paste("Insufficient CFR data for", input$geography, "in this time period."))
)
trend_only(df_weekly,
cfr,
date_range = c(ymd(input$date) %m-% duration_dates(), ymd(input$date)),
trend_length = 1,
tooltip_acc = 0.1,
percent = TRUE)
})
output$tpr_fig <- renderPlotly({
shiny::validate(
need(nrow(df_selected() %>% filter(!is.na(tpr))) > 0,
paste("Insufficient TPR data for", input$geography, "in this time period."))
)
line_graph(df_selected(),
"tpr",
tpr_threshold1,
duration = input$change_over,
label = "Percent",
acc = .1,
label_opt = "percent",
tooltip_acc = .1)
})
output$tests_fig <- renderPlotly({
shiny::validate(
need(nrow(df_selected() %>% filter(!is.na(tests_per_mil))) > 0,
paste("Insufficient test data for", input$geography, "in this time period."))
)
line_graph(df_selected(),
"tests_per_mil",
test_threshold1(),
duration = input$change_over,
label = "Tests",
acc = 1,
tooltip_acc = 0,
label_opt = "numeric",
trim_target = TRUE)
})
output$cases_fig <- renderPlotly({
shiny::validate(
need(nrow(df_selected() %>% filter(!is.na(cases_per_mil))) > 0,
paste("Insufficient case data for", input$geography, "in this time period."))
)
line_graph(df_selected(),
"cases_per_mil",
cases_threshold1,
duration = input$change_over,
label = "Cases",
acc = .1,
tooltip_acc = 0,
label_opt = "numeric",
target_label_down = FALSE)
})
output$lmic_fig <- renderPlotly({
shiny::validate(
need(nrow(df_selected() %>% filter(!is.na(LMIC_vacc_pct))) > 0,
paste("Insufficient case data for", input$geography, "in this time period."))
)
line_graph(df_selected(),
"LMIC_vacc_pct",
threshold=vpeople_display_threshold,
duration = input$change_over,
label = "Percent",
acc = .1,
tooltip_acc = 1,
label_opt = "percent")
})
output$prop_vacc_county_figure <- renderPlotly({
# vpeople_fig
req(df_demo_selected())
create_multi_time_series(df_demo_selected(), start_date(), input$date,
c("all ages" = "cum_pct_people_fully_vacc"),
input$change_over,
c("black"),
y_lab = "Proportion fully vaccinated",
showlegend = FALSE)
})
output$prop_vacc_county_err <- renderText({
req(df_demo_selected())
paste("Insufficient vaccination data for", input$geography, "in this time period.")
})
observe({
req(df_demo_selected())
validate_mod(name = "prop_vacc_county",
condition =
(nrow(df_demo_selected() %>% filter(!is.na(cum_pct_people_fully_vacc)) > 0))
)
})
output$vacc_accept_msa_figure <- renderPlotly({
# vac_figure
req(df_demo_valid_dates(), start_date(),
nrow(df_demo_valid_dates() %>% dplyr::filter(!is.na(vaccine_accept))) > 0)
behServer(df_demo_valid_dates(), line_bool_demo(), start_date(), input$date,
vac_map_census, vac_map_census_wrap,
input$change_over, c("black"), input$level, input$geography,
y_lab = "Percentage of\nunvaccinated respondents", wrap = TRUE,
legend_pos = list(orientation = "h", y = -0.1),
target = vaccine_accept_threshold)
})
output$vacc_accept_msa_err <- renderText({
req(df_demo_valid_dates())
paste("Insufficient vaccine acceptance data for", input$geography, "in this time period.")
})
observe({
req(df_demo_valid_dates(), start_date())
validate_mod(name = "vacc_accept_msa",
condition =
(nrow(df_demo_valid_dates()) > 0))
})
output$vpeople_fig <- renderPlotly({
propVaccServer(df_selected(), vpeople_map,
input$geography, input$change_over, threshold = vpeople_display_threshold,
colors = c('black', dark_grey, light_grey),
booster_fully_vac_threshold = booster_fully_vac_threshold)
})
output$bipoc_pct_vacc_est_fig <- renderPlotly({
propVaccServer(df_selected(), "bipoc_pct_vacc_est", input$geography, input$change_over,
threshold = vpeople_display_threshold)
})
vax_acc_plot <- reactive({
req((nrow(df_selected() %>% filter(!is.na(vaccinated_appointment_or_accept))) > 0) |
(nrow(df_selected() %>% filter(!is.na(appointment_or_accept_covid_vaccine))) > 0),
input$change_over)
line_graph(df_selected(),
prop_metric = prob_def_vacc_map,
threshold = vacc_acc_threshold,
label = "Percent",
duration = input$change_over,
acc = .1,
label_opt = "percent",
tooltip_acc = .1,
show_threshold = TRUE,
colors = c("black", dark_grey))
})
output$vax_acc_fig <- renderPlotly({
# need either "vaccinated_appointment_or_accept" or "appointment_or_accept_covid_vaccine"
# to have at least 1 row of data
shiny::validate(
need((nrow(df_selected() %>% filter(!is.na(vaccinated_appointment_or_accept))) > 0) |
(nrow(df_selected() %>% filter(!is.na(appointment_or_accept_covid_vaccine))) > 0),
paste("Insufficient survey data for", input$geography, "in this time period."))
)
vax_acc_plot()
})
output$vax_acc_fig_regional <- renderPlotly({
# need either "vaccinated_appointment_or_accept" or "appointment_or_accept_covid_vaccine"
# to have at least 1 row of data
shiny::validate(
need((nrow(df_selected() %>% filter(!is.na(vaccinated_appointment_or_accept))) > 0) |
(nrow(df_selected() %>% filter(!is.na(appointment_or_accept_covid_vaccine))) > 0),
paste("Insufficient survey data for", input$geography, "in this time period."))
)
vax_acc_plot()
})
# map ------------------------------------------------------------------------
output$map <- renderLeaflet({
req(input$level, input$date, input$geography)
req(nrow(df_selected()) > 0)
req(test_high_threshold(), test_threshold1())
if(input$level == "Sub-national"){
req(input$country)
req(input$district)
}
if(input$level == "Demonstration site"){
geo <- demo_state()
lvl <- "Sub-national"
country <- unique(df_selected()$subregion_1)
site_name <- input$geography
} else {
geo <- input$geography
lvl <- input$level
country <- input$country
site_name <- NULL
}
map_geo(lvl, geo, country, input$district,
input$date, df_selected(), test_high_threshold(), test_threshold1(), test_threshold2(), site_name)
})
output$demo_map <- renderLeaflet({
req(input$level, input$date, input$geography)
req(nrow(df_selected()) > 0)
req(test_high_threshold(), test_threshold1())
if(input$level == "Sub-national"){
req(input$country)
req(input$district)
}
if(input$level == "Demonstration site"){
geo <- demo_state()
lvl <- "Sub-national"
country <- unique(df_selected()$subregion_1)
site_name <- input$geography
} else {
geo <- input$geography
lvl <- input$level
country <- input$country
site_name <- NULL
}
map_geo(lvl, geo, country, input$district,
input$date, df_selected(), test_high_threshold(), test_threshold1(), test_threshold2(), site_name)
})
observe({
req(nrow(df_selected()) != 0, input$level != "Demonstration site", input$geography != 'United States',
!(input$level == 'Sub-national' & input$country == 'United States'))
val <- filter(df_selected(), date == input$date)$rel_cvi*100
kpiServer(id = "cvi", indicator_value = val,
format = ".1f", tick_format = ".0f",
rev = "false", min = 0, max = 100)
})
observe({
req(nrow(df_selected()) != 0, input$level != "Demonstration site", input$geography != 'United States',
!(input$level == 'Sub-national' & input$country == 'United States'))
val <- filter(df_selected(), date == input$date)$sci
kpiServer(id = "ehsi", indicator_value = val,
format = ".0f", tick_format = ".0f",
rev = "true", min = 0, max = 100) # rev = true implies lower = worse
})
observe({
req(nrow(df_selected()) != 0, input$level != "Demonstration site", input$geography != 'United States',
!(input$level == 'Sub-national' & input$country == 'United States'))
val <- filter(df_selected(), date == input$date)$dtp3*100
kpiServer(id = "dtp3", indicator_value = val,
format = ".0f", tick_format = ".0f",
rev = "true", min = 0, max = 100) # rev = true implies lower = worse
})
observe({
req(nrow(df_selected()) != 0, input$level != "Demonstration site")
val <- filter(df_selected(), date == input$date)$stringency_index
kpiServer(id = "polstr", indicator_value = val,
format = ".0f", tick_format = ".0f",
rev = "false", min = 0, max = 100)
})
# update the map legend
observeEvent(c(input$level, input$map_groups, input$date, input$geography), {
map <- leafletProxy("map") %>% clearControls()
if (any(input$map_groups %in% "Proportion BIPOC vaccinated (ages 5+, at least one dose), estimated")) {
map <- map %>%
addLegend(data = map,
colors = c(red, orange, yellow, navy, light_grey),
labels = c(paste0("Less than ", scales::percent(bipoc_vacc_pct_threshold1, digits = 0)),
paste0(scales::percent(bipoc_vacc_pct_threshold1, digits = 0), " to ", scales::percent(bipoc_vacc_pct_threshold2-.01, digits = 0)),
paste0(scales::percent(bipoc_vacc_pct_threshold2, digits = 0), " to ", scales::percent(bipoc_vacc_pct_threshold3-.01, digits = 0)),
paste0("Greater than or equal to ", scales::percent(bipoc_vacc_pct_threshold3, digits = 0)),
"No data"),
group = "Proportion BIPOC vaccinated (ages 5+, at least one dose), estimated",
position = "topleft")
}
if (any(input$map_groups %in% "New cases per million per day (7-day avg.)")) {
map <- map %>%
addLegend(data = map,
colors = c(navy, yellow, orange, red, light_grey),
labels = c(paste0("Less than ",format(cases_threshold1, big.mark=",", digits =0, scientific = FALSE)),
paste0(format(cases_threshold1, big.mark=",", digits =0, scientific = FALSE), " to ", format(cases_threshold2-1, big.mark=",", digits =0, scientific = FALSE)),
paste0(format(cases_threshold2, big.mark=",", digits =0, scientific = FALSE), " to ", format(cases_high_threshold-1, big.mark=",", digits =0, scientific = FALSE)),
paste0("Greater than or equal to ", format(cases_high_threshold, big.mark=",", digits =0, scientific = FALSE)),
"No data"),
group = "New cases per million per day (7-day avg.)",
position = "topleft")
}
if (any(input$map_groups %in% "Tests conducted per million per day (7-day avg.)")) {
map <- map %>%
addLegend(data = map,
colors = c(navy, yellow, orange, red, light_grey),
labels = c("Sufficient given case counts",
"Marginal given case counts",
"Low given case counts",
"Very low given case counts",
"No data"),
group = "Tests conducted per million per day (7-day avg.)",
position = "topleft")
}
if(any(input$map_groups %in% "Test positivity rate (7-day avg.)")) {
map <- map %>%
addLegend(data = map,
colors = c(navy, yellow, orange, red, light_grey),
labels = c(paste0("Less than ", scales::percent(tpr_threshold1, accuracy = 0.1)),
paste0(scales::percent(tpr_threshold1, accuracy = 0.1), " to ", scales::percent(tpr_threshold2-0.001, accuracy = 0.1)),
paste0(scales::percent(tpr_threshold2, accuracy = 0.1), " to ", scales::percent(tpr_high_threshold-0.001, accuracy = 0.1)),
paste0("Greater than or equal to ", scales::percent(tpr_high_threshold, accuracy = 0.1)),
"No data"),
group = "Test positivity rate (7-day avg.)",
position = "topleft")
}
if(any(input$map_groups %in% "Proportion vaccinated (full population, all ages, at least one dose)")) {
map <- map %>%
addLegend(data = map,
colors = c(red, orange, yellow, navy, light_grey),
labels = c(paste0("Less than ", scales::percent(vpeople_threshold1, digits = 0)),
paste0(scales::percent(vpeople_threshold1, digits = 0), " to ", scales::percent(vpeople_threshold2-.01, digits = 0)),
paste0(scales::percent(vpeople_threshold2, digits = 0), " to ", scales::percent(vpeople_high_threshold-.01, digits = 0)),
paste0("Greater than or equal to ", scales::percent(vpeople_high_threshold, digits = 0)),
"No data"),
group = "Proportion vaccinated (full population, all ages, at least one dose)",
position = "topleft")
}
})
# modify button text based on level
observeEvent(c(input$level, input$country, input$district), {
if (input$level == "Country") {
return_label <- "Return to region"
} else if(input$level == "Sub-national"){
req(input$country, input$district)
if(input$country != "India" | input$district == "TOTAL"){
return_label <- "Return to country"
} else {
return_label <- "Return to state"
}
} else {
return_label <- "Return to state"
}
updateActionButton(session = session, inputId = "return",
label = return_label)
updateActionButton(session = session, inputId = "demo_return",
label = return_label)