From 3922f6bd8538526c5a02f5ee826b89bb6407231f Mon Sep 17 00:00:00 2001 From: boB Rudis Date: Sun, 12 Nov 2017 16:21:22 -0500 Subject: [PATCH] added years parameter to agdipt --- R/agd-ipt.r | 28 +++++++++++++++++++++++++++- man/age_group_distribution.Rd | 10 +++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/R/agd-ipt.r b/R/agd-ipt.r index 31ffffb..5220664 100644 --- a/R/agd-ipt.r +++ b/R/agd-ipt.r @@ -7,11 +7,17 @@ #' @references #' - [CDC FluView Portal](https://gis.cdc.gov/grasp/fluview/fluportaldashboard.html) #' - [AGD IPT Portal](https://gis.cdc.gov/grasp/fluview/flu_by_age_virus.html) +#' @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 1997. +#' 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{ #' agd_ipt() #' } -age_group_distribution <- function() { +age_group_distribution <- function(years = NULL) { httr::GET( url = "https://gis.cdc.gov/grasp/fluView6/GetFlu6AllDataP", @@ -58,6 +64,26 @@ age_group_distribution <- function() { vir_df <- dplyr::left_join(vir_df, mmwrid_map, "mmwrid") + available_seasons <- sort(unique(vir_df$seasonid)) + + if (!is.null(years)) { # 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(available_seasons)[1] + curr_season_descr <- vir_df[vir_df$seasonid == years,]$sea_description[1] + message(sprintf("No valid years specified, defaulting to this flu season => ID: %s [%s]", + years, curr_season_descr)) + } + + vir_df <- dplyr::filter(vir_df, seasonid %in% years) + + } + vir_df } diff --git a/man/age_group_distribution.Rd b/man/age_group_distribution.Rd index 0db2388..20d59aa 100644 --- a/man/age_group_distribution.Rd +++ b/man/age_group_distribution.Rd @@ -4,7 +4,15 @@ \alias{age_group_distribution} \title{Age Group Distribution of Influenza Positive Tests Reported by Public Health Laboratories} \usage{ -age_group_distribution() +age_group_distribution(years = NULL) +} +\arguments{ +\item{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 1997. +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.} } \description{ Retrieves the age group distribution of influenza positive tests that are reported by