From bf3026ceb155a92d5591dce94b68af9c4756afea Mon Sep 17 00:00:00 2001 From: emanuele-guidotti Date: Wed, 15 Apr 2020 16:06:12 +0200 Subject: [PATCH] added start-end dates #4 --- DESCRIPTION | 2 +- R/covid19.R | 13 ++++++++++--- man/covid19.Rd | 13 ++++++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 981d4686a2c..d9671c03cfe 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: COVID19 Type: Package Title: Coronavirus COVID-19 (2019-nCoV) Epidemic Datasets -Version: 0.4.1 +Version: 0.4.2 Authors@R: person(given = "Emanuele", family = "Guidotti", email = "emanuele.guidotti@unine.ch", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-8961-6623")) Description: Unified tidy format datasets of the 2019 Novel Coronavirus COVID-19 (2019-nCoV) epidemic across several sources. The data are downloaded in real-time, cleaned and matched with exogenous variables. License: GPL-3 diff --git a/R/covid19.R b/R/covid19.R index 0d3207f26f6..3cb8e66ab5f 100644 --- a/R/covid19.R +++ b/R/covid19.R @@ -6,6 +6,8 @@ #' #' @param ISO vector of ISO codes to retrieve (alpha-2, alpha-3 or numeric). Each country is identified by one of its \href{https://github.com/emanuele-guidotti/COVID19/blob/master/inst/extdata/db/ISO.csv}{ISO codes} #' @param level integer. Granularity level. 1: country-level data. 2: state-level data. 3: city-level data. +#' @param start the start date of the period of interest. +#' @param end the end date of the period of interest. Skip yesterday by default, as some observation may be incomplete. #' @param raw logical. Skip data cleaning? Default \code{FALSE}. See details. #' @param cache logical. Memory caching? Significantly improves performance on successive calls. Default \code{TRUE}. #' @@ -39,7 +41,12 @@ #' #' @export #' -covid19 <- function(ISO = NULL, level = 1, raw = FALSE, cache = TRUE){ +covid19 <- function(ISO = NULL, + level = 1, + start = "2019-01-01", + end = Sys.Date()-2, + raw = FALSE, + cache = TRUE){ # fallback if(!(level %in% 1:3)) @@ -51,7 +58,7 @@ covid19 <- function(ISO = NULL, level = 1, raw = FALSE, cache = TRUE){ # cache cachekey <- make.names(sprintf("covid19_%s_%s_%s",paste0(ISO, collapse = "."), level, raw)) if(cache & exists(cachekey, envir = cachedata)) - return(get(cachekey, envir = cachedata)) + return(get(cachekey, envir = cachedata) %>% dplyr::filter(date >= start & date <= end)) # bindings iso_alpha_3 <- id <- date <- country <- state <- city <- confirmed <- tests <- deaths <- recovered <- hosp <- icu <- vent <- NULL @@ -204,6 +211,6 @@ covid19 <- function(ISO = NULL, level = 1, raw = FALSE, cache = TRUE){ assign(cachekey, x, envir = cachedata) # return - return(x) + return(x %>% dplyr::filter(date >= start & date <= end)) } diff --git a/man/covid19.Rd b/man/covid19.Rd index 8deeeeef35e..e5f33953d76 100644 --- a/man/covid19.Rd +++ b/man/covid19.Rd @@ -7,13 +7,24 @@ \href{https://github.com/emanuele-guidotti/COVID19#data-sources}{Data sources} } \usage{ -covid19(ISO = NULL, level = 1, raw = FALSE, cache = TRUE) +covid19( + ISO = NULL, + level = 1, + start = "2019-01-01", + end = Sys.Date() - 2, + raw = FALSE, + cache = TRUE +) } \arguments{ \item{ISO}{vector of ISO codes to retrieve (alpha-2, alpha-3 or numeric). Each country is identified by one of its \href{https://github.com/emanuele-guidotti/COVID19/blob/master/inst/extdata/db/ISO.csv}{ISO codes}} \item{level}{integer. Granularity level. 1: country-level data. 2: state-level data. 3: city-level data.} +\item{start}{the start date of the period of interest.} + +\item{end}{the end date of the period of interest. Skip yesterday by default, as some observation may be incomplete.} + \item{raw}{logical. Skip data cleaning? Default \code{FALSE}. See details.} \item{cache}{logical. Memory caching? Significantly improves performance on successive calls. Default \code{TRUE}.}