Skip to content

Commit 933e171

Browse files
committed
clean up
1 parent 7a020d2 commit 933e171

File tree

6 files changed

+63
-41
lines changed

6 files changed

+63
-41
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: hubhelpr
22
Title: Helper functions for CDC hubs maintainance
33
Version: 0.0.0.9000
4-
Authors@R:
5-
person("Subekshya", "Bidari",
4+
Authors@R:
5+
person("Subekshya", "Bidari",
66
email = "[email protected]",
77
role = c("aut", "cre"))
88
Description: This package provides functions to help with the maintainance of CDC hubs.

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(update_hub_target_data)

R/update_hub_target_data.R

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,27 @@ nssp_col_names <- list(
1212
##' This function pulls, formats and save NHSN and NSSP data for use in the hub.
1313
##'
1414
##' @param base_hub_path Path to the base hub directory.
15-
##' @param disease Disease name (e.g., "covid", "rsv").
15+
##' @param disease Disease name ("covid" or "rsv").
1616
##' @param excluded_locations Vector of location codes to exclude from the output.
17-
##' @param first_full_weekending_date First week-ending date to include.
17+
##' Default value is c("78", "69", "66", "60"), corresponding to "US Virgin Islands",
18+
##' "Guam", "American Samoa", and "Northern Mariana Islands".
19+
##' @param nhsn_first_weekending_date First week-ending date to include for
20+
##' the NHSN dataset. Default value is "2024-11-09".
1821
##' @param legacy_file Logical. Whether to write legacy CSV output (default: FALSE).
1922
##'
20-
##' @returns Writes target data files to target-data directory in the hub.
23+
##' @return Writes `time-series.parquet` and optionally legacy csv target data files
24+
##' to the target-data directory in the hub.
2125
##' @export
2226
update_hub_target_data <- function(
2327
base_hub_path,
2428
disease,
25-
first_full_weekending_date = lubridate::as_date("2024-11-09"),
26-
excluded_locations = c("78", "0", "69", "66", "60"),
29+
nhsn_first_weekending_date = lubridate::as_date("2024-11-09"),
30+
excluded_locations = c("78", "69", "66", "60"),
2731
legacy_file = FALSE
2832
) {
33+
if (!disease %in% c("covid", "rsv")) {
34+
stop("'disease' must be either 'covid' or 'rsv'")
35+
}
2936
today <- lubridate::today()
3037
output_dirpath <- fs::path(base_hub_path, "target-data")
3138
fs::dir_create(output_dirpath)
@@ -36,14 +43,14 @@ update_hub_target_data <- function(
3643
nhsn_data <- forecasttools::pull_data_cdc_gov_dataset(
3744
dataset = "nhsn_hrd_prelim",
3845
columns = nhsn_col_name,
39-
start_date = first_full_weekending_date
46+
start_date = nhsn_first_weekending_date
4047
) |>
4148
dplyr::rename(
4249
observation = nhsn_col_name,
4350
date = "weekendingdate"
4451
) |>
4552
dplyr::mutate(
46-
date = as.Date(.data$date),
53+
date = lubridate::as_date(.data$date),
4754
observation = as.numeric(.data$observation),
4855
jurisdiction = stringr::str_replace(.data$jurisdiction, "USA", "US")
4956
) |>
@@ -82,7 +89,7 @@ update_hub_target_data <- function(
8289
locations = "All"
8390
) |>
8491
dplyr::mutate(
85-
date = as.Date(.data$week_end),
92+
date = lubridate::as_date(.data$week_end),
8693
observation = as.numeric(.data[[nssp_col_name]]) / 100,
8794
location = forecasttools::us_location_recode(
8895
.data$geography,

air.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ line-ending = "auto"
66
persistent-line-breaks = true
77
exclude = []
88
default-exclude = true
9-
skip = []
9+
skip = []

man/update_hub_target_data.Rd

Lines changed: 10 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
1-
library(fs)
2-
library(dplyr)
3-
library(testthat)
4-
library(hubhelpr)
5-
library(forecasttools)
6-
7-
run_update_hub_target_data_test <- function(
1+
update_hub_target_data_test <- function(
82
disease,
93
nhsn_col,
104
nssp_col,
115
nhsn_target,
126
nssp_target
137
) {
148
base_hub_path <- tempfile(paste0("base_hub_", disease, "_"))
15-
dir_create(base_hub_path)
16-
output_file <- path(base_hub_path, "target-data/time-series.parquet")
9+
fs::dir_create(base_hub_path)
10+
output_file <- fs::path(base_hub_path, "target-data/time-series.parquet")
1711
fs::dir_create(fs::path(base_hub_path, "target-data"))
1812

1913
mock_target_ts <- tibble::tibble(
20-
date = as.Date(character()),
14+
date = lubridate::as_date(character()),
2115
observation = numeric(),
2216
location = character(),
23-
as_of = as.Date(character()),
17+
as_of = lubridate::as_date(character()),
2418
target = character()
2519
)
2620
forecasttools::write_tabular_file(mock_target_ts, output_file)
2721

2822
mock_nhsn <- tibble::tibble(
29-
weekendingdate = as.Date(c(
23+
weekendingdate = lubridate::as_date(c(
3024
"2024-11-09",
3125
"2024-11-16",
3226
"2024-11-09",
@@ -37,7 +31,7 @@ run_update_hub_target_data_test <- function(
3731
totalconfrsvnewadm = c(3, 4, 3, 2)
3832
)
3933
mock_nssp <- tibble::tibble(
40-
week_end = as.Date(c("2024-11-09", "2024-11-09")),
34+
week_end = lubridate::as_date(c("2024-11-09", "2024-11-09")),
4135
county = c("All", "All"),
4236
geography = c("Alabama", "Alaska"),
4337
percent_visits_covid = c(1.0, 0.2),
@@ -52,17 +46,16 @@ run_update_hub_target_data_test <- function(
5246
} else if (dataset == "nssp_prop_ed_visits") {
5347
mock_nssp |> dplyr::select(week_end, geography, all_of(columns))
5448
} else {
55-
stop("Unknown dataset")
49+
stop("Dataset type not supported by the Hubs")
5650
}
5751
},
5852
ns = "forecasttools"
5953
)
6054

6155
expect_silent(
62-
update_hub_target_data(
56+
hubhelpr::update_hub_target_data(
6357
base_hub_path = base_hub_path,
64-
disease = disease,
65-
first_full_weekending_date = as.Date("2024-11-09")
58+
disease = disease
6659
)
6760
)
6861

@@ -78,23 +71,30 @@ run_update_hub_target_data_test <- function(
7871
dplyr::select(date, observation, location, target) |>
7972
dplyr::arrange(date, location),
8073
tibble::tibble(
81-
date = as.Date(c(mock_nhsn$weekendingdate, mock_nssp$week_end)),
74+
date = lubridate::as_date(c(
75+
mock_nhsn$weekendingdate,
76+
mock_nssp$week_end
77+
)),
8278
observation = c(
8379
as.numeric(mock_nhsn[[nhsn_col]]),
8480
as.numeric(mock_nssp[[nssp_col]]) / 100
8581
),
8682
location = c(
87-
us_location_recode(mock_nhsn$jurisdiction, "abbr", "code"),
88-
us_location_recode(mock_nssp$geography, "name", "code")
83+
forecasttools::us_location_recode(
84+
mock_nhsn$jurisdiction,
85+
"abbr",
86+
"code"
87+
),
88+
forecasttools::us_location_recode(mock_nssp$geography, "name", "code")
8989
),
9090
target = c(rep(nhsn_target, 4), rep(nssp_target, 2))
9191
) |>
9292
dplyr::arrange(date, location)
9393
)
9494
}
9595

96-
test_that("update_hub_target_data returns exactly the mocked NHSN data for covid", {
97-
run_update_hub_target_data_test(
96+
test_that("update_hub_target_data returns expected data for covid", {
97+
update_hub_target_data_test(
9898
disease = "covid",
9999
nhsn_col = "totalconfc19newadm",
100100
nssp_col = "percent_visits_covid",
@@ -103,12 +103,22 @@ test_that("update_hub_target_data returns exactly the mocked NHSN data for covid
103103
)
104104
})
105105

106-
test_that("update_hub_target_data returns exactly the mocked NHSN data for rsv", {
107-
run_update_hub_target_data_test(
106+
test_that("update_hub_target_data returns expected data for rsv", {
107+
update_hub_target_data_test(
108108
disease = "rsv",
109109
nhsn_col = "totalconfrsvnewadm",
110110
nssp_col = "percent_visits_rsv",
111111
nhsn_target = "wk inc rsv hosp",
112112
nssp_target = "wk inc rsv prop ed visits"
113113
)
114114
})
115+
116+
test_that("update_hub_target_data errors for unsupported disease", {
117+
expect_error(
118+
update_hub_target_data(
119+
base_hub_path = tempdir(),
120+
disease = "flu"
121+
),
122+
"'disease' must be either 'covid' or 'rsv'"
123+
)
124+
})

0 commit comments

Comments
 (0)