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.

61 lines
2.4 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #' Age Group Distribution of Influenza Positive Tests Reported by Public Health Laboratories
  2. #'
  3. #' Retrieves the age group distribution of influenza positive tests that are reported by
  4. #' public health laboratories by influenza virus type and subtype/lineage. Laboratory data
  5. #' from multiple seasons and different age groups is provided.
  6. #'
  7. #' @references
  8. #' - [CDC FluView Portal](https://gis.cdc.gov/grasp/fluview/fluportaldashboard.html)
  9. #' - [AGD IPT Portal](https://gis.cdc.gov/grasp/fluview/flu_by_age_virus.html)
  10. #' @export
  11. #' @examples \dontrun{
  12. #' agd_ipt()
  13. #' }
  14. age_group_distribution <- function() {
  15. httr::GET(
  16. url = "https://gis.cdc.gov/grasp/fluView6/GetFlu6AllDataP",
  17. httr::user_agent(.cdcfluview_ua),
  18. httr::add_headers(
  19. Accept = "application/json, text/plain, */*",
  20. Referer = "https://gis.cdc.gov/grasp/fluview/flu_by_age_virus.html"
  21. ),
  22. # httr::verbose(),
  23. httr::timeout(60)
  24. ) -> res
  25. httr::stop_for_status(res)
  26. xdat <- httr::content(res, as="parsed")
  27. xdat <- jsonlite::fromJSON(xdat, flatten=TRUE)
  28. sea_names <- c("seasonid", "sea_description", "sea_startweek", "sea_endweek", "sea_enabled",
  29. "sea_label", "sea_showlabtype")
  30. age_names <- c("ageid", "age_label", "age_color_hexvalue", "age_enabled")
  31. typ_names <- c("virusid", "vir_description", "vir_label", "vir_startmmwrid", "vir_endmmwrid",
  32. "vir_displayorder", "vir_colorname", "vir_color_hexvalue", "vir_labtypeid",
  33. "vir_sortid")
  34. vir_names <- c("virusid", "ageid", "count", "mmwrid", "seasonid", "publishyearweekid", "loaddatetime")
  35. sea_df <- stats::setNames(xdat$Season, sea_names)
  36. age_df <- stats::setNames(xdat$Age, age_names)
  37. typ_df <- stats::setNames(xdat$VirusType, typ_names)
  38. vir_df <- stats::setNames(xdat$VirusData, vir_names)
  39. vir_df <- dplyr::left_join(vir_df, sea_df, "seasonid")
  40. vir_df <- dplyr::left_join(vir_df, age_df, "ageid")
  41. vir_df <- dplyr::left_join(vir_df, typ_df, "virusid")
  42. class(vir_df) <- c("tbl_df", "tbl", "data.frame")
  43. vir_df_cols <- c("sea_label", "age_label", "vir_label", "count", "mmwrid", "seasonid",
  44. "publishyearweekid", "sea_description", "sea_startweek",
  45. "sea_endweek", "vir_description", "vir_startmmwrid", "vir_endmmwrid")
  46. vir_df <- vir_df[,vir_df_cols]
  47. vir_df$age_label <- factor(vir_df$age_label, levels=.age_grp)
  48. vir_df$vir_label <- factor(vir_df$vir_label, levels=.vir_grp)
  49. vir_df
  50. }