Skip to content

Commit

Permalink
Reformatting of code
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardMN committed Sep 16, 2021
1 parent ce5de11 commit b54bd03
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 107 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Expand Up @@ -122,4 +122,4 @@ Encoding: UTF-8
Language: en-gb
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
RoxygenNote: 7.1.2
210 changes: 106 additions & 104 deletions R/Vietnam.R
Expand Up @@ -13,117 +13,119 @@
#' region <- Vietnam$new(verbose = TRUE, steps = TRUE, get = TRUE)
#' region$return()
#' }
# nolint start
Vietnam <- R6::R6Class("Vietnam",
inherit = DataClass,
public = list(
inherit = DataClass,
public = list(

# Core Attributes (amend each parameter for country specific information)
#' @field origin name of country to fetch data for
origin = "Vietnam",
#' @field supported_levels List of supported levels.
supported_levels = list("1"),
#' @field supported_region_names List of region names in order of level.
supported_region_names = list("1" = "region"),
#' @field supported_region_codes 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.
common_data_urls = list(),
#' @field source_data_cols existing columns within the raw data
source_data_cols = c(
"cases_total", "deaths_total", "recovered_total"
),
#' @field source_text Plain text description of the source of the data
source_text = "Public COVID-19 for Vietnam, curated by NCSC's COVID-19 team",
#' @field source_url Website address for explanation/introduction of the
#' data
source_url = "https://covid.ncsc.gov.vn", # nolint
# Core Attributes (amend each parameter for country specific information)
#' @field origin name of country to fetch data for
origin = "Vietnam",
#' @field supported_levels List of supported levels.
supported_levels = list("1"),
#' @field supported_region_names List of region names in order of level.
supported_region_names = list("1" = "region"),
#' @field supported_region_codes 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.
common_data_urls = list(),
#' @field source_data_cols existing columns within the raw data
source_data_cols = c(
"cases_total", "deaths_total", "recovered_total"
),
#' @field source_text Plain text description of the source of the data
source_text = "Public COVID-19 for Vietnam, curated by NCSC's COVID-19 team",
#' @field source_url Website address for explanation/introduction of the
#' data
source_url = "https://covid.ncsc.gov.vn", # nolint

#' @description Set up a table of region codes for clean data
#' @importFrom tibble tibble
set_region_codes = function(){
self$codes_lookup$`1` <- covidregionaldata::vietnam_codes
},
#' @description Set up a table of region codes for clean data
#' @importFrom tibble tibble
set_region_codes = function() {
self$codes_lookup$`1` <- covidregionaldata::vietnam_codes
},

#' @description download function to get raw data
#' @importFrom tidyr replace_na drop_na
#' @importFrom lubridate dmy
#' @importFrom jsonlite fromJSON
download = function() {
bundles_urls = list(
"case_by_time" = 'https://covid.ncsc.gov.vn/api/v3/covid/provinces?filter_type=case_by_time',
"death_by_time" = 'https://covid.ncsc.gov.vn/api/v3/covid/provinces?filter_type=death_by_time',
"recovered_by_time" = 'https://covid.ncsc.gov.vn/api/v3/covid/provinces?filter_type=recovered_by_time'
#' @description download function to get raw data
#' @importFrom tidyr replace_na drop_na
#' @importFrom lubridate dmy
#' @importFrom jsonlite fromJSON
download = function() {
bundles_urls <- list(
"case_by_time" = "https://covid.ncsc.gov.vn/api/v3/covid/provinces?filter_type=case_by_time",
"death_by_time" = "https://covid.ncsc.gov.vn/api/v3/covid/provinces?filter_type=death_by_time",
"recovered_by_time" = "https://covid.ncsc.gov.vn/api/v3/covid/provinces?filter_type=recovered_by_time"
)
Sys.setenv("VROOM_CONNECTION_SIZE" = 131072 * 4) # Fix VROOM error
provines_url <- "https://covid.ncsc.gov.vn/api/v3/covid/provinces"
bundles <- names(bundles_urls)
provines_data <- fromJSON(provines_url)

)
Sys.setenv("VROOM_CONNECTION_SIZE" = 131072*4) # Fix VROOM error
provines_url = 'https://covid.ncsc.gov.vn/api/v3/covid/provinces'
bundles = names(bundles_urls)
provines_data = fromJSON(provines_url)
get_bundles_data <- function(bundles) {
bundles_data <- list()
for (bundle in bundles) {
url <- paste0("https://covid.ncsc.gov.vn/api/v3/covid/provinces?filter_type=", bundle)
data <- fromJSON(url)
bundles_data <- c(bundles_data, setNames(list(data), bundle))
}
bundles_data
}

get_bundles_data = function(bundles){
bundles_data = list()
for (bundle in bundles){
url = paste0('https://covid.ncsc.gov.vn/api/v3/covid/provinces?filter_type=', bundle)
data = fromJSON(url)
bundles_data = c(bundles_data, setNames(list(data), bundle))
}
bundles_data
}
bundles_data <- get_bundles_data(bundles)

bundles_data = get_bundles_data(bundles)
get_province <- function(id, data) {
row_dat <- provines_data[(id <- id), ]
death_by_time <- do.call(cbind, data$death_by_time[id])
case_by_time <- do.call(cbind, data$case_by_time[id])
recovered_by_time <- do.call(cbind, data$recovered_by_time[id])
if (!identical(row.names(death_by_time), row.names(death_by_time))) {
stop("Dates on case_by_time and death_by_time do not match!")
}
df <- tibble(
date = dmy(row.names(case_by_time)),
id = row_dat$id,
name = row_dat$name,
case_by_time = case_by_time,
death_by_time = death_by_time,
recovered_by_time = recovered_by_time
)
df
}

get_province = function(id, data){
row_dat = provines_data[(id=id),]
death_by_time= do.call(cbind, data$death_by_time[id])
case_by_time=do.call(cbind, data$case_by_time[id])
recovered_by_time=do.call(cbind, data$recovered_by_time[id])
if (!identical(row.names(death_by_time), row.names(death_by_time))) {
stop("Dates on case_by_time and death_by_time do not match!")
}
df = tibble(date= dmy(row.names(case_by_time)),
id = row_dat$id,
name = row_dat$name,
case_by_time= case_by_time,
death_by_time= death_by_time,
recovered_by_time= recovered_by_time)
df
}
df <- do.call(rbind, lapply(provines_data$id, function(id) {
get_province(id, bundles_data)
}))
names(df) <- c("date", "id", "region_name", "cases_total", "deaths_total", "recovered_total")
self$data$raw[["main"]] <- df
},

df = do.call(rbind, lapply(provines_data$id, function(id){get_province(id, bundles_data)}))
names(df) <- c("date", "id", "region_name", "cases_total", "deaths_total", "recovered_total")
self$data$raw[['main']] = df
},

#' @description Provincial Level Data
#' cleaning
#' @param ... pass additional arguments
#'
#' @importFrom dplyr filter select mutate rename tibble
#' @importFrom tidyr replace_na drop_na
clean_common = function() {
self$data$clean <- self$data$raw[['main']] %>%
select( date, region_name, cases_total, deaths_total, recovered_total) %>%
mutate(cases_total = as.numeric(cases_total),
deaths_total = as.numeric(deaths_total),
recovered_total = as.numeric(recovered_total),
region_name = stringr::str_replace_all(region_name, 'TP HCM', 'Hochiminh'),
) %>%
tidyr::drop_na(date, region_name) %>%
rename(level_1_region = region_name) %>%
mutate(
level_1_region = stringi::stri_trans_general(level_1_region, "latin-ascii"),
level_1_region = stringi::stri_trim_both(level_1_region),
level_1_region = stringr::str_replace_all(level_1_region, '\\(.*\\)|-| ', ''),
level_1_region = stringr::str_to_title(level_1_region),
level_1_region = tidyr::replace_na(level_1_region, "Unknown")
) %>%
left_join(
self$codes_lookup$`1`,
by = c("level_1_region" = "level_1_region")
)
}

)
#' @description Provincial Level Data
#' cleaning
#' @param ... pass additional arguments
#'
#' @importFrom dplyr filter select mutate rename tibble
#' @importFrom tidyr replace_na drop_na
clean_common = function() {
self$data$clean <- self$data$raw[["main"]] %>%
select(date, region_name, cases_total, deaths_total, recovered_total) %>%
mutate(
cases_total = as.numeric(cases_total),
deaths_total = as.numeric(deaths_total),
recovered_total = as.numeric(recovered_total),
region_name = stringr::str_replace_all(region_name, "TP HCM", "Hochiminh"),
) %>%
tidyr::drop_na(date, region_name) %>%
rename(level_1_region = region_name) %>%
mutate(
level_1_region = stringi::stri_trans_general(level_1_region, "latin-ascii"),
level_1_region = stringi::stri_trim_both(level_1_region),
level_1_region = stringr::str_replace_all(level_1_region, "\\(.*\\)|-| ", ""),
level_1_region = stringr::str_to_title(level_1_region),
level_1_region = tidyr::replace_na(level_1_region, "Unknown")
) %>%
left_join(
self$codes_lookup$`1`,
by = c("level_1_region" = "level_1_region")
)
}
)
)
# nolint end

Binary file modified data/all_country_data.rda
Binary file not shown.
1 change: 1 addition & 0 deletions inst/WORDLIST
Expand Up @@ -63,6 +63,7 @@ Meixco
memoise
metacran
MX
Nam
nhs
nhsregions
nr
Expand Down
14 changes: 12 additions & 2 deletions man/Vietnam.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b54bd03

Please sign in to comment.