Skip to content

Commit

Permalink
Moved old Switzerland code to SwitzerlandAlt, written functional 2-st…
Browse files Browse the repository at this point in the history
…age download method
  • Loading branch information
RichardMN committed Oct 4, 2021
1 parent b5b9cbf commit 76e4c44
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 29 deletions.
52 changes: 23 additions & 29 deletions R/Switzerland.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,6 @@
#' @description Information for downloading, cleaning
#' and processing COVID-19 region data for Switzerland
#'
#' @section Liechtenstein:
#' Liechtenstein is not a canton of Switzerland, but is presented in the
#' source data as a peer of Swiss cantons and assigned the two letter code
#' `FL`. `covidregionaldata` modifies this and presents the region code
#' for Liechtenstein as `FL-FL`, consistent with the Swiss ISO 3166-2 codes
#' which are of the form `CH-BE`, `CH-ZH`, `CH-VD`, ...
#'
#' If you do not wish to work with Liechtenstein
#' data, filter out on this code. Note that this is labelled as a ISO 3166-2
#' code but Liechtenstein's real ISO 3166-2 codes refer to sub-national
#' regions.
#'
#' @section Additional data:
#'
#' In addition to the standard `covidregionaldata` columns provided,
#' the OpenDataZH source data provides other figures for ICU occupancy,
#' number of patients on ventilators, and the how many individuals are
#' isolated or quarantined. These columns are passed through unchanged.

#' Further detail on them can be found at
# nolint start
#' \url{https://github.com/openZH/covid_19/#swiss-cantons-and-principality-of-liechtenstein-unified-dataset}
#' @source \url{https://github.com/openZH/covid_19/}
# nolint end
#'
#' @export
#' @concept dataset
#' @family subnational
Expand All @@ -48,12 +23,12 @@ Switzerland <- R6::R6Class("Switzerland",
supported_region_names = list("1" = "canton"),
#' @field supported_region_codes A list of region codes in order of level.
supported_region_codes = list("1" = "iso_3166_2"),
#' @field common_data_urls List of named links to raw data.
# nolint start
#' @field common_data_urls List of named links to raw data. This url links
#' to a JSON file which provides the addresses for the most recently-updated
#' CSV files, which are then downloaded.
common_data_urls = list(
"main" = "https://github.com/openZH/covid_19/raw/master/COVID19_Fallzahlen_CH_total_v2.csv"
"main" = "https://www.covid19.admin.ch/api/data/context"
),
# nolint end
#' @field source_data_cols existing columns within the raw data
source_data_cols = c(
"hosp_new",
Expand Down Expand Up @@ -93,6 +68,25 @@ Switzerland <- R6::R6Class("Switzerland",
)
)
},

#' @description Download function to get raw data. Downloads
#' the updated list of CSV files using `download_JSON`, filters
#' that to identify the required CSV files, then uses the parent
#' method `download` to download the CSV files.
#' @importFrom purrr keep
download = function() {
message_verbose(
self$verbose,
paste0("Downloading updated URLs from ", common_data_urls$main) )

super$download_JSON()

self$data_urls <-
self$data$raw$main$data$sources$individual$csv$daily %>%
keep(names(.) %in% c('cases', 'test', 'death', 'hosp'))

super$download()
},

#' @description Switzerland specific state level data cleaning
#' @importFrom dplyr select filter mutate left_join rename
Expand Down
128 changes: 128 additions & 0 deletions R/SwitzerlandAlt.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#' Switzerland Class for downloading, cleaning and processing notification data
#' @description Information for downloading, cleaning
#' and processing COVID-19 region data for Switzerland
#'
#' @section Liechtenstein:
#' Liechtenstein is not a canton of Switzerland, but is presented in the
#' source data as a peer of Swiss cantons and assigned the two letter code
#' `FL`. `covidregionaldata` modifies this and presents the region code
#' for Liechtenstein as `FL-FL`, consistent with the Swiss ISO 3166-2 codes
#' which are of the form `CH-BE`, `CH-ZH`, `CH-VD`, ...
#'
#' If you do not wish to work with Liechtenstein
#' data, filter out on this code. Note that this is labelled as a ISO 3166-2
#' code but Liechtenstein's real ISO 3166-2 codes refer to sub-national
#' regions.
#'
#' @section Additional data:
#'
#' In addition to the standard `covidregionaldata` columns provided,
#' the OpenDataZH source data provides other figures for ICU occupancy,
#' number of patients on ventilators, and the how many individuals are
#' isolated or quarantined. These columns are passed through unchanged.

