Skip to content

Commit

Permalink
Refactor code for PR #413
Browse files Browse the repository at this point in the history
  • Loading branch information
biocyberman authored and Vang Le-Quy committed Sep 15, 2021
1 parent 7ca169a commit 29070dd
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 41 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Imports:
countrycode (>= 1.2.0),
dplyr,
httr,
jsonlite,
lifecycle,
lubridate,
magrittr,
Expand All @@ -100,7 +101,7 @@ Imports:
tidyselect,
vroom,
withr,
xml2
xml2,
Suggests:
ggplot2,
ggspatial,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ importFrom(dplyr,slice_tail)
importFrom(dplyr,starts_with)
importFrom(dplyr,summarise)
importFrom(dplyr,tally)
importFrom(dplyr,tibble)
importFrom(dplyr,ungroup)
importFrom(dplyr,vars)
importFrom(httr,GET)
importFrom(httr,POST)
importFrom(httr,content)
importFrom(httr,status_code)
importFrom(jsonlite,fromJSON)
importFrom(lifecycle,deprecate_warn)
importFrom(lifecycle,deprecated)
importFrom(lifecycle,is_present)
Expand Down
81 changes: 43 additions & 38 deletions R/Vietnam.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
#' @description Information for downloading, cleaning
#' and processing covid-19 region data for Vietnam.
#'
# nolint start
#' @source \url{https://github.com/biocyberman/covidregionaldata/}
# nolint end
#' @source \url{https://covid.ncsc.gov.vn}
#' @export
#' @concept dataset
#' @family subnational
Expand All @@ -15,34 +13,28 @@
#' region <- Vietnam$new(verbose = TRUE, steps = TRUE, get = TRUE)
#' region$return()
#' }
# nolint start
Vietnam <- R6::R6Class("Vietnam",
inherit = DataClass,
public = list(

# Core Attributes (amend each paramater for country specific infomation)
# 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" = "is_3166_2"),
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(
"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'

),
# nolint end
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 data curated by 5F team",
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
Expand All @@ -53,26 +45,28 @@ Vietnam <- R6::R6Class("Vietnam",
self$codes_lookup$`1` <- covidregionaldata::vietnam_codes
},

#' @description Provincial Level Data
#' cleaning
#' @param ... pass additional arguments
#'
#' @importFrom dplyr filter select mutate rename
#' @description download function to get raw data
#' @importFrom tidyr replace_na drop_na
#' @importFrom lubridate dmy
#' @importFrom lubridate dmy
#' @importFrom jsonlite fromJSON
clean_common = function() {
Sys.setenv("VROOM_CONNECTION_SIZE" = 131072*4) # Fix VROOM error
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(self$data$raw)
provines_data = jsonlite::fromJSON(provines_url)
bundles = names(bundles_urls)
provines_data = fromJSON(provines_url)

get_bundles_data = function(bundles){
bundles_data = list()
for (bundle in bundles){
for (bundle in bundles){
url = paste0('https://covid.ncsc.gov.vn/api/v3/covid/provinces?filter_type=', bundle)
data = jsonlite::fromJSON(url)
bundles_data = c(bundles_data, setNames(list(data), bundle))
data = fromJSON(url)
bundles_data = c(bundles_data, setNames(list(data), bundle))
}
bundles_data
}
Expand All @@ -82,30 +76,39 @@ Vietnam <- R6::R6Class("Vietnam",
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])
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 = dplyr::tibble(date= lubridate::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 = 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$clean <- df %>%
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(
Expand All @@ -120,5 +123,7 @@ Vietnam <- R6::R6Class("Vietnam",
by = c("level_1_region" = "level_1_region")
)
}

)
)
# nolint end
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 29070dd

Please sign in to comment.