diff --git a/DESCRIPTION b/DESCRIPTION index df397e9..9f15f8b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: cdcfluview Type: Package Title: cdcfluview is package that retrieves the data behind the CDC's FluView portal -Version: 0.1 -Date: 2015-01-10 +Version: 0.2 +Date: 2015-01-11 Author: Bob Rudis (@hrbrmstr) Maintainer: Bob Rudis Description: The CDC's FluView is a Flash portal and the only way to get flu season diff --git a/NAMESPACE b/NAMESPACE index eac7d32..b161c27 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,4 +1,5 @@ # Generated by roxygen2 (4.1.0): do not edit by hand export(get_flu_data) +export(get_state_data) import(httr) diff --git a/R/cdcfluview.R b/R/cdcfluview.R index e161692..d510d76 100644 --- a/R/cdcfluview.R +++ b/R/cdcfluview.R @@ -85,3 +85,48 @@ get_flu_data <- function(region="hhs", sub_region=1:10, } } + +#' Retrieves the state-level data from the CDC's FluView Portal +#' +#' Uses the data source from the CDC' State-levelFluView \url{http://gis.cdc.gov/grasp/fluview/main.html} +#' and provides state flu reporting data as a single data frame +#' +#' @param years a vector of years to retrieve data for (i.e. \code{2014} for CDC flu seasn 2014-2015) +#' @return A \code{data.frame} of state-level data for the specified seasons +#' @export +#' @examples \dontrun{ +#' get_state_dat(2014) +#' get_state_data(c(2013,2014)) +#' } +get_state_data <- function(years=2014) { + + if (any(years < 1997)) + stop("Error: years should be > 1997") + + years <- years - 1960 + + out_file <- tempfile(fileext=".zip") + + params <- list(EndMMWRID=0, + StartMMWRID=0, + QueryType=1, + DataMode="STATE", + SeasonsList=years) + + tmp <- POST("http://gis.cdc.gov/grasp/fluview/FluViewPhase1CustomDownload.ashx", + body=params, + write_disk(out_file)) + + 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) + + read.csv(files, header=TRUE, stringsAsFactors=FALSE) + +} + diff --git a/README.Rmd b/README.Rmd index 3022885..5d6b810 100644 --- a/README.Rmd +++ b/README.Rmd @@ -1,7 +1,7 @@ --- title: "README" author: "Bob Rudis" -date: January 10, 2015 +date: January 11, 2015 output: md_document: variant: markdown_github @@ -13,13 +13,15 @@ uses the same API the portal does to programmatically retrieve data. The following functions are implemented: -- `get_flu_data` : retrieve flu data +- `get_flu_data` : retrieve flu data (national, by various region/sub-region types) +- `get_state_data` : retrieve state-level flu data The following data sets are included: ### News - Version 0.1 released +- Version 0.2 releases : added state-level data retrieval ### Installation @@ -36,12 +38,16 @@ options(width=120) ```{r} library(cdcfluview) library(ggplot2) +library(dplyr) +library(statebins) # current verison packageVersion("cdcfluview") flu <- get_flu_data("hhs", sub_region=1:10, "ilinet", years=2014) +state_flu <- get_state_data() + dplyr::glimpse(flu) gg <- ggplot(flu, aes(x=WEEK, y=X..WEIGHTED.ILI, group=REGION)) @@ -54,6 +60,22 @@ gg <- gg + theme_bw() gg ``` +```{r} +gg_s <- state_flu %>% + filter(WEEKEND=="Jan-03-2015") %>% + select(state=STATENAME, value=ACTIVITY.LEVEL) %>% + filter(!(state %in% c("Puerto Rico", "New York City"))) %>% # need to add PR to statebins + mutate(value=as.numeric(gsub("Level ", "", value))) %>% + statebins(brewer_pal="RdPu", breaks=4, + labels=c("Minimal", "Low", "Moderate", "High"), + legend_position="bottom", legend_title="ILI Activity Level") + + ggtitle("CDC State FluView (2014-01-03)") +``` + +```{r echo=FALSE, fig.height=5, fig.width=7} +gg_s +``` + ### Test Results ```{r} diff --git a/README.md b/README.md index 72b98ab..0c2e4e3 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,15 @@ The CDC's FluView is a Flash portal and the only way to get flu season data is t The following functions are implemented: -- `get_flu_data` : retrieve flu data +- `get_flu_data` : retrieve flu data (national, by various region/sub-region types) +- `get_state_data` : retrieve state-level flu data The following data sets are included: ### News - Version 0.1 released +- Version 0.2 releases : added state-level data retrieval ### Installation @@ -26,7 +28,30 @@ library(cdcfluview) ``` r library(ggplot2) +library(dplyr) +``` + + ## + ## Attaching package: 'dplyr' + ## + ## The following object is masked from 'package:stats': + ## + ## filter + ## + ## The following objects are masked from 'package:base': + ## + ## intersect, setdiff, setequal, union + +``` r +library(statebins) +``` + + ## Loading required package: grid + ## Loading required package: scales + ## Loading required package: gridExtra + ## Loading required package: RColorBrewer +``` r # current verison packageVersion("cdcfluview") ``` @@ -36,9 +61,12 @@ packageVersion("cdcfluview") ``` r flu <- get_flu_data("hhs", sub_region=1:10, "ilinet", years=2014) +state_flu <- get_state_data() + dplyr::glimpse(flu) ``` + ## Observations: 140 ## Variables: ## $ REGION.TYPE (chr) "HHS Regions", "HHS Regions", "HHS Regions", "HHS Regions", "HHS Regions", "HHS Regions",... ## $ REGION (chr) "Region 1", "Region 2", "Region 3", "Region 4", "Region 5", "Region 6", "Region 7", "Regi... @@ -65,6 +93,20 @@ gg <- gg + theme_bw() ![](README_files/figure-markdown_github/unnamed-chunk-4-1.png) +``` r +gg_s <- state_flu %>% + filter(WEEKEND=="Jan-03-2015") %>% + select(state=STATENAME, value=ACTIVITY.LEVEL) %>% + filter(!(state %in% c("Puerto Rico", "New York City"))) %>% # need to add PR to statebins + mutate(value=as.numeric(gsub("Level ", "", value))) %>% + statebins(brewer_pal="RdPu", breaks=4, + labels=c("Minimal", "Low", "Moderate", "High"), + legend_position="bottom", legend_title="ILI Activity Level") + + ggtitle("CDC State FluView (2014-01-03)") +``` + +![](README_files/figure-markdown_github/unnamed-chunk-6-1.png) + ### Test Results ``` r @@ -74,7 +116,7 @@ library(testthat) date() ``` - ## [1] "Sat Jan 10 20:25:05 2015" + ## [1] "Sun Jan 11 10:53:36 2015" ``` r test_dir("tests/") diff --git a/README_files/figure-markdown_github/unnamed-chunk-6-1.png b/README_files/figure-markdown_github/unnamed-chunk-6-1.png new file mode 100644 index 0000000..79a24a1 Binary files /dev/null and b/README_files/figure-markdown_github/unnamed-chunk-6-1.png differ diff --git a/man/get_state_data.Rd b/man/get_state_data.Rd new file mode 100644 index 0000000..f3859c0 --- /dev/null +++ b/man/get_state_data.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2 (4.1.0): do not edit by hand +% Please edit documentation in R/cdcfluview.R +\name{get_state_data} +\alias{get_state_data} +\title{Retrieves the state-level data from the CDC's FluView Portal} +\usage{ +get_state_data(years = 2014) +} +\arguments{ +\item{years}{a vector of years to retrieve data for (i.e. \code{2014} for CDC flu seasn 2014-2015)} +} +\value{ +A \code{data.frame} of state-level data for the specified seasons +} +\description{ +Uses the data source from the CDC' State-levelFluView \url{http://gis.cdc.gov/grasp/fluview/main.html} +and provides state flu reporting data as a single data frame +} +\examples{ +\dontrun{ +get_state_dat(2014) +get_state_data(c(2013,2014)) +} +} +