diff --git a/R/geographic-spread.R b/R/geographic-spread.R index a673ad1..88afd72 100644 --- a/R/geographic-spread.R +++ b/R/geographic-spread.R @@ -1,13 +1,41 @@ #' State and Territorial Epidemiologists Reports of Geographic Spread of Influenza #' +#' @md +#' @param years a vector of years to retrieve data for (i.e. `2014` for CDC +#' flu season 2014-2015). CDC has data for this API going back to 2003 +#' and up until the current, active flu season. +#' Default value (`NULL`) means retrieve **all** years. NOTE: if you +#' happen to specify a 2-digit season value (i.e. `57` == 2017-2018) +#' the function is smart enough to retrieve by season ID vs convert that +#' to a year. #' @export #' @examples \dontrun{ #' geographic_spread() #' } -geographic_spread <- function() { +geographic_spread <- function(years=NULL) { meta <- jsonlite::fromJSON("https://gis.cdc.gov/grasp/Flu8/GetPhase08InitApp?appVersion=Public") + available_seasons <- sort(meta$seasons$seasonid) + + if (is.null(years)) { # ALL YEARS + years <- available_seasons + } else { # specified years or seasons or a mix + + years <- as.numeric(years) + years <- ifelse(years > 1996, years - 1960, years) + years <- sort(unique(years)) + years <- years[years %in% available_seasons] + + if (length(years) == 0) { + years <- rev(sort(meta$seasons$seasonid))[1] + curr_season_descr <- meta$seasons[meta$seasons$seasonid == years, "description"] + message(sprintf("No valid years specified, defaulting to this flu season => ID: %s [%s]", + years, curr_season_descr)) + } + + } + httr::POST( url = "https://gis.cdc.gov/grasp/Flu8/PostPhase08DownloadData", httr::user_agent(.cdcfluview_ua), @@ -19,7 +47,7 @@ geographic_spread <- function() { encode = "json", body = list( AppVersion = "Public", - SeasonIDs = paste0(meta$seasons$seasonid, collapse=",") + SeasonIDs = paste0(years, collapse=",") ), # httr::verbose(), httr::timeout(.httr_timeout) diff --git a/man/geographic_spread.Rd b/man/geographic_spread.Rd index 425b1f1..e83907f 100644 --- a/man/geographic_spread.Rd +++ b/man/geographic_spread.Rd @@ -4,7 +4,16 @@ \alias{geographic_spread} \title{State and Territorial Epidemiologists Reports of Geographic Spread of Influenza} \usage{ -geographic_spread() +geographic_spread(years = NULL) +} +\arguments{ +\item{years}{a vector of years to retrieve data for (i.e. \code{2014} for CDC +flu season 2014-2015). CDC has data for this API going back to 2003 +and up until the current, active flu season. +Default value (\code{NULL}) means retrieve \strong{all} years. NOTE: if you +happen to specify a 2-digit season value (i.e. \code{57} == 2017-2018) +the function is smart enough to retrieve by season ID vs convert that +to a year.} } \description{ State and Territorial Epidemiologists Reports of Geographic Spread of Influenza