#' Further detail on them can be found at
# nolint start
#' \url{https://github.com/openZH/covid_19/#swiss-cantons-and-principality-of-liechtenstein-unified-dataset}
#' @source \url{https://github.com/openZH/covid_19/}
# nolint end
#'
#' @export
#' @concept dataset
#' @family subnational
#' @examples
#' \dontrun{
#' region <- SwitzerlandAlt$new(verbose = TRUE, steps = TRUE, get = TRUE)
#' region$return()
#' }
SwitzerlandAlt <- R6::R6Class("SwitzerlandAlt",
inherit = DataClass,
public = list(

# Core Attributes
#' @field origin name of origin to fetch data for
origin = "Switzerland",
#' @field supported_levels A list of supported levels.
supported_levels = list("1"),
#' @field supported_region_names A list of region names in order of level.
supported_region_names = list("1" = "canton"),
#' @field supported_region_codes A list of region codes in order of level.
supported_region_codes = list("1" = "iso_3166_2"),
#' @field common_data_urls List of named links to raw data.
# nolint start
common_data_urls = list(
"main" = "https://github.com/openZH/covid_19/raw/master/COVID19_Fallzahlen_CH_total_v2.csv"
),
# nolint end
#' @field source_data_cols existing columns within the raw data
source_data_cols = c(
"hosp_new",
"deaths_total",
"recovered_total",
"cases_total",
"tested_total"
),
#' @field source_text Plain text description of the source of the data
source_text = "Open Data, Canton of Zurich",
#' @field source_url Website address for explanation/introduction of the
#' data
source_url = "https://github.com/openZH/covid_19/",

#' @description Set up a table of region codes for clean data
#' @importFrom tibble tibble
set_region_codes = function() {
self$codes_lookup$`1` <- tibble(
code = c(
"CH-AG", "CH-AR", "CH-AI", "CH-BL", "CH-BS",
"CH-BE", "CH-FR", "CH-GE", "CH-GL", "CH-GR", "CH-JU", "CH-LU",
"CH-NE", "CH-NW", "CH-OW", "CH-SG", "CH-SH", "CH-SZ", "CH-SO",
"CH-TG", "CH-TI", "CH-UR", "CH-VS", "CH-VD", "CH-ZG", "CH-ZH",
"FL-FL"
),
region = c(
"Aargau", "Appenzell Ausserrhoden",
"Appenzell Innerrhoden", "Basel-Landschaft",
"Basel-Stadt", "Bern",
"Fribourg", "Gen\u00e8ve",
"Glarus", "Grisons",
"Jura", "Luzern", "Neuch\u00e2tel", "Nidwalden",
"Obwalden", "St. Gallen", "Schaffhausen",
"Schwyz", "Solothurn", "Thurgau", "Ticino",
"Uri", "Valais", "Vaud", "Zug",
"Z\u00fcrich", "Liechtenstein"
)
)
},

#' @description Switzerland specific state level data cleaning
#' @importFrom dplyr select filter mutate left_join rename
#' @importFrom lubridate as_date ymd
#' @importFrom rlang .data
#'
clean_common = function() {
self$data$clean <- self$data$raw[["main"]] %>%
select(-time, -source) %>%
mutate(
level_1_region_code = if_else(
.data$abbreviation_canton_and_fl == "FL",
"FL-FL",
paste0("CH-", .data$abbreviation_canton_and_fl)
),
date = as_date(ymd(.data$date))
) %>%
left_join(
self$codes_lookup$`1`,
by = c("level_1_region_code" = "code")
) %>%
select(-abbreviation_canton_and_fl) %>%
rename(
level_1_region = .data$region,
cases_total = .data$ncumul_conf,
deaths_total = .data$ncumul_deceased,
hosp_new = .data$new_hosp,
recovered_total = .data$ncumul_released,
tested_total = .data$ncumul_tested
)
}
)
)

0 comments on commit 76e4c44

Please sign in to comment.