diff --git a/DESCRIPTION b/DESCRIPTION index 86fd1d5f..87d1d841 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -86,6 +86,7 @@ Imports: countrycode (>= 1.2.0), dplyr, httr, + jsonlite, lifecycle, lubridate, magrittr, @@ -100,7 +101,7 @@ Imports: tidyselect, vroom, withr, - xml2 + xml2, Suggests: ggplot2, ggspatial, diff --git a/NAMESPACE b/NAMESPACE index 37b43471..4c694bca 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/Vietnam.R b/R/Vietnam.R index 9afa1eea..22ed8302 100644 --- a/R/Vietnam.R +++ b/R/Vietnam.R @@ -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 @@ -15,11 +13,12 @@ #' 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. @@ -27,22 +26,15 @@ Vietnam <- R6::R6Class("Vietnam", #' @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 @@ -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 } @@ -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( @@ -120,5 +123,7 @@ Vietnam <- R6::R6Class("Vietnam", by = c("level_1_region" = "level_1_region") ) } + ) ) +# nolint end diff --git a/man/Vietnam.Rd b/man/Vietnam.Rd index 20f22e4b..56f9876e 100644 --- a/man/Vietnam.Rd +++ b/man/Vietnam.Rd @@ -5,7 +5,7 @@ \title{Vietnam Class for downloading, cleaning and processing notification data} \source{ -\url{https://github.com/biocyberman/covidregionaldata/} +\url{https://covid.ncsc.gov.vn} } \description{ Information for downloading, cleaning @@ -70,6 +70,7 @@ data} \subsection{Public methods}{ \itemize{ \item \href{#method-set_region_codes}{\code{Vietnam$set_region_codes()}} +\item \href{#method-download}{\code{Vietnam$download()}} \item \href{#method-clean_common}{\code{Vietnam$clean_common()}} \item \href{#method-clone}{\code{Vietnam$clone()}} } @@ -79,7 +80,6 @@ data} \itemize{ \item \out{}\href{../../covidregionaldata/html/DataClass.html#method-available_regions}{\code{covidregionaldata::DataClass$available_regions()}}\out{} \item \out{}\href{../../covidregionaldata/html/DataClass.html#method-clean}{\code{covidregionaldata::DataClass$clean()}}\out{} -\item \out{}\href{../../covidregionaldata/html/DataClass.html#method-download}{\code{covidregionaldata::DataClass$download()}}\out{} \item \out{}\href{../../covidregionaldata/html/DataClass.html#method-filter}{\code{covidregionaldata::DataClass$filter()}}\out{} \item \out{}\href{../../covidregionaldata/html/DataClass.html#method-get}{\code{covidregionaldata::DataClass$get()}}\out{} \item \out{}\href{../../covidregionaldata/html/DataClass.html#method-initialize}{\code{covidregionaldata::DataClass$initialize()}}\out{} @@ -99,6 +99,16 @@ Set up a table of region codes for clean data \if{html}{\out{
}}\preformatted{Vietnam$set_region_codes()}\if{html}{\out{
}} } +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-download}{}}} +\subsection{Method \code{download()}}{ +download function to get raw data +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{Vietnam$download()}\if{html}{\out{
}} +} + } \if{html}{\out{
}} \if{html}{\out{}}