From 0df2fdf1e05d9bd944b87e5219d1c1f2241535b9 Mon Sep 17 00:00:00 2001 From: Bob Rudis Date: Mon, 5 Dec 2016 14:04:35 -0500 Subject: [PATCH] fixed state data d/l & added tests+travis --- .Rbuildignore | 2 +- .travis.yml | 26 ++++---------------------- DESCRIPTION | 4 +++- NAMESPACE | 1 + NEWS.md | 1 + R/aaa.r | 1 + R/cdcfluview-package.R | 1 + R/get_state_data.r | 34 +++++++++++----------------------- R/mortalty.r | 1 + man/get_state_data.Rd | 2 +- tests/testthat/test-cdcfluview.R | 4 +++- 11 files changed, 28 insertions(+), 49 deletions(-) create mode 100644 R/aaa.r diff --git a/.Rbuildignore b/.Rbuildignore index 608bdfa..342923a 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -6,4 +6,4 @@ ^crunch/.* ^README_files/ ^README_files/.* -^README-.* \ No newline at end of file +^README-.* diff --git a/.travis.yml b/.travis.yml index cf8c99a..8d139ac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,23 +1,5 @@ -language: c +# R for travis: see documentation at https://docs.travis-ci.com/user/languages/r -before_install: - - curl -OL http://raw.github.com/craigcitro/r-travis/master/scripts/travis-tool.sh - - chmod 755 ./travis-tool.sh - - ./travis-tool.sh bootstrap - -install: - - ./travis-tool.sh install_github plyr - - ./travis-tool.sh install_deps - -script: ./travis-tool.sh run_tests - -on_failure: - - ./travis-tool.sh dump_logs - -branches: - except: - - /-expt$/ - notifications: - email: - on_success: change - on_failure: change +language: R +sudo: false +cache: packages diff --git a/DESCRIPTION b/DESCRIPTION index 0661e08..53be6e7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -5,6 +5,7 @@ Version: 0.5.1 Date: 2016-12-05 Author: Bob Rudis (bob@rud.is) Maintainer: Bob Rudis +Encoding: UTF-8 Description: The U.S. Centers for Disease Control (CDC) maintains a portal for accessing state, regional and national influenza statistics as well as @@ -23,7 +24,8 @@ Imports: dplyr, utils, purrr, - readr + readr, + V8 Depends: R (>= 3.2.0) RoxygenNote: 5.0.1 diff --git a/NAMESPACE b/NAMESPACE index ba2a80b..c9b1a8d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,7 @@ export(get_flu_data) export(get_mortality_surveillance_data) export(get_state_data) export(get_weekly_flu_report) +import(V8) import(dplyr) import(httr) import(utils) diff --git a/NEWS.md b/NEWS.md index 0de8a2f..ee01fad 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ # cscfluview 0.5.1 * Replaced `http` URLs with `https` as `http` ones no longer work (fixes #6) +* Fixed State data download (CDC changed the hidden API) # cdcfluview 0.5.0 diff --git a/R/aaa.r b/R/aaa.r new file mode 100644 index 0000000..b41eca4 --- /dev/null +++ b/R/aaa.r @@ -0,0 +1 @@ +utils::globalVariables(".") \ No newline at end of file diff --git a/R/cdcfluview-package.R b/R/cdcfluview-package.R index 2f9a948..832f2ce 100644 --- a/R/cdcfluview-package.R +++ b/R/cdcfluview-package.R @@ -13,4 +13,5 @@ #' @import httr xml2 dplyr utils #' @importFrom purrr map map_df map_chr map_lgl discard keep #' @importFrom readr read_csv +#' @import V8 NULL diff --git a/R/get_state_data.r b/R/get_state_data.r index e5de4d1..d3eaa42 100644 --- a/R/get_state_data.r +++ b/R/get_state_data.r @@ -10,7 +10,7 @@ #' #' @param years a vector of years to retrieve data for (i.e. \code{2014} for CDC #' flu season 2014-2015). Default value is the current year and all -#' \code{years} values should be > \code{1997} +#' \code{years} values should be >= \code{2008} #' @return A \code{data.frame} of state-level data for the specified seasons #' (also classed as \code{cdcstatedata}) #' @export @@ -26,34 +26,22 @@ #' } get_state_data <- function(years=as.numeric(format(Sys.Date(), "%Y"))) { - if (any(years < 1997)) - stop("Error: years should be > 1997") + if (any(years < 2008)) + stop("Error: years should be >= 2008") - years <- years - 1960 + years <- c((years - 1960), 1) + years <- paste0(years, collapse=",") - out_file <- tempfile(fileext=".zip") - - params <- list(EndMMWRID=0, - StartMMWRID=0, - QueryType=1, - DataMode="STATE", - SeasonsList=paste0(years, collapse=",")) - - tmp <- httr::POST("https://gis.cdc.gov/grasp/fluview/FluViewPhase1CustomDownload.ashx", - body=params, - write_disk(out_file)) + tmp <- httr::GET(sprintf("https://gis.cdc.gov/grasp/fluView1/Phase1DownloadDataP/%s", years)) stop_for_status(tmp) - if (!(file.exists(out_file))) - stop("Error: cannot process downloaded data") - - out_dir <- tempdir() - - files <- unzip(out_file, exdir=out_dir, overwrite=TRUE) + res <- httr::content(tmp, as="parsed") - out <- read.csv(files, header=TRUE, stringsAsFactors=FALSE) - # out <- readr::read_csv(files, col_types="cccccic") + ctx <- v8() + ctx$eval(JS(sprintf("var dat=%s;", res))) + res <- ctx$get("dat", flatten=FALSE) + out <- suppressMessages(readr::type_convert(res$datadownload)) class(out) <- c("cdcstatedata", class(out)) diff --git a/R/mortalty.r b/R/mortalty.r index 94e31aa..e58f96b 100644 --- a/R/mortalty.r +++ b/R/mortalty.r @@ -76,3 +76,4 @@ get_mortality_surveillance_data <- function() { ) } + diff --git a/man/get_state_data.Rd b/man/get_state_data.Rd index 453d244..fb8ee2a 100644 --- a/man/get_state_data.Rd +++ b/man/get_state_data.Rd @@ -9,7 +9,7 @@ get_state_data(years = as.numeric(format(Sys.Date(), "\%Y"))) \arguments{ \item{years}{a vector of years to retrieve data for (i.e. \code{2014} for CDC flu season 2014-2015). Default value is the current year and all -\code{years} values should be > \code{1997}} +\code{years} values should be >= \code{2008}} } \value{ A \code{data.frame} of state-level data for the specified seasons diff --git a/tests/testthat/test-cdcfluview.R b/tests/testthat/test-cdcfluview.R index ab6f62f..17aeb7f 100644 --- a/tests/testthat/test-cdcfluview.R +++ b/tests/testthat/test-cdcfluview.R @@ -1,6 +1,8 @@ context("basic functionality") test_that("we can do something", { - #expect_that(some_function(), is_a("data.frame")) + expect_that(dim(get_flu_data("hhs", years=2015)), equals(c(520L, 15L))) + + expect_that(dim(get_state_data(2008)), equals(c(2494L, 8L))) })