-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathapp.r
734 lines (526 loc) · 27.2 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
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
library(shiny)
library(leaflet)
library(RColorBrewer)
library(rgdal)
library(RCurl)
library(plotly)
library(viridis)
library(tidyverse)
variable <-F
# names(data)
url <- "https://twitter.com/intent/tweet?url=https://thibautfabacher.shinyapps.io/covid-19"
#
# https://www.naturalearthdata.com/downloads/50m-cultural-vectors/50m-admin-0-countries-2/
# countries <- readOGR(dsn ="ne_50m_admin_0_countries",
# layer = "ne_50m_admin_0_countries",
# encoding = "utf-8",use_iconv = T,
# verbose = FALSE)
# save(countries, file="shapeFile.RData")
load("shapeFile.RData")
dataCook<- function(data, pop, countries){
data$`Country/Region`<-as.character(data$`Country/Region`)
data$`Country/Region`[data$`Country/Region`=="Macau"]<- "Macao"
data$`Country/Region`[data$`Country/Region`=="Mainland China"]<- "China"
data$`Country/Region`[data$`Country/Region`=="South Korea"]<- "South Korea"
data$`Country/Region`[data$`Country/Region`=="North Macedonia"]<- "Macedonia"
data$`Country/Region`[data$`Country/Region`=="Czech Republic"]<- "Czechia"
data$`Country/Region`[data$`Country/Region`=="Dominican Republic"]<- "Dominican Rep."
data$`Country/Region`[data$`Country/Region`=="UK"]<- "United Kingdom"
data$`Country/Region`[data$`Country/Region`=="Gibraltar"]<- "United Kingdom"
data$`Country/Region`[data$`Country/Region`=="US"]<- "United States"
data$`Country/Region`[data$`Country/Region`=="Saint Barthelemy"]<- "St-Barthélemy"
data$`Country/Region`[data$`Country/Region`=="Faroe Islands"]<- "Faeroe Is."
data$`Country/Region`[data$`Country/Region`=="Bosnia and Herzegovina"]<- "Bosnia and Herz."
data$`Country/Region`[data$`Country/Region`=="Vatican City"]<- "Vatican"
data$`Country/Region`[data$`Country/Region`=="Korea, South"]<- "South Korea"
data$`Country/Region`[data$`Country/Region`=="Republic of Ireland"]<- "Ireland"
data$`Country/Region`[data$`Country/Region`=="Taiwan*"]<-"Taiwan"
data$`Country/Region`[data$`Country/Region`=="Congo (Kinshasa)"]<-"Congo"
data$`Country/Region`[data$`Country/Region`=="Cote d'Ivoire"]<-"Côte d'Ivoire"
data$`Country/Region`[data$`Country/Region`=="Reunion"]<-"France"
data$`Country/Region`[data$`Country/Region`=="Martinique"]<-"France"
data$`Country/Region`[data$`Country/Region`=="French Guiana"]<-"France"
data$`Country/Region`[data$`Country/Region`=="Holy See"]<-"Vatican"
data$`Country/Region`[data$`Country/Region`=="Cayman Islands"]<-"Cayman Is."
data$`Country/Region`[data$`Country/Region`=="Guadeloupe"]<-"France"
data$`Country/Region`[data$`Country/Region`=="Antigua and Barbuda"]<-"Antigua and Barb."
data$`Country/Region`[data$`Country/Region`=="Curacao"]<-"Curaçao"
data$`Country/Region`[data$`Country/Region`=="Guadeloupe"]<-"France"
data$`Country/Region`[data$`Country/Region`=="occupied Palestinian territory"]<-"Palestine"
data$`Country/Region`[data$`Country/Region`=="Congo (Brazzaville)"]<-"Congo"
data$`Country/Region`[data$`Country/Region`=="Equatorial Guinea"]<-"Guinea"
data$`Country/Region`[data$`Country/Region`=="Central African Republic"]<-"Central African Rep."
data$`Country/Region`[data$`Country/Region`=="South Sudan"]<-"S. Sudan"
data$`Country/Region`[data$`Country/Region`=="Eswatini"]<-"eSwatini"
data$`Country/Region`[data$`Country/Region`=="Western Sahara"]<-"W. Sahara"
data$`Country/Region`[data$`Country/Region`=="Burma"]<-"Myanmar"
data$`Country/Region`[data$`Country/Region`=="Sao Tome and Principe"]<-"São Tomé and Principe"
data$`Country/Region`[data$`Country/Region`=="Saint Vincent and the Grenadines"]<-"St. Vin. and Gren."
data$`Country/Region`[data$`Country/Region`=="Saint Kitts and Nevis"]<-"St. Kitts and Nevis"
# countries$NAME<-as.character(countries$NAME)
# countries$NAME[is.na(countries$NAME)]<-"Côte d'Ivoire"
data$Pays<-as.character(unique(countries$NAME)[charmatch(data$`Country/Region`,unique(countries$NAME))])
print(data$`Country/Region`[is.na(data$Pays)])
dataPays<- data%>%dplyr::select(-`Province/State`, -Lat, -Long,-`Country/Region`)%>%group_by(Pays)%>%summarise_each(sum)
dataPays$Pays<-as.character(dataPays$Pays)
return(dataPays)
}
#https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population
population<- read.csv2("pop.csv",stringsAsFactors = F)
population$pays<-as.character(unique(countries$NAME)[charmatch(population$Country,unique(countries$NAME))])
URL <- getURL("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv")
data <- read.csv(text = URL, check.names = F)
dataCases<- dataCook(data, pop, countries)
URL <- getURL("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv")
data <- read.csv(text = URL, check.names = F)
dataDeaths<- dataCook(data, pop, countries)
dataPays<-function(data=dataCases) return(data)
jour<-names(dataCases%>%select(contains( "/")))
jourDate<- as.Date(jour, "%m/%d/%y")
names(dataCases)[str_detect(names(dataCases), "/")]<-format.Date(jourDate, "%m/%d/%y")
names(dataDeaths)[str_detect(names(dataDeaths), "/")]<-format.Date(jourDate, "%m/%d/%y")
dataCases<-left_join(data.frame(Pays = countries$NAME%>%as.character(), Pop =countries$POP_EST%>%as.character()%>%as.numeric()),dataCases)
dataCases<-dataCases%>%filter(!is.na(Pays))
dataDeaths<-left_join(data.frame(Pays = countries$NAME%>%as.character(), Pop =countries$POP_EST%>%as.character()%>%as.numeric()),dataDeaths)
dataDeaths<-dataDeaths%>%filter(!is.na(Pays))
arrondi<- function(x) 10^(ceiling(log10(x)))
dataDeaths[,3]<-ifelse(is.na(dataDeaths[,3]),0,dataDeaths[,3])
dataCases[,3]<-ifelse(is.na(dataCases[,3]),0,dataCases[,3])
for(i in 4: dim(dataDeaths)[2]) dataDeaths[,i]<- ifelse(is.na(dataDeaths[,i]), dataDeaths[,i-1],dataDeaths[,i])
for(i in 4: dim(dataCases)[2]) dataCases[,i]<- ifelse(is.na(dataCases[,i]), dataCases[,i-1],dataCases[,i])
ui <- bootstrapPage(
tags$style(type = "text/css", "html, body {width:100%;height:100%}",
HTML( ".panel-default {background-color: rgb(256, 256, 256,0.5);
padding : 10px;;}
.panel-title {background-color: rgb(256, 256, 256,0.8);
padding : 10px;
border-style: solid;
border-color: grey;}
.panel-credits {background-color: rgb(256, 256, 256,1);
padding : 15px;
border-style: solid;
border-color: black;}
.panel-mobile {background-color: rgb(256, 256, 256,0);
padding : 15px;
box-shadow: unset;}
")
),
leafletOutput("map", width = "100%", height = "93%"),
column(6,HTML("<b><a href='https://www.linkedin.com/in/thibaut-fabacher'>Thibaut FABACHER</a></b></br>
<i>Groupe Methode en Recherche Clinique (Pr. MEYER) <a href='http://www.chru-strasbourg.fr/' target ='_blank'> CHRU STRASBOURG</a></br>Laboratoire de Biostatistique (Pr. SAULEAU)<a href='https://icube.unistra.fr/' target ='_blank'> ICUBE</a></i>")),
column(2,br(), actionButton("twitter_share",
label = "Share",
icon = icon("twitter"),
onclick = sprintf("window.open('%s')",url))
),
column(2,br(),
checkboxInput("plotEvolT", "Show Evolution",F)
),
column(2, br(),checkboxInput("credits", "Credits", FALSE)),
absolutePanel(id = "input_date_control",class = "panel panel-default",bottom = 60, left = 10, draggable = F,
selectInput("choices", "Cases or Deaths ?", choices = c("Cases","Deaths"),selected = "Cases"),
uiOutput("Slider"),
helpText("The detail of each country can be obtained by clicking on it."),
uiOutput("selection"),
checkboxInput("legend", "Show legend", TRUE)
),
absolutePanel(id = "mobile",class = "panel panel-mobile",top = 10, right = 10, HTML("<a href='https://thibautfabacher.shinyapps.io/covid-19-m/'>Mobile Version</a>")),
uiOutput("Credits"),
uiOutput("mobile"),
uiOutput("plotEvol"),
absolutePanel(id = "name",class = "panel panel-title",top = 10, left = 100, HTML("<h1>COVID-19 outbreak</h1>"),draggable = T)
)
server <- function(input, output, session) {
dataPays<- reactive({
if(!is.null(input$choices)){
if(input$choices == "Cases"){
return( dataCases)
}else{
return(
dataDeaths)
}}
})
maxTotal<- reactive( max(dataPays()%>%select(-Pop)%>%select_if(is.numeric), na.rm = T)
)
maxTotalPrevalence<- reactive(max(dataPays()%>%select(-Pop)%>%select_if(is.numeric)%>%mutate_all(function(x) x/dataPays()$Pop*100000), na.rm = T)
)
#
#
Top5<-reactive( unique(c(dataPays()$Pays[order(dataPays()[,dim(dataPays())[2]]%>%unlist(),decreasing = T)][1:5]
,"France")))
#
#
#
output$map <- renderLeaflet({
# Use leaflet() here, and only include aspects of the map that
# won't need to change dynamically (at least, not unless the
# entire map is being torn down and recreated).
leaflet(data = countries) %>%
setView(0, 30, zoom = 3)
})
pal <- reactive(colorNumeric(c("#FFFFFFFF" ,rev(inferno(256))), domain = c(0,log(arrondi(maxTotal())))))
pal2 <- reactive(colorNumeric(c("#FFFFFFFF" ,rev(inferno(256))), domain = c(0,log(arrondi(maxTotalPrevalence())))))
observe({
casesDeath<- ifelse(input$choices == "Cases","Cases","Deaths")
if (!is.null(input$day1)) {
indicator<-format.Date(input$day1, "%m/%d/%y")
}else{
indicator = format.Date(max(jourDate), "%m/%d/%y")
}
if (!is.null(input$day2)) {
indicator2<-format.Date(input$day2-c(1,0), "%m/%d/%y")
}else{
indicator2 =format.Date(c(min(jourDate)-1,max(jourDate)), "%m/%d/%y")
}
if(is.null(input$variable)){
}else{
variable<- input$variable
if(variable =="Total cases/population"){
# nCases
countries2 <- merge(countries,
dataPays(),
by.x = "NAME",
by.y = "Pays",
sort = FALSE)
country_popup <- paste0("<strong>Country: </strong>",
countries2$NAME,
"<br><strong>",
"Total cases/population :",
" </strong>",
round(countries2[[indicator]]/countries2$Pop*100000,2)," /100 000")
leafletProxy("map", data = countries2)%>%
addPolygons(fillColor = pal2()(log((countries2[[indicator]]/countries2$Pop*100000)+1)),
layerId = ~NAME,
fillOpacity = 1,
color = "#BDBDC3",
weight = 1,
popup = country_popup)
}else if(variable =="Total cases"){
countries2 <- merge(countries,
dataPays(),
by.x = "NAME",
by.y = "Pays",
sort = FALSE)
country_popup <- paste0("<strong>Country: </strong>",
countries2$NAME,
"<br><strong>",
"Total ",casesDeath," :",
" </strong>",
round(countries2[[indicator]],2))
leafletProxy("map", data = countries2)%>%
addPolygons(fillColor = pal()(log((countries2[[indicator]])+1)),
fillOpacity = 1,
layerId = ~NAME,
color = "#BDBDC3",
weight = 1,
popup = country_popup)
}else if(variable =="New cases over period"){
dataPaysSel<-dataPays()%>%select(Pays, Pop)
if(indicator2[1] == format.Date(min(jourDate)-1, "%m/%d/%y")){
dataPaysSel$ncases<-dataPays()[,indicator2[2]]
}else{
dataPaysSel$ncases<-dataPays()[,indicator2[2]]-dataPays()[,indicator2[1]]
}
# nCases
countries2 <- merge(countries,
dataPaysSel,
by.x = "NAME",
by.y = "Pays",
sort = FALSE)
country_popup <- paste0("<strong>Country: </strong>",
countries2$NAME,
"<br><strong>",
"New ",casesDeath," over period :",
" </strong>",
countries2$ncases)
leafletProxy("map", data = countries2)%>%
addPolygons(fillColor = pal()(log(countries2$ncases+1)),
fillOpacity = 1,
color = "#BDBDC3",
layerId = ~NAME,
weight = 1,
popup = country_popup)
}else{
dataPaysSel<-dataPays()%>%select(Pays, Pop)
if(indicator2[1] == format.Date(min(jourDate)-1, "%m/%d/%y")){
dataPaysSel$ncases<-dataPays()[,indicator2[2]]
}else{
dataPaysSel$ncases<-dataPays()[,indicator2[2]]-dataPays()[,indicator2[1]]
}
# nCases
countries2 <- merge(countries,
dataPaysSel,
by.x = "NAME",
by.y = "Pays",
sort = FALSE)
country_popup <- paste0("<strong>Country: </strong>",
countries2$NAME,
"<br><strong>",
"New ",casesDeath," over period / population :",
" </strong>",
round(countries2$ncases/countries2$Pop*100000,2)," /100 000")
leafletProxy("map", data = countries2)%>%
addPolygons(fillColor = pal2()(log(countries2$ncases/countries2$Pop*100000+1)),
fillOpacity = 1,
color = "#BDBDC3",
layerId = ~NAME,
weight = 1,
popup = country_popup)
}
}
}
)
observe({
if(is.null(input$variable)){
}else{
variable<- input$variable
proxy <- leafletProxy("map", data = countries)
# Remove any existing legend, and only if the legend is
# enabled, create a new one.
proxy %>% clearControls()
if (input$legend) {
if(variable %in% c("Total cases/population","New cases over period/population")){
proxy %>% addLegend(position = "bottomright",
pal = pal2(),opacity = 1,
bins = log(10^(seq(0,log10(arrondi(maxTotalPrevalence())),0.5))),
value = log(1:10^(log10(arrondi(maxTotalPrevalence())))),
data =log(1:10^(log10(arrondi(maxTotalPrevalence())))),
labFormat = labelFormat(transform = function(x) round(exp(x)) ,suffix = " /100 000")
)
}else{
proxy %>% addLegend(position = "bottomright",
pal = pal(),opacity = 1,
bins = log(10^(0:log10(arrondi(maxTotal())))),
value = log(1:10^(log10(arrondi(maxTotal())))),
data = log(10^(0:log10(arrondi(maxTotal())))),
labFormat = labelFormat(transform = exp )
)
}
}
}
})
output$Slider<-renderUI({
if(is.null(input$variable)){
}else{
if(input$variable %in% c("Total cases", "Total cases/population")){
sliderInput("day1", "Day", min(jourDate), max(jourDate),
value = c(max(jourDate)),animate = T, step = 1
#min(jourDate),
)}else{
sliderInput("day2", "Day", min(jourDate), max(jourDate),
value = c(max(jourDate)-7,max(jourDate)),animate = T, step = 1
#min(jourDate),
)
}
}
})
output$selection <- renderUI({
if(input$choices =="Cases"){
radioButtons("variable", choices = c("New cases over period",
"New cases over period/population","Total cases", 'Total cases/population' ),
label = "Indicator")
}else{
radioButtons("variable", choices = list("Deaths over period"="New cases over period",
"Deaths over period/population"="New cases over period/population",
"Total deaths"="Total cases",
'Total deaths/population'='Total cases/population' ),
label = "Indicator")
}
})
output$plotEvol<-renderUI({
if (input$plotEvolT) {
tagList(absolutePanel(
id = "name",
class = "panel panel-credits",
top = 10,width = "700px",
right = 10,draggable = F,
plotlyOutput(outputId = "evol",width = "600px"),
actionButton("reset", "Reset Graph"),
actionButton("clear", "Clear all traces")
))
}
})
output$evol <-renderPlotly({
if(input$variable %in% c("Total cases/population","Total cases")){
df_evo<- dataPays()%>%filter(Pays%in% trace$data)%>%pivot_longer(cols = -c(Pays,Pop),
values_to = "Cases",names_to = "Date")%>%
mutate(Date= lubridate::parse_date_time(Date, orders = c("mdy")))
if(input$variable=="Total cases/population"){
plot_ly(data = df_evo,x = ~Date, y = ~Cases/Pop*100000, color = ~Pays, type = "scatter",mode = "lines")%>%
layout(yaxis = list( title = paste(input$choices,"/ 100 000")))
}else{
plot_ly(data = df_evo,x = ~Date, y = ~Cases, color = ~Pays, type = "scatter",mode = "lines")%>%
layout(yaxis = list( title = input$choices))
}
}else{
df_evo<- dataPays()%>%filter(Pays%in% trace$data)
for(i in dim( df_evo)[2]:4) df_evo[i]<- df_evo[i]- df_evo[i-1]
df_evo<- df_evo%>%pivot_longer(cols = -c(Pays,Pop),
values_to = "Cases",names_to = "Date")%>%
mutate(Date= lubridate::parse_date_time(Date, orders = c("mdy")))
if( input$variable=="New cases over period/population"){
plot_ly(data = df_evo,x = ~Date, y = ~Cases/Pop*100000, color = ~Pays, type = "scatter",mode = "lines")%>%
layout(yaxis = list( title = paste(input$choices,"/ 100 000/day")))
}else{
plot_ly(data = df_evo,x = ~Date, y = ~Cases, color = ~Pays, type = "scatter",mode = "lines")%>%
layout(yaxis = list( title = paste(input$choices,"/day")))
}
}
})
trace<- reactiveValues()
observe({trace$data<-Top5()
})
observeEvent(input$reset, {
for (i in 1: length(trace$data)){
plotlyProxy("evol", session) %>%
plotlyProxyInvoke("deleteTraces",list(0))
}
if(input$variable %in% c("Total cases/population","Total cases")){
df_evo<- dataPays()%>%filter(Pays%in% Top5())%>%pivot_longer(cols = -c(Pays,Pop),
values_to = "Cases",names_to = "Date")%>%
mutate(Date= lubridate::parse_date_time(Date, orders = c("mdy")))
if(input$variable=="Total cases/population"){
for (i in Top5()){
df_evoi<- df_evo%>%filter(Pays == i)
plotlyProxy("evol", session) %>%
plotlyProxyInvoke("addTraces",
list(x =df_evoi$Date ,
name =i ,
y = df_evoi$Cases/df_evoi$Pop*100000,
type = 'scatter',
mode = 'lines'))
}
}else{
for (i in Top5()){
df_evoi<- df_evo%>%filter(Pays == i)
plotlyProxy("evol", session) %>%
plotlyProxyInvoke("addTraces",
list(x =df_evoi$Date ,
name =i ,
y = df_evoi$Cases,
type = 'scatter',
mode = 'lines'))
}
}
}else{
df_evo<- dataPays()%>%filter(Pays%in% Top5())
for(i in dim(df_evo)[2]:4) df_evo[i]<-df_evo[i]-df_evo[i-1]
df_evo<-df_evo%>%pivot_longer(cols = -c(Pays,Pop),
values_to = "Cases",names_to = "Date")%>%
mutate(Date= lubridate::parse_date_time(Date, orders = c("mdy")))
if( input$variable=="New cases over period/population"){
for (i in Top5()){
df_evoi<- df_evo%>%filter(Pays == i)
plotlyProxy("evol", session) %>%
plotlyProxyInvoke("addTraces",
list(x =df_evoi$Date ,
name =i ,
y = df_evoi$Cases/df_evoi$Pop*100000,
type = 'scatter',
mode = 'lines'))
}
}else{
for (i in Top5()){
df_evoi<- df_evo%>%filter(Pays == i)
plotlyProxy("evol", session) %>%
plotlyProxyInvoke("addTraces",
list(x =df_evoi$Date ,
name =i ,
y = df_evoi$Cases,
type = 'scatter',
mode = 'lines'))
}
}
}
trace$data<-Top5()
})
observeEvent(input$clear, {
for (i in 1: length(trace$data)){
plotlyProxy("evol", session) %>%
plotlyProxyInvoke("deleteTraces",list(0))
}
trace$data<- NULL
})
observeEvent(input$map_shape_click, {
country_Click<- input$map_shape_click$id
if (!country_Click%in%trace$data & input$plotEvolT){
trace$data<-c(trace$data,country_Click)
if(input$variable %in% c("Total cases/population","Total cases")){
df_click<- dataPays()%>%filter(Pays%in% country_Click)%>%pivot_longer(cols = -c(Pays,Pop),
values_to = "Cases",names_to = "Date")%>%
mutate(Date= lubridate::parse_date_time(Date, orders = c("mdy")))
if(input$variable=="Total cases/population"){
plotlyProxy("evol", session) %>%
plotlyProxyInvoke("addTraces",
list(x =df_click$Date ,
name =country_Click ,
y = df_click$Cases/df_click$Pop*100000,
type = 'scatter',
mode = 'lines'))
}else{
plotlyProxy("evol", session) %>%
plotlyProxyInvoke("addTraces",
list(x =df_click$Date ,
name =country_Click ,
y = df_click$Cases,
type = 'scatter',
mode = 'lines'))
}
}else{
df_click<- dataPays()%>%filter(Pays%in% country_Click)
for(i in dim( df_click)[2]:4) df_click[i]<- df_click[i]- df_click[i-1]
df_click<- df_click%>%pivot_longer(cols = -c(Pays,Pop),
values_to = "Cases",names_to = "Date")%>%
mutate(Date= lubridate::parse_date_time(Date, orders = c("mdy")))
if( input$variable=="New cases over period/population"){
plotlyProxy("evol", session) %>%
plotlyProxyInvoke("addTraces",
list(x =df_click$Date ,
name =country_Click ,
y = df_click$Cases/df_click$Pop*100000,
type = 'scatter',
mode = 'lines'))
}else{
plotlyProxy("evol", session) %>%
plotlyProxyInvoke("addTraces",
list(x =df_click$Date ,
name =country_Click ,
y = df_click$Cases,
type = 'scatter',
mode = 'lines'))
}
}
}
})
output$mobile<- renderUI({
if (!input$plotEvolT) {
absolutePanel(id = "mobile",class = "panel panel-mobile",top = 10, right = 10, HTML("<a href='https://thibautfabacher.shinyapps.io/covid-19-m/'>Mobile Version</a>"))
}
})
output$Credits <- renderUI({
if (input$credits) {
tagList(
absolutePanel(
id = "name",
class = "panel panel-credits",
top = "45%",
left = "45%",
HTML(
"<h1> Data Source : </h1>
<p> <li><a href='https://coronavirus.jhu.edu/map.html'>Coronavirus COVID-19 Global Cases map Johns Hopkins University</a></li>
<li>COVID-19 Cases : <a href='https://github.com/CSSEGISandData/COVID-19' target='_blank'>Github Johns Hopkins University</a></li>
<li>World population : <a href='https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population' target='_blank'>Wikipedia</a></li>
<li>Shapefile : <a href='https://www.naturalearthdata.com/downloads/50m-cultural-vectors/50m-admin-0-countries-2/' target='_blank'>Natural Earth Data</a></li>
<li> <a href ='https://github.com/DrFabach/Corona' target='_blank'>Code on Github </a></li>
<li> <a href = 'https://www.r-project.org/' target='_blank'>The R Project for Statistical Computing</a></li>
<li> <a href = 'https://shiny.rstudio.com/' target='_blank'>Shiny R package</a></li>
<li> <a href = 'https://leafletjs.com/' target='_blank'>Leaflet </a></li>
</p>"
),
draggable = T
)
)
}
})
}
shinyApp(ui, server)