Browse Source

initial commit

tags/v0.7.0
boB Rudis 6 years ago
commit
9cae6fca71
No known key found for this signature in database GPG Key ID: 2A514A4997464560
  1. 10
      .Rbuildignore
  2. 1
      .codecov.yml
  3. 8
      .gitignore
  4. 31
      .travis.yml
  5. 27
      DESCRIPTION
  6. 2
      LICENSE
  7. 13
      NAMESPACE
  8. 2
      NEWS.md
  9. 8
      R/aaa.R
  10. 52
      R/agd-ipt.r
  11. 11
      R/cdcfluview-package.R
  12. 67
      R/hospital.r
  13. 80
      R/ilinet.r
  14. 15
      R/utils.r
  15. 91
      R/who-nrvess.r
  16. 32
      README.Rmd
  17. 21
      cdcfluview.Rproj
  18. 17
      man/agd_ipt.Rd
  19. 13
      man/cdcfluview.Rd
  20. 22
      man/hospitalizations.Rd
  21. 40
      man/ilinet.Rd
  22. 49
      man/who_nrevss.Rd
  23. 2
      tests/test-all.R
  24. 6
      tests/testthat/test-cdcfluview.R

10
.Rbuildignore

@ -0,0 +1,10 @@
^.*\.Rproj$
^\.Rproj\.user$
^\.travis\.yml$
^README\.*Rmd$
^README\.*html$
^NOTES\.*Rmd$
^NOTES\.*html$
^\.codecov\.yml$
^README_files$
^doc$

1
.codecov.yml

@ -0,0 +1 @@
comment: false

8
.gitignore

@ -0,0 +1,8 @@
.DS_Store
.Rproj.user
.Rhistory
.RData
.Rproj
src/*.o
src/*.so
src/*.dll

31
.travis.yml

@ -0,0 +1,31 @@
language: r
warnings_are_errors: true
sudo: required
cache: packages
r:
- oldrel
- release
- devel
apt_packages:
- libv8-dev
- xclip
env:
global:
- CRAN: http://cran.rstudio.com
after_success:
- Rscript -e 'covr::codecov()'
notifications:
email:
- bob@rud.is
irc:
channels:
- "104.236.112.222#builds"
nick: travisci

27
DESCRIPTION

@ -0,0 +1,27 @@
Package: cdcfluview
Type: Package
Title: cdcfluview title goes here otherwise CRAN checks fail
Version: 0.1.0
Date: 2017-11-04
Authors@R: c(
person("Bob", "Rudis", email = "bob@rud.is", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-5670-2640"))
)
Author: Bob Rudis (bob@rud.is)
Maintainer: Bob Rudis <bob@rud.is>
Description: A good description goes here otherwise CRAN checks fail.
URL: https://github.com/hrbrmstr/cdcfluview
BugReports: https://github.com/hrbrmstr/cdcfluview/issues
License: MIT + file LICENSE
Suggests:
testthat,
covr
Depends:
R (>= 3.2.0)
Imports:
httr,
tools,
dplyr,
jsonlite,
stats
RoxygenNote: 6.0.1

2
LICENSE

@ -0,0 +1,2 @@
YEAR: 2017
COPYRIGHT HOLDER: Bob Rudis

13
NAMESPACE

@ -0,0 +1,13 @@
# Generated by roxygen2: do not edit by hand
export(agd_ipt)
export(hospitalizations)
export(ilinet)
export(who_nrevss)
import(httr)
importFrom(dplyr,"%>%")
importFrom(dplyr,bind_rows)
importFrom(dplyr,left_join)
importFrom(jsonlite,fromJSON)
importFrom(stats,setNames)
importFrom(tools,file_path_sans_ext)

2
NEWS.md

@ -0,0 +1,2 @@
0.1.0
* Initial release

8
R/aaa.R

@ -0,0 +1,8 @@
# CDC U.S. region names to ID map
.region_map <- c(national=3, hhs=1, census=2, state=5)
# CDC hospital surveillance region map
.hosp_surv_map <- c(flusurv=1, eip=2, ihsp=3)
# Our bot's user-agent string
.cdcfluview_ua <- "Mozilla/5.0 (compatible; R-cdcvluview Bot/2.0; https://github.com/hrbrmstr/cdcfluview)"

52
R/agd-ipt.r

@ -0,0 +1,52 @@
#' Age Group Distribution of Influenza Positive Tests Reported by Public Health Laboratories
#'
#' Retrieves the age group distribution of influenza positive tests that are reported by
#' public health laboratories by influenza virus type and subtype/lineage. Laboratory data
#' from multiple seasons and different age groups is provided.
#'
#' @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)
#' @export
agd_ipt <- function() {
httr::GET(
url = "https://gis.cdc.gov/grasp/fluView6/GetFlu6AllDataP",
httr::user_agent(.cdcfluview_ua),
httr::add_headers(
Accept = "application/json, text/plain, */*",
Referer = "https://gis.cdc.gov/grasp/fluview/flu_by_age_virus.html"
),
httr::verbose(),
httr::timeout(60)
) -> res
httr::stop_for_status(res)
xdat <- httr::content(res, as="parsed")
xdat <- jsonlite::fromJSON(xdat, flatten=TRUE)
sea_names <- c("seasonid", "sea_description", "sea_startweek", "sea_endweek", "sea_enabled",
"sea_label", "sea_showlabtype")
age_names <- c("ageid", "age_label", "age_color_hexvalue", "age_enabled")
typ_names <- c("virusid", "vir_description", "vir_label", "vir_startmmwrid", "vir_endmmwrid",
"vir_displayorder", "vir_colorname", "vir_color_hexvalue", "vir_labtypeid",
"vir_sortid")
vir_names <- c("virusid", "ageid", "count", "mmwrid", "seasonid", "publishyearweekid", "loaddatetime")
sea_df <- stats::setNames(xdat$Season, sea_names)
age_df <- stats::setNames(xdat$Age, age_names)
typ_df <- stats::setNames(xdat$VirusType, typ_names)
vir_df <- stats::setNames(xdat$VirusData, vir_names)
vir_df <- dplyr::left_join(vir_df, sea_df, "seasonid")
vir_df <- dplyr::left_join(vir_df, age_df, "ageid")
vir_df <- dplyr::left_join(vir_df, typ_df, "virusid")
class(vir_df) <- c("tbl_df", "tbl", "data.frame")
vir_df_cols <- c("sea_label", "age_label", "vir_label", "count", "mmwrid", "seasonid",
"publishyearweekid", "sea_description", "sea_startweek", "sea_endweek",
"vir_description", "vir_startmmwrid", "vir_endmmwrid")
vir_df[,vir_df_cols]
}

