-
Notifications
You must be signed in to change notification settings - Fork 0
/
crontab.R
executable file
·94 lines (80 loc) · 2.09 KB
/
crontab.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
#libraries needed
library(tidyverse)
library(RSQLite)
# Functions needed
#Rename
#'
#' Rename first few columns
#'
#' @param df Sheet.
#'
#' @keywords internal
rename_sheets <- function(df){
names(df)[1:4] <- c(
"state",
"country",
"lat",
"lon"
)
return(df)
}
#' Pivot
#'
#' Change data from wide to long.
#'
#' @param df Sheet.
#'
#' @keywords internal
pivot <- function(df){
tidyr::pivot_longer(
df,
tidyselect::contains("/"),
names_to = c("date"),
values_to = c("cases")
)
}
#' Convert
#'
#' Convert dates.
#'
#' @keywords internal
as_date <- function(date){
date <- lubridate::mdy(date, "%m/%d/%Y")
date[!is.na(date)]
}
# jhu data
confirmed_sheet <- "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv"
deaths_sheet <- "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv"
# confirmed cases
confirmed <- readr::read_csv(confirmed_sheet, col_types = readr::cols())
# deaths
deaths <- readr::read_csv(deaths_sheet, col_types = readr::cols())
# add col
confirmed$type <- "confirmed"
deaths$type <- "death"
# rename
confirmed <- rename_sheets(confirmed)
deaths <- rename_sheets(deaths)
# pivot longer
confirmed <- pivot(confirmed)
deaths <- pivot(deaths)
suppressWarnings({
df <- dplyr::bind_rows(confirmed, deaths) %>%
dplyr::mutate(
date = as_date(date),
cases = trimws(cases),
cases = as.numeric(cases),
cases = dplyr::case_when(
is.na(cases) ~ 0,
TRUE ~ cases
),
country_iso2c = countrycode::countrycode(country, "country.name", "iso2c")
)
})
df$state <- ifelse(is.na(df$state), df$country,df$state)
# Connect to SQLite and save the data
con <- dbConnect(SQLite(), "/srv/shiny-server/CoronaOutbreak/covid.db")
log <- tibble::tibble(last_updated = Sys.time())
DBI::dbWriteTable(con, "jhu", df, overwrite =TRUE, append = FALSE)
DBI::dbWriteTable(con, "log", log, append = TRUE)
DBI::dbDisconnect(con)