Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
^LICENSE\.md$
^\.github$
1 change: 1 addition & 0 deletions .github/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.html
53 changes: 53 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
name: R-CMD-check.yaml

on:
pull_request:
push:
branches: [main]

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
cancel-in-progress: true

permissions: read-all

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: macos-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
- {os: ubuntu-latest, r: 'release'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
needs: check

- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")'
62 changes: 62 additions & 0 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:

name: test-coverage.yaml

permissions: read-all

jobs:
test-coverage:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::covr, any::xml2
needs: coverage

- name: Test coverage
run: |
cov <- covr::package_coverage(
quiet = FALSE,
clean = FALSE,
install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package")
)
print(cov)
covr::to_cobertura(cov)
shell: Rscript {0}

- uses: codecov/codecov-action@v5
with:
# Fail if error if not on PR, or if on PR and token is given
fail_ci_if_error: ${{ github.event_name != 'pull_request' || secrets.CODECOV_TOKEN }}
files: ./cobertura.xml
plugins: noop
disable_search: true
token: ${{ secrets.CODECOV_TOKEN }}

- name: Show testthat output
if: always()
run: |
## --------------------------------------------------------------------
find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload test results
if: failure()
uses: actions/upload-artifact@v4
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,5 @@ docs/site/
# such, it should not be committed for packages, but should be committed for
# applications that require a static environment.
#Manifest.toml

/.quarto/
10 changes: 7 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ Imports:
readr,
stringr
Remotes:
forecasttools=github::cdcgov/forecasttools
forecasttools=github::cdcgov/forecasttools,
hubUtils=github::hubverse-org/hubUtils
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
httptest2,
testthat (>= 3.0.0),
tibble,
withr,
purrr
119 changes: 119 additions & 0 deletions R/update_hub_target_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
nhsn_col_names <- list(
covid = "totalconfc19newadm",
rsv = "totalconfrsvnewadm"
)
nssp_col_names <- list(
covid = "percent_visits_covid",
rsv = "percent_visits_rsv"
)

#' Get and process target data for the hub
#'
#' This function pulls, formats and save NHSN and NSSP data for use in the hub.
#'
#' @param base_hub_path Path to the base hub directory.
#' @param disease Disease name ("covid" or "rsv").
#' @param as_of Date of the data pull. Default is today's date.
#' @param excluded_locations Vector of location codes to exclude from the output.
#' Default value is c("78", "74", "69", "66", "60"), corresponding to "US Virgin Islands",
#' "U.S. Minor Outlying Islands", "Guam", "American Samoa", and "Northern Mariana Islands".
#' @param nhsn_first_weekending_date First week-ending date to include for
#' the NHSN dataset. Default value is "2024-11-09".
#' @param legacy_file Logical. Whether to write legacy CSV output (default: FALSE).
#'
#' @return Writes `time-series.parquet` and optionally legacy csv target data files
#' to the target-data directory in the hub.
#' @export
update_hub_target_data <- function(
base_hub_path,
disease,
as_of = lubridate::today(),
nhsn_first_weekending_date = lubridate::as_date("2024-11-09"),
excluded_locations = c("78", "74", "69", "66", "60"),
legacy_file = FALSE
) {
if (!disease %in% c("covid", "rsv")) {
stop("'disease' must be either 'covid' or 'rsv'")
}
output_dirpath <- fs::path(base_hub_path, "target-data")
fs::dir_create(output_dirpath)

nhsn_col_name <- nhsn_col_names[[disease]]
nssp_col_name <- nssp_col_names[[disease]]
hubverse_ts_req_cols <- c(
"date",
"observation",
"location",
"as_of",
"target"
)
nhsn_data <- forecasttools::pull_data_cdc_gov_dataset(
dataset = "nhsn_hrd_prelim",
columns = nhsn_col_name,
start_date = nhsn_first_weekending_date
) |>
dplyr::mutate(
date = lubridate::as_date(.data$weekendingdate),
observation = as.numeric(.data[[nhsn_col_name]]),
jurisdiction = stringr::str_replace(.data$jurisdiction, "USA", "US"),
location = forecasttools::us_location_recode(
.data$jurisdiction,
"abbr",
"code"
),
as_of = !!as_of,
target = glue::glue("wk inc {disease} hosp")
) |>
dplyr::filter(!(.data$location %in% !!excluded_locations))

hubverse_format_nhsn_data <- nhsn_data |>
dplyr::select(dplyr::all_of(hubverse_ts_req_cols))

if (legacy_file) {
legacy_file_name <- glue::glue(
"{disease}-hospital-admissions.csv"
)
nhsn_data |>
dplyr::rename(
value = "observation",
state = "jurisdiction"
) |>
dplyr::arrange(state) |>
dplyr::select("state", "date", "value", "location") |>
readr::write_csv(
fs::path(output_dirpath, legacy_file_name)
)
}

hubverse_format_nssp_data <- forecasttools::pull_data_cdc_gov_dataset(
dataset = "nssp_prop_ed_visits",
columns = c(nssp_col_name, "geography"),
locations = "All"
) |>
dplyr::mutate(
date = lubridate::as_date(.data$week_end),
observation = as.numeric(.data[[nssp_col_name]]) / 100,
location = forecasttools::us_location_recode(
.data$geography,
"name",
"code"
),
as_of = !!as_of,
target = glue::glue("wk inc {disease} prop ed visits")
) |>
dplyr::select(dplyr::all_of(hubverse_ts_req_cols)) |>
dplyr::arrange(date, location)

output_file <- fs::path(output_dirpath, "time-series", ext = "parquet")
if (fs::file_exists(output_file)) {
existing_data <- forecasttools::read_tabular_file(output_file)
} else {
existing_data <- NULL
}
dplyr::bind_rows(
existing_data,
hubverse_format_nhsn_data,
hubverse_format_nssp_data
) |>
forecasttools::write_tabular_file(output_file)
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<!-- badges: start -->
[![R-CMD-check](https://github.com/CDCgov/hubhelpr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/CDCgov/hubhelpr/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->

# hubhelpr

## Overview
Expand Down
38 changes: 38 additions & 0 deletions man/update_hub_target_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(hubhelpr)

test_check("hubhelpr")
Loading
Loading