You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.4 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  1. #' Retrieve ILINet Surveillance Data
  2. #'
  3. #' The CDC FluView Portal provides in-season and past seasons' national, regional,
  4. #' and state-level outpatient illness and viral surveillance data from both
  5. #' ILINet (Influenza-like Illness Surveillance Network) and WHO/NREVSS
  6. #' (National Respiratory and Enteric Virus Surveillance System).
  7. #'
  8. #' This function retrieves current and historical ILINet surveillance data for
  9. #' the identified region.
  10. #'
  11. #' @md
  12. #' @param region one of "`national`", "`hhs`", "`census`", or "`state`"
  13. #' @references
  14. #' - [CDC FluView Portal](https://gis.cdc.gov/grasp/fluview/fluportaldashboard.html)
  15. #' - [ILINet Portal](https://wwwn.cdc.gov/ilinet/) (Login required)
  16. #' - [WHO/NREVSS](https://www.cdc.gov/surveillance/nrevss/index.html)
  17. #' @export
  18. #' @examples
  19. #' national_ili <- ilinet("national")
  20. #' hhs_ili <- ilinet("hhs")
  21. #' census_ili <- ilinet("census")
  22. #' state_ili <- ilinet("state")
  23. #' \dontrun{
  24. #' library(purrr)
  25. #' map_df(
  26. #' c("national", "hhs", "census", "state"),
  27. #' ~ilinet(.x) %>% readr::type_convert())
  28. #' }
  29. ilinet <- function(region=c("national", "hhs", "census", "state")) {
  30. region <- match.arg(tolower(region), c("national", "hhs", "census", "state"))
  31. list(
  32. AppVersion = "Public",
  33. DatasourceDT = list(list(ID = 1, Name = "ILINet")),
  34. RegionTypeId = .region_map[region]
  35. ) -> params
  36. params$SubRegionsDT <- switch(region,
  37. national = { list(list(ID=0, Name="")) },
  38. hhs = { lapply(1:10, function(i) list(ID=i, Name=as.character(i))) },
  39. census = { lapply(1:9, function(i) list(ID=i, Name=as.character(i))) },
  40. state = { lapply(1:59, function(i) list(ID=i, Name=as.character(i))) }
  41. )
  42. seasons <- 37:((unclass(as.POSIXlt(Sys.time()))[["year"]] + 1900) - 1960)
  43. params$SeasonsDT <- lapply(seasons, function(i) list(ID=i, Name=as.character(i)))
  44. tf <- tempfile(fileext = ".zip")
  45. td <- tempdir()
  46. on.exit(unlink(tf), TRUE)
  47. httr::POST(
  48. url = "https://gis.cdc.gov/grasp/flu2/PostPhase02DataDownload",
  49. httr::user_agent(.cdcfluview_ua),
  50. httr::add_headers(
  51. Origin = "https://gis.cdc.gov",
  52. Accept = "application/json, text/plain, */*",
  53. Referer = "https://gis.cdc.gov/grasp/fluview/fluportaldashboard.html"
  54. ),
  55. encode = "json",
  56. body = params,
  57. # httr::verbose(),
  58. httr::write_disk(tf)
  59. ) -> res
  60. httr::stop_for_status(res)
  61. nm <- unzip(tf, overwrite = TRUE, exdir = td)
  62. xdf <- read.csv(nm, skip = 1, stringsAsFactors=FALSE)
  63. xdf <- .mcga(xdf)
  64. suppressWarnings(xdf$weighted_ili <- to_num(xdf$weighted_ili))
  65. suppressWarnings(xdf$unweighted_ili <- to_num(xdf$unweighted_ili))
  66. suppressWarnings(xdf$age_0_4 <- to_num(xdf$age_0_4))
  67. suppressWarnings(xdf$age_25_49 <- to_num(xdf$age_25_49))
  68. suppressWarnings(xdf$age_25_64 <- to_num(xdf$age_25_64))
  69. suppressWarnings(xdf$age_5_24 <- to_num(xdf$age_5_24))
  70. suppressWarnings(xdf$age_50_64 <- to_num(xdf$age_50_64))
  71. suppressWarnings(xdf$age_65 <- to_num(xdf$age_65))
  72. suppressWarnings(xdf$ilitotal <- to_num(xdf$ilitotal))
  73. suppressWarnings(xdf$num_of_providers <- to_num(xdf$num_of_providers))
  74. suppressWarnings(xdf$total_patients <- to_num(xdf$total_patients))
  75. suppressWarnings(xdf$week_start <- as.Date(sprintf("%s-%02d-1", xdf$year, xdf$week), "%Y-%U-%u"))
  76. if (region == "national") xdf$region <- "National"
  77. if (region == "hhs") xdf$region <- factor(xdf$region, levels=sprintf("Region %s", 1:10))
  78. class(xdf) <- c("tbl_df", "tbl", "data.frame")
  79. xdf
  80. }