11
R/cdcfluview-package.R

@ -0,0 +1,11 @@
#' ...
#'
#' @name cdcfluview
#' @docType package
#' @author Bob Rudis (bob@@rud.is)
#' @import httr
#' @importFrom tools file_path_sans_ext
#' @importFrom dplyr left_join bind_rows %>%
#' @importFrom jsonlite fromJSON
#' @importFrom stats setNames
NULL

67
R/hospital.r

@ -0,0 +1,67 @@
#' Laboratory-Confirmed Influenza Hospitalizations
#'
#' @param surveillance_area one of "`flusurv`", "`eip`", or "`ihsp`"
#' @references
#' - [Hospital Portal](https://gis.cdc.gov/GRASP/Fluview/FluHospRates.html)
#' @export
#' @examples
#' hosp_fs <- hospitalizations("flusurv")
#' hosp_eip <- hospitalizations("eip")
#' hosp_ihsp <- hospitalizations("ihsp")
hospitalizations <- function(surveillance_area=c("flusurv", "eip", "ihsp")) {
surveillance_area <- match.arg(tolower(surveillance_area), c("flusurv", "eip", "ihsp"))
network_id <- .hosp_surv_map[surveillance_area]
meta <- jsonlite::fromJSON("https://gis.cdc.gov/GRASP/Flu3/GetPhase03InitApp?appVersion=Public")
httr::POST(
url = "https://gis.cdc.gov/GRASP/Flu3/PostPhase03GetData",
httr::user_agent(.cdcfluview_ua),
httr::add_headers(
Origin = "https://gis.cdc.gov",
Accept = "application/json, text/plain, */*",
Referer = "https://gis.cdc.gov/grasp/fluview/fluportaldashboard.html"
),
encode = "json",
body = list(
appversion = "Public",
networkid = network_id,
cacthmentid = "22"
),
httr::verbose()
) -> res
httr::stop_for_status(res)
res <- httr::content(res)
hosp <- list(res = res, meta = meta)
age_df <- setNames(hosp$meta$ages, c("age_label", "age", "color"))
age_df <- age_df[,c("age", "age_label")]
sea_df <- setNames(
hosp$meta$seasons,
c("sea_description", "sea_endweek", "sea_label", "seasonid", "sea_startweek", "color", "color_hexvalue"))
sea_df <- sea_df[,c("seasonid", "sea_label", "sea_description", "sea_startweek", "sea_endweek")]
ser_names <- unlist(hosp$res$busdata$datafields, use.names = FALSE)
mmwr_df <- bind_rows(hosp$res$mmwr)
mmwr_df <- mmwr_df[,c("mmwrid", "weekend", "weeknumber", "weekstart", "year",
"yearweek", "seasonid", "weekendlabel", "weekendlabel2")]
dplyr::bind_rows(lapply(hosp$res$busdata$dataseries, function(.x) {
tdf <- dplyr::bind_rows(lapply(.x$data, function(.x) setNames(.x, ser_names)))
tdf$age <- .x$age
tdf$season <- .x$season
tdf
})) -> xdf
dplyr::left_join(xdf, mmwr_df, c("mmwrid", "weeknumber")) %>%
dplyr::left_join(age_df, "age") %>%
dplyr::left_join(sea_df, "seasonid")
}

