From 615547285506ac3f73849c16e98e996943a914c4 Mon Sep 17 00:00:00 2001 From: boB Rudis Date: Sun, 12 Nov 2017 16:12:38 -0500 Subject: [PATCH] ilinet years parameter added --- .Rbuildignore | 1 + Makefile | 44 ++++++++++++++++++++++++++++++++++++++++++++ R/ilinet.r | 33 ++++++++++++++++++++++++++++++--- man/ilinet.Rd | 9 ++++++++- man/who_nrevss.Rd | 3 ++- 5 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 Makefile diff --git a/.Rbuildignore b/.Rbuildignore index 172071b..1f58f13 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -14,6 +14,7 @@ ^\.Rproj\.user$ ^\.travis\.yml$ ^.*md$ +^crunch ^crunch/ ^crunch/.* ^README_files/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..194d0ce --- /dev/null +++ b/Makefile @@ -0,0 +1,44 @@ +PACKAGE := $(shell grep '^Package:' DESCRIPTION | sed -E 's/^Package:[[:space:]]+//') +RSCRIPT = Rscript --no-init-file + +all: install + +test: + ${RSCRIPT} -e 'library(methods); devtools::test()' + +roxygen: + @mkdir -p man + ${RSCRIPT} -e "library(methods); devtools::document()" + +install: + R CMD INSTALL . + +build: + R CMD build . + +check: + _R_CHECK_CRAN_INCOMING_=FALSE make check_all + +check_all: + ${RSCRIPT} -e "rcmdcheck::rcmdcheck(args = c('--as-cran', '--no-manual'))" + +staticdocs: + @mkdir -p inst/staticdocs + ${RSCRIPT} -e "library(methods); staticdocs::build_site()" + rm -f vignettes/*.html + @rmdir inst/staticdocs +website: staticdocs + ./update_web.sh + +README.md: README.Rmd + Rscript -e 'library(methods); devtools::load_all(); knitr::knit("README.Rmd")' + sed -i.bak 's/[[:space:]]*$$//' $@ + rm -f $@.bak + +vignettes/%.Rmd: vignettes/src/%.R + ${RSCRIPT} -e 'library(sowsear); sowsear("$<", output="$@")' +vignettes: vignettes/cyphr.Rmd vignettes/data.Rmd + ${RSCRIPT} -e 'library(methods); devtools::build_vignettes()' + +# No real targets! +.PHONY: all test document install vignettes diff --git a/R/ilinet.r b/R/ilinet.r index 9625f58..75d76d4 100644 --- a/R/ilinet.r +++ b/R/ilinet.r @@ -10,6 +10,12 @@ #' #' @md #' @param region one of "`national`", "`hhs`", "`census`", or "`state`" +#' @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. #' @references #' - [CDC FluView Portal](https://gis.cdc.gov/grasp/fluview/fluportaldashboard.html) #' - [ILINet Portal](https://wwwn.cdc.gov/ilinet/) (Login required) @@ -26,10 +32,12 @@ #' c("national", "hhs", "census", "state"), #' ~ilinet(.x) %>% readr::type_convert()) #' } -ilinet <- function(region=c("national", "hhs", "census", "state")) { +ilinet <- function(region=c("national", "hhs", "census", "state"), years=NULL) { region <- match.arg(tolower(region), c("national", "hhs", "census", "state")) + meta <- jsonlite::fromJSON("https://gis.cdc.gov/grasp/flu2/GetPhase02InitApp?appVersion=Public") + list( AppVersion = "Public", DatasourceDT = list(list(ID = 1, Name = "ILINet")), @@ -43,8 +51,27 @@ ilinet <- function(region=c("national", "hhs", "census", "state")) { state = { lapply(1:59, function(i) list(ID=i, Name=as.character(i))) } ) - seasons <- 37:((unclass(as.POSIXlt(Sys.time()))[["year"]] + 1900) - 1960) - params$SeasonsDT <- lapply(seasons, function(i) list(ID=i, Name=as.character(i))) + 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)) + } + + } + + params$SeasonsDT <- lapply(years, function(i) list(ID=i, Name=as.character(i))) tf <- tempfile(fileext = ".zip") td <- tempdir() diff --git a/man/ilinet.Rd b/man/ilinet.Rd index ada5bc9..1f110a3 100644 --- a/man/ilinet.Rd +++ b/man/ilinet.Rd @@ -4,10 +4,17 @@ \alias{ilinet} \title{Retrieve ILINet Surveillance Data} \usage{ -ilinet(region = c("national", "hhs", "census", "state")) +ilinet(region = c("national", "hhs", "census", "state"), years = NULL) } \arguments{ \item{region}{one of "\code{national}", "\code{hhs}", "\code{census}", or "\code{state}"} + +\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 1997. +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{ The CDC FluView Portal provides in-season and past seasons' national, regional, diff --git a/man/who_nrevss.Rd b/man/who_nrevss.Rd index ffcb728..60a4536 100644 --- a/man/who_nrevss.Rd +++ b/man/who_nrevss.Rd @@ -32,7 +32,8 @@ HHS, Census and State data retrieval is not as "instantaneous" as their ILINet counterparts.\cr\cr Also, beginning for the 2015-16 season, reports from public health and clinical laboratories are presented separately in the weekly influenza update. This is -the reason why a list of data frames is returned. +the reason why a list of data frames is returned.\cr\cr +\strong{ALSO} The new CDC API seems to be missing some public health lab data fields. } \examples{ \dontrun{