-
Notifications
You must be signed in to change notification settings - Fork 3
/
global.R
180 lines (110 loc) · 5.58 KB
/
global.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# ---- Loading libraries ----
library("shiny")
library("shinydashboard")
library("tidyverse")
library("leaflet")
library("plotly")
library("DT")
library("fs")
library("wbstats")
source("utils.R", local = T)
downloadGithubData <- function() {
download.file(
url = "https://github.com/CSSEGISandData/COVID-19/archive/master.zip",
destfile = "data/covid19_data.zip"
)
data_path <- "COVID-19-master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_"
unzip(
zipfile = "data/covid19_data.zip",
files = paste0(data_path, c("confirmed_global.csv", "deaths_global.csv", "recovered_global.csv")),
exdir = "data",
junkpaths = T
)
}
updateData <- function() {
# Download data from Johns Hopkins (https://github.com/CSSEGISandData/COVID-19) if the data is older than 0.5h
if (!dir_exists("data")) {
dir.create('data')
downloadGithubData()
} else if ((!file.exists("data/covid19_data.zip")) || (as.double(Sys.time() - file_info("data/covid19_data.zip")$change_time, units = "hours") > 0.5)) {
downloadGithubData()
}
}
# Update with start of app
updateData()
# TODO: Still throws a warning but works for now
data_confirmed <- read_csv("data/time_series_covid19_confirmed_global.csv")
data_deceased <- read_csv("data/time_series_covid19_deaths_global.csv")
# data_recovered <- read_csv("data/time_series_covid19_recovered_global.csv")
# Get latest data
current_date <- as.Date(names(data_confirmed)[ncol(data_confirmed)], format = "%m/%d/%y")
changed_date <- file_info("data/covid19_data.zip")$change_time
# Get evolution data by country
data_confirmed_sub <- data_confirmed %>%
pivot_longer(names_to = "date", cols = 5:ncol(data_confirmed)) %>%
group_by(`Province/State`, `Country/Region`, date, Lat, Long) %>%
summarise("confirmed" = sum(value, na.rm = T))
# data_recovered_sub <- data_recovered %>%
# pivot_longer(names_to = "date", cols = 5:ncol(data_recovered)) %>%
# group_by(`Province/State`, `Country/Region`, date, Lat, Long) %>%
# summarise("recovered" = sum(value, na.rm = T))
data_deceased_sub <- data_deceased %>%
pivot_longer(names_to = "date", cols = 5:ncol(data_deceased)) %>%
group_by(`Province/State`, `Country/Region`, date, Lat, Long) %>%
summarise("deceased" = sum(value, na.rm = T))
data_evolution <- data_confirmed_sub %>%
full_join(data_deceased_sub) %>%
ungroup() %>%
mutate(date = as.Date(date, "%m/%d/%y")) %>%
arrange(date) %>%
group_by(`Province/State`, `Country/Region`, Lat, Long) %>%
mutate(
recovered = lag(confirmed, 14, default = 0) - deceased,
recovered = ifelse(recovered > 0, recovered, 0),
active = confirmed - recovered - deceased
) %>%
pivot_longer(names_to = "var", cols = c(confirmed, recovered, deceased, active)) %>%
ungroup()
# Calculating new cases
data_evolution <- data_evolution %>%
group_by(`Province/State`, `Country/Region`) %>%
mutate(value_new = value - lag(value, 4, default = 0)) %>%
ungroup()
rm(data_confirmed, data_confirmed_sub, data_recovered, data_recovered_sub, data_deceased, data_deceased_sub)
# ---- Download population data ----
population <- wb(country = "countries_only", indicator = "SP.POP.TOTL", startdate = 2018, enddate = 2020) %>%
select(country, value) %>%
rename(population = value)
countryNamesPop <- c("Brunei Darussalam", "Congo, Dem. Rep.", "Congo, Rep.", "Czech Republic",
"Egypt, Arab Rep.", "Iran, Islamic Rep.", "Korea, Rep.", "St. Lucia", "West Bank and Gaza", "Russian Federation",
"Slovak Republic", "United States", "St. Vincent and the Grenadines", "Venezuela, RB")
countryNamesDat <- c("Brunei", "Congo (Kinshasa)", "Congo (Brazzaville)", "Czechia", "Egypt", "Iran", "Korea, South",
"Saint Lucia", "occupied Palestinian territory", "Russia", "Slovakia", "US", "Saint Vincent and the Grenadines", "Venezuela")
population[which(population$country %in% countryNamesPop), "country"] <- countryNamesDat
# Data from wikipedia
noDataCountries <- data.frame(
country = c("Cruise Ship", "Guadeloupe", "Guernsey", "Holy See", "Jersey", "Martinique", "Reunion", "Taiwan*"),
population = c(3700, 395700, 63026, 800, 106800, 376480, 859959, 23780452)
)
population <- bind_rows(population, noDataCountries)
data_evolution <- data_evolution %>%
left_join(population, by = c("Country/Region" = "country"))
rm(population, countryNamesPop, countryNamesDat, noDataCountries)
data_atDate <- function(inputDate) {
data_evolution[which(data_evolution$date == inputDate),] %>%
distinct() %>%
pivot_wider(id_cols = c("Province/State", "Country/Region", "date", "Lat", "Long", "population"), names_from = var, values_from = value) %>%
filter(confirmed > 0 |
recovered > 0 |
deceased > 0 |
active > 0)
}
data_latest <- data_atDate(max(data_evolution$date))
top5_countries <- data_evolution %>%
filter(var == "active", date == current_date) %>%
group_by(`Country/Region`) %>%
summarise(value = sum(value, na.rm = T)) %>%
arrange(desc(value)) %>%
top_n(5) %>%
select(`Country/Region`) %>%
pull()