80
R/ilinet.r

@ -0,0 +1,80 @@
#' Retrieve ILINet Surveillance Data
#'
#' The CDC FluView Portal provides in-season and past seasons' national, regional,
#' and state-level outpatient illness and viral surveillance data from both
#' ILINet (Influenza-like Illness Surveillance Network) and WHO/NREVSS
#' (National Respiratory and Enteric Virus Surveillance System).
#'
#' This function retrieves current and historical ILINet surveillance data for
#' the identified region.
#'
#' @md
#' @param region one of "`national`", "`hhs`", "`census`", or "`state`"
#' @references
#' - [CDC FluView Portal](https://gis.cdc.gov/grasp/fluview/fluportaldashboard.html)
#' - [ILINet Portal](https://wwwn.cdc.gov/ilinet/) (Login required)
#' - [WHO/NREVSS](https://www.cdc.gov/surveillance/nrevss/index.html)
#' @export
#' @examples
#' national_ili <- ilinet("national")
#' hhs_ili <- ilinet("hhs")
#' census_ili <- ilinet("census")
#' state_ili <- ilinet("state")
#' \dontrun{
#' library(purrr)
#' map_df(
#' c("national", "hhs", "census", "state"),
#' ~ilinet(.x) %>% readr::type_convert())
#' }
ilinet <- function(region=c("national", "hhs", "census", "state")) {
region <- match.arg(tolower(region), c("national", "hhs", "census", "state"))
list(
AppVersion = "Public",
DatasourceDT = list(list(ID = 1, Name = "ILINet")),
RegionTypeId = .region_map[region]
) -> params
params$SubRegionsDT <- switch(region,
national = { list(list(ID=0, Name="")) },
hhs = { lapply(1:10, function(i) list(ID=i, Name=as.character(i))) },
census = { lapply(1:9, function(i) list(ID=i, Name=as.character(i))) },
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)))
tf <- tempfile(fileext = ".zip")
td <- tempdir()
on.exit(unlink(tf), TRUE)
httr::POST(
url = "https://gis.cdc.gov/grasp/flu2/PostPhase02DataDownload",
httr::user_agent(.cdcfluview_ua),
httr::add_headers(
Origin = "https://gis.cdc.gov",
Accept = "application/json, text/plain, */*",
Referer = "https://gis.cdc.gov/grasp/fluview/fluportaldashboard.html"
),
encode = "json",
body = params,
httr::verbose(),
httr::write_disk(tf)
) -> res
httr::stop_for_status(res)
nm <- unzip(tf, overwrite = TRUE, exdir = td)
xdf <- read.csv(nm, skip = 1, stringsAsFactors=FALSE)
xdf <- .mcga(xdf)
class(xdf) <- c("tbl_df", "tbl", "data.frame")
xdf[xdf=="X"] <- NA
xdf
}

15
R/utils.r

@ -0,0 +1,15 @@
.mcga <- function(tbl) {
x <- colnames(tbl)
x <- tolower(x)
x <- gsub("[[:punct:][:space:]]+", "_", x)
x <- gsub("_+", "_", x)
x <- gsub("(^_|_$)", "", x)
x <- gsub("^x_", "", x)
x <- make.unique(x, sep = "_")
colnames(tbl) <- x
tbl
}

