Skip to content
Open
Show file tree
Hide file tree
Changes from all 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: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
^.github$
^.githooks$
^.pre-commit-config.yaml
^tools$
^cran-plan\.md$
9 changes: 6 additions & 3 deletions .github/workflows/PR_cleanup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ jobs:
delete_pr_artifacts:
runs-on: ubuntu-latest
steps:
- uses: stefanluptak/delete-old-pr-artifacts@v1
with:
workflow_filename: Check.yaml
- uses: actions/checkout@v3
- name: Delete PR artifacts
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh cache list -R ${{ github.repository }} --json id --jq '.[] | .id' | xargs -I {} gh cache delete -R ${{ github.repository }} {}
40 changes: 20 additions & 20 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: chef
Title: Framework for generating statistical evidence
Title: Framework for Generating Statistical Evidence
Version: 0.1.1
Authors@R: c(
person("MEWP (Matthew David Phelps)", , , "mewp@novonordisk.com", role = "aut"),
Expand All @@ -8,51 +8,51 @@ Authors@R: c(
person("CINO (Christian Haargaard Olsen)", , , "cino@novonordisk.com", role = c("aut", "cre"))
)
Description: Take endpoint recipes and transform them into statistical
outputs.
outputs. This package provides a framework for defining clinical trial
endpoints and generating statistical analyses using the {targets} pipeline
system, enabling reproducible and automated evidence generation workflows.
It is particularly useful for healthcare technology assessments and
regulatory submissions.
License: MIT + file LICENSE
BugReports: https://github.com/hta-pharma/chef/issues
Depends:
targets(>= 1.3.2)
Imports:
R (>= 4.1.0),
targets (>= 1.3.2)
Imports:
checkmate,
cli,
data.table (>= 1.14.2),
digest,
future,
future.callr,
magrittr,
methods,
qs,
rlang,
stringr,
tarchetypes,
usethis,
purrr,
stats,
stats
Suggests:
covr,
dplyr,
fs,
future,
future.callr,
glue,
htmltools (>= 0.5.4),
kableExtra,
knitr,
pharmaverseadam,
plyr,
dplyr,
glue,
pryr,
rmarkdown,
testr,
tarchetypes,
testthat (>= 3.0.0),
tidyr,
withr,
whisker
VignetteBuilder:
VignetteBuilder:
knitr
Remotes:
matthew-phelps/testr
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
URL:
https://hta-pharma.github.io/chef/,
https://github.com/hta-pharma/chef
RoxygenNote: 7.3.3
URL: https://hta-pharma.github.io/chef/,
https://github.com/hta-pharma/chef
11 changes: 2 additions & 9 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
MIT License

Copyright (c) 2024 Novo Nordisk A/S, Danish company registration no. 24256790

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
YEAR: 2024
COPYRIGHT HOLDER: Novo Nordisk A/S
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# chef 0.1.1

* Added comprehensive examples to all exported functions for better documentation and CRAN compliance.
* Removed dependency on {testr} package and replaced with base {testthat}/{withr} equivalents for better CRAN compatibility.
* Improved test robustness by ensuring that changes to adam functions only invalidate targets that use those functions.
* Fixed Windows path handling issues.
* Updated test snapshots to use snapshot values.
* Moved {qs}, {future}, {future.callr}, and {tarchetypes} from Imports to Suggests in DESCRIPTION.
* Added BugReports field to DESCRIPTION and improved package metadata.

# chef 0.1.0

* Initial release.
35 changes: 35 additions & 0 deletions R/add_event_index.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,41 @@ create_flag <- function(dat, var_value_pairs = NULL, singletons = NULL) {
#' `INDEX_` column in the clinical data. This `INDEX_` column is created by
#' chef when a user supplies a clinical dataset.
#' @export
#'
#' @examples
#' library(data.table)
#' library(pharmaverseadam)
#'
#' # Prepare clinical data with INDEX_ column
#' adcm_data <- as.data.table(pharmaverseadam::adcm)
#' adcm_data[, INDEX_ := .I]
#'
#' analysis_data_container <- data.table(
#' dat = list(adcm_data),
#' key_analysis_data = "a"
#' )
#' setkey(analysis_data_container, key_analysis_data)
#'
#' # Create endpoint with specific filters
#' ep <- data.table(
#' endpoint_id = 1L,
#' pop_var = "SAFFL",
#' pop_value = "Y",
#' period_var = NA_character_,
#' period_value = NA_character_,
#' endpoint_filter = NA_character_,
#' endpoint_group_filter = NA_character_,
#' custom_pop_filter = NA_character_,
#' key_analysis_data = "a"
#' )
#' setkey(ep, key_analysis_data)
#'
#' # Add event index: identifies which rows match endpoint criteria
#' ep_with_index <- add_event_index(ep, analysis_data_container)
#'
#' # event_index contains row numbers from adcm_data matching the criteria
#' str(ep_with_index$event_index) # integer vector of INDEX_ values
#' length(ep_with_index$event_index[[1]]) # e.g., 47 events match SAFFL="Y"

add_event_index <- function(ep, analysis_data_container) {
event_index <-
Expand Down
26 changes: 25 additions & 1 deletion R/add_id.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
#' Add ID to user-defined endpoint groups
#'
#' Adds a sequential identifier (`endpoint_spec_id`) to each row of an endpoint
#' definition table. These IDs track endpoint specifications through the analysis
#' pipeline and enable mapping of results back to original endpoint definitions.
#'
#' @param ep A `data.table` containing endpoint definitions.
#'
#' @return data.table
#' @return data.table with an additional `endpoint_spec_id` column containing
#' sequential integers starting from 1.
#'
#' @export
#'
#' @examples
#' library(data.table)
#'
#' # Typical endpoint definition table from chef workflows
#' endpoints <- data.table(
#' endpoint_label = c("Primary Efficacy", "Safety", "Tolerability"),
#' analysis_type = c("efficacy", "adverse_event", "adverse_event"),
#' custom_pop_filter = c("AGE >= 18", "SAFFL=='Y'", "SAFFL=='Y'")
#' )
#'
#' # Add sequential IDs for tracking through analysis pipeline
#' endpoints_with_ids <- add_id(endpoints)
#'
#' endpoints_with_ids
#'
#' # The endpoint_spec_id column now uniquely identifies each endpoint
#' # and can be used to map statistical results back to endpoint definitions
add_id <- function(ep){
endpoint_spec_id <- NULL
x <- copy(ep)
Expand Down
109 changes: 109 additions & 0 deletions R/apply_criterion.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,57 @@
#' @return A `data.table` with an additional logical column `crit_accept_endpoint`
#' indicating whether each endpoint meets the defined criteria.
#' @export
#'
#' @examples
#' library(data.table)
#' library(pharmaverseadam)
#'
#' # Create a simple criterion function: keep endpoint if n events >= 5
#' min_events_fn <- function(dat, event_index, ...) {
#' length(event_index) >= 5
#' }
#'
#' # Prepare endpoint with event index
#' adcm_data <- as.data.table(pharmaverseadam::adcm)
#' adcm_data[, INDEX_ := .I]
#'
#' analysis_data_container <- data.table(
#' dat = list(adcm_data),
#' key_analysis_data = "a"
#' )
#' setkey(analysis_data_container, key_analysis_data)
#'
#' ep <- data.table(
#' endpoint_id = "1-0001",
#' endpoint_spec_id = 1L,
#' pop_var = "SAFFL",
#' pop_value = "Y",
#' period_var = NA_character_,
#' period_value = NA_character_,
#' endpoint_filter = NA_character_,
#' endpoint_group_filter = NA_character_,
#' custom_pop_filter = NA_character_,
#' stratify_by = list(),
#' treatment_var = "TRT01A",
#' treatment_refval = "Xanomeline High Dose",
#' event_index = list(1:20), # Simulated event indices
#' key_analysis_data = "a"
#' )
#' setkey(ep, key_analysis_data)
#'
#' # Create function map linking endpoint to criterion
#' fn_map <- data.table(
#' endpoint_spec_id = 1L,
#' fn_type = "crit_endpoint",
#' fn_callable = list(min_events_fn),
#' fn_name = "min_events"
#' )
#'
#' # Apply endpoint criterion
#' ep_filtered <- apply_criterion_endpoint(ep, analysis_data_container, fn_map)
#'
#' # Result: crit_accept_endpoint = TRUE (20 events >= 5)
#' ep_filtered[, .(endpoint_id, crit_accept_endpoint)]
apply_criterion_endpoint <- function(ep, analysis_data_container, fn_map) {
fn_type <-
crit_accept_endpoint <-
Expand Down Expand Up @@ -83,6 +134,64 @@ apply_criterion_endpoint <- function(ep, analysis_data_container, fn_map) {
#' @return A `data.table` with one row per stratum for each endpoint, with an
#' additional logical column indicating whether each row meets the criteria.
#' @export
#'
#' @examples
#' library(data.table)
#' library(pharmaverseadam)
#'
#' # Create endpoint with stratification that already passed endpoint criteria
#' ep <- data.table(
#' endpoint_id = "1-0001",
#' endpoint_spec_id = 1L,
#' crit_accept_endpoint = TRUE,
#' stratify_by = list(c("SEX")),
#' strata_var = c("M", "F"), # Two strata plus TOTAL
#' event_index = list(1:20),
#' treatment_var = "TRT01A",
#' treatment_refval = "Xanomeline High Dose",
#' endpoint_filter = NA_character_,
#' endpoint_group_filter = NA_character_,
#' endpoint_group_metadata = list(),
#' custom_pop_filter = NA_character_,
#' period_var = NA_character_,
#' period_value = NA_character_,
#' key_analysis_data = "a"
#' )
#'
#' # Prepare data container
#' adcm_data <- as.data.table(pharmaverseadam::adcm)
#' adcm_data[, INDEX_ := .I]
#'
#' analysis_data_container <- data.table(
#' dat = list(adcm_data),
#' key_analysis_data = "a"
#' )
#' setkey(analysis_data_container, key_analysis_data)
#' setkey(ep, key_analysis_data)
#'
#' # Create strata-level criterion: keep strata if >= 3 subjects
#' min_subgroup_size <- function(dat, event_index, stratify_by, strata_var, ...) {
#' # This is a simplified example; actual implementation evaluates actual data
#' length(event_index) >= 3
#' }
#'
#' fn_map <- data.table(
#' endpoint_spec_id = 1L,
#' fn_type = "crit_by_strata_by_trt",
#' fn_callable = list(min_subgroup_size),
#' fn_name = "min_subgroup_size"
#' )
#'
#' # Apply strata criteria
#' ep_strata <- apply_criterion_by_strata(
#' ep = ep,
#' analysis_data_container = analysis_data_container,
#' fn_map = fn_map,
#' type = "by_strata_by_trt"
#' )
#'
#' # Result: multiple rows per endpoint (one per stratum)
#' ep_strata[, .(endpoint_id, strata_var, crit_accept_by_strata_by_trt)]
apply_criterion_by_strata <-
function(ep,
analysis_data_container,
Expand Down
44 changes: 42 additions & 2 deletions R/check_duplicate_functions.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
#' Check for duplicate function definitions
#'
#' Scans a directory for R files and identifies any function definitions that
#' appear multiple times (by name). This is useful in endpoint analysis pipelines
#' to prevent accidental function redefinitions that could cause unexpected
#' behavior. The check is case-sensitive (i.e., `func_name` and `Func_name` are
#' considered distinct).
#'
#' @param dir The directory where the custom functions are defined
#'
#' @return run for side-effects, if multiple functions are encountered it throws
#' an error
#' @return Run for side-effects. Returns `NULL` invisibly if no duplicates are
#' found. If duplicate functions are detected, an error is thrown with details
#' about which functions are duplicated.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' # Create a temporary directory for demonstration
#' tmp_dir <- tempdir()
#'
#' # Valid case: no duplicate function names
#' writeLines("
#' compute_n_subjects <- function(dat, ...) {
#' nrow(dat)
#' }
#' ", file.path(tmp_dir, "stat_functions_1.R"))
#'
#' writeLines("
#' compute_event_rate <- function(dat, ...) {
#' sum(dat$is_event) / nrow(dat)
#' }
#' ", file.path(tmp_dir, "stat_functions_2.R"))
#'
#' # Check passes - no duplicates found
#' check_duplicate_functions(tmp_dir)
#'
#' # Invalid case: duplicate function names cause error
#' writeLines("
#' compute_n_subjects <- function(dat, ...) {
#' sum(!is.na(dat$id)) # Different implementation
#' }
#' ", file.path(tmp_dir, "stat_functions_2.R"))
#'
#' # This will error with message about "compute_n_subjects" being duplicated
#' check_duplicate_functions(tmp_dir)
#' }
#'
check_duplicate_functions <- function(dir) {
if (!dir.exists(dir)) {
stop(paste0("Directory ", dir, " does not exist"))
Expand Down
Loading
Loading