91
R/who-nrvess.r

@ -0,0 +1,91 @@
#' Retrieve WHO/NREVSS Surveillance Data
#'
#' The CDC FluView Portal provides in-season and past seasons' national, regional,
#' and state-level outpatient illness and viral surveillance data from both
#' ILINet (Influenza-like Illness Surveillance Network) and WHO/NREVSS
#' (National Respiratory and Enteric Virus Surveillance System).
#'
#' This function retrieves current and historical WHO/NREVSS surveillance data for
#' the identified region.
#'
#' @md
#' @note 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.
#' @param region one of "`national`", "`hhs`", "`census`", or "`state`"
#' @return list of data frames identified by
#' - `combined_prior_to_2015_16`
#' - `public_health_labs`
#' - `clinical_labs`
#' @references
#' - [CDC FluView Portal](https://gis.cdc.gov/grasp/fluview/fluportaldashboard.html)
#' - [ILINet Portal](https://wwwn.cdc.gov/ilinet/) (Login required)
#' - [WHO/NREVSS](https://www.cdc.gov/surveillance/nrevss/index.html)
#' @export
#' @examples
#' national_who <- who_nrevss("national")
#' hhs_who <- who_nrevss("hhs")
#' census_who <- who_nrevss("census")
#' state_who <- who_nrevss("state")
who_nrevss <- function(region=c("national", "hhs", "census", "state")) {
region <- match.arg(tolower(region), c("national", "hhs", "census", "state"))
list(
AppVersion = "Public",
DatasourceDT = list(list(ID = 1, Name = "WHO_NREVSS")),
RegionTypeId = .region_map[region]
) -> params
params$SubRegionsDT <- switch(
region,
national = { list(list(ID=0, Name="")) },
hhs = { lapply(1:10, function(i) list(ID=i, Name=as.character(i))) },
census = { lapply(1:9, function(i) list(ID=i, Name=as.character(i))) },
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)))
tf <- tempfile(fileext = ".zip")
td <- tempdir()
on.exit(unlink(tf), TRUE)
httr::POST(
url = "https://gis.cdc.gov/grasp/flu2/PostPhase02DataDownload",
httr::user_agent(.cdcfluview_ua),
httr::add_headers(
Origin = "https://gis.cdc.gov",
Accept = "application/json, text/plain, */*",
Referer = "https://gis.cdc.gov/grasp/fluview/fluportaldashboard.html"
),
encode = "json",
body = params,
httr::verbose(),
httr::timeout(60),
httr::write_disk(tf)
) -> res
httr::stop_for_status(res)
nm <- unzip(tf, overwrite = TRUE, exdir = td)
lapply(nm, function(x) {
tdf <- read.csv(x, skip = 1, stringsAsFactors=FALSE)
tdf <- .mcga(tdf)
class(tdf) <- c("tbl_df", "tbl", "data.frame")
tdf[tdf=="X"] <- NA
tdf
}) -> xdf
setNames(xdf, sub("who_nrevss_", "", tools::file_path_sans_ext(tolower(basename(nm)))))
}

32
README.Rmd

@ -0,0 +1,32 @@
---
output: rmarkdown::github_document
---
# cdcfluview
## Description
## What's Inside The Tin
The following functions are implemented:
## Installation
```{r eval=FALSE}
devtools::install_github("hrbrmstr/cdcfluview")
```
```{r message=FALSE, warning=FALSE, error=FALSE, include=FALSE}
options(width=120)
```
## Usage
```{r message=FALSE, warning=FALSE, error=FALSE}
library(cdcfluview)
# current verison
packageVersion("cdcfluview")
```

21
cdcfluview.Rproj

@ -0,0 +1,21 @@
Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX
StripTrailingWhitespace: Yes
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageBuildArgs: --resave-data
PackageRoxygenize: rd,collate,namespace

17
man/agd_ipt.Rd

@ -0,0 +1,17 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/agd-ipt.r
\name{agd_ipt}
\alias{agd_ipt}
\title{Age Group Distribution of Influenza Positive Tests Reported by Public Health Laboratories}
\usage{
agd_ipt()
}
\description{
Retrieves the age group distribution of influenza positive tests that are reported by
public health laboratories by influenza virus type and subtype/lineage. Laboratory data
from multiple seasons and different age groups is provided.
}
\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)
}

13
man/cdcfluview.Rd

@ -0,0 +1,13 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/cdcfluview-package.R
\docType{package}
\name{cdcfluview}
\alias{cdcfluview}
\alias{cdcfluview-package}
\title{...}
\description{
...
}
\author{
Bob Rudis (bob@rud.is)
}

22
man/hospitalizations.Rd

@ -0,0 +1,22 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/hospital.r
\name{hospitalizations}
\alias{hospitalizations}
\title{Laboratory-Confirmed Influenza Hospitalizations}
\usage{
hospitalizations(surveillance_area = c("flusurv", "eip", "ihsp"))
}
\arguments{
\item{surveillance_area}{one of "`flusurv`", "`eip`", or "`ihsp`"}
}
\description{
Laboratory-Confirmed Influenza Hospitalizations
}
\examples{
hosp_fs <- hospitalizations("flusurv")
hosp_eip <- hospitalizations("eip")
hosp_ihsp <- hospitalizations("ihsp")
}
\references{
- [Hospital Portal](https://gis.cdc.gov/GRASP/Fluview/FluHospRates.html)
}

40
man/ilinet.Rd

@ -0,0 +1,40 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ilinet.r
\name{ilinet}
\alias{ilinet}
\title{Retrieve ILINet Surveillance Data}
\usage{
ilinet(region = c("national", "hhs", "census", "state"))
}
\arguments{
\item{region}{one of "\code{national}", "\code{hhs}", "\code{census}", or "\code{state}"}
}
\description{
The CDC FluView Portal provides in-season and past seasons' national, regional,
and state-level outpatient illness and viral surveillance data from both
ILINet (Influenza-like Illness Surveillance Network) and WHO/NREVSS
(National Respiratory and Enteric Virus Surveillance System).
}
\details{
This function retrieves current and historical ILINet surveillance data for
the identified region.
}
\examples{
national_ili <- ilinet("national")
hhs_ili <- ilinet("hhs")
census_ili <- ilinet("census")
state_ili <- ilinet("state")
\dontrun{
library(purrr)
map_df(
c("national", "hhs", "census", "state"),
~ilinet(.x) \%>\% readr::type_convert())
}
}
\references{
\itemize{
\item \href{https://gis.cdc.gov/grasp/fluview/fluportaldashboard.html}{CDC FluView Portal}
\item \href{https://wwwn.cdc.gov/ilinet/}{ILINet Portal} (Login required)
\item \href{https://www.cdc.gov/surveillance/nrevss/index.html}{WHO/NREVSS}
}
}

49
man/who_nrevss.Rd

@ -0,0 +1,49 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/who-nrvess.r
\name{who_nrevss}
\alias{who_nrevss}
\title{Retrieve WHO/NREVSS Surveillance Data}
\usage{
who_nrevss(region = c("national", "hhs", "census", "state"))
}
\arguments{
\item{region}{one of "\code{national}", "\code{hhs}", "\code{census}", or "\code{state}"}
}
\value{
list of data frames identified by
\itemize{
\item \code{combined_prior_to_2015_16}
\item \code{public_health_labs}
\item \code{clinical_labs}
}
}
\description{
The CDC FluView Portal provides in-season and past seasons' national, regional,
and state-level outpatient illness and viral surveillance data from both
ILINet (Influenza-like Illness Surveillance Network) and WHO/NREVSS
(National Respiratory and Enteric Virus Surveillance System).
}
\details{
This function retrieves current and historical WHO/NREVSS surveillance data for
the identified region.
}
\note{
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.
}
\examples{
national_who <- who_nrevss("national")
hhs_who <- who_nrevss("hhs")
census_who <- who_nrevss("census")
state_who <- who_nrevss("state")
}
\references{
\itemize{
\item \href{https://gis.cdc.gov/grasp/fluview/fluportaldashboard.html}{CDC FluView Portal}
\item \href{https://wwwn.cdc.gov/ilinet/}{ILINet Portal} (Login required)
\item \href{https://www.cdc.gov/surveillance/nrevss/index.html}{WHO/NREVSS}
}
}

2
tests/test-all.R

@ -0,0 +1,2 @@
library(testthat)
test_check("cdcfluview")

6
tests/testthat/test-cdcfluview.R

@ -0,0 +1,6 @@
context("basic functionality")
test_that("we can do something", {
#expect_that(some_function(), is_a("data.frame"))
})
Loading…
Cancel
Save