From c707bbbbc6e2afd2b7dcb84569d8bfac302ad74b Mon Sep 17 00:00:00 2001 From: Daniel Sjoberg Date: Tue, 25 Jul 2023 08:01:38 -0700 Subject: [PATCH] #1984 allowing missing trt end date --- R/derive_var_ontrtfl.R | 4 +-- tests/testthat/_snaps/derive_var_ontrtfl.md | 22 +++++++++++++ tests/testthat/test-derive_var_ontrtfl.R | 35 +++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/_snaps/derive_var_ontrtfl.md diff --git a/R/derive_var_ontrtfl.R b/R/derive_var_ontrtfl.R index 6c3696d1d9..c539e613c3 100644 --- a/R/derive_var_ontrtfl.R +++ b/R/derive_var_ontrtfl.R @@ -236,9 +236,9 @@ derive_var_ontrtfl <- function(dataset, } else { # Scenario 2: Treatment end date is passed, window added above if (ignore_time_for_ref_end_date) { - end_cond <- expr(date(!!start_date) <= date(!!ref_end_date) + days(!!ref_end_window)) + end_cond <- expr((date(!!start_date) <= date(!!ref_end_date) + days(!!ref_end_window)) | is.na(!!ref_end_date)) } else { - end_cond <- expr(!!start_date <= !!ref_end_date + days(!!ref_end_window)) + end_cond <- expr((!!start_date <= !!ref_end_date + days(!!ref_end_window)) | is.na(!!ref_end_date)) } dataset <- mutate( dataset, diff --git a/tests/testthat/_snaps/derive_var_ontrtfl.md b/tests/testthat/_snaps/derive_var_ontrtfl.md new file mode 100644 index 0000000000..5704854a1d --- /dev/null +++ b/tests/testthat/_snaps/derive_var_ontrtfl.md @@ -0,0 +1,22 @@ +# derive_var_ontrtfl Test 15: if trt end date is missing, the obs may still be flagged + + Code + derive_var_ontrtfl(adcm, start_date = ASTDT, end_date = AENDT, ref_start_date = TRTSDT, + ref_end_date = TRTEDT, span_period = "Y") + Output + USUBJID ASTDT TRTSDT TRTEDT AENDT ONTRTFL + 1 P01 2018-03-15 2019-01-01 NA 2022-12-01 Y + 2 P02 2020-04-30 2019-01-01 NA 2022-03-15 Y + 3 P03 2020-04-30 2019-01-01 NA Y + +--- + + Code + derive_var_ontrtfl(adcm, start_date = ASTDT, end_date = AENDT, ref_start_date = TRTSDT, + ref_end_date = TRTEDT) + Output + USUBJID ASTDT TRTSDT TRTEDT AENDT ONTRTFL + 1 P01 2018-03-15 2019-01-01 NA 2022-12-01 + 2 P02 2020-04-30 2019-01-01 NA 2022-03-15 Y + 3 P03 2020-04-30 2019-01-01 NA Y + diff --git a/tests/testthat/test-derive_var_ontrtfl.R b/tests/testthat/test-derive_var_ontrtfl.R index 324e8915bc..55c5434df9 100644 --- a/tests/testthat/test-derive_var_ontrtfl.R +++ b/tests/testthat/test-derive_var_ontrtfl.R @@ -402,3 +402,38 @@ test_that("derive_var_ontrtfl Test 14: start_date < ref_start_date and end_date keys = c("STUDYID", "USUBJID", "ASTDT") ) }) + + +## Test 15: if trt end date is missing, the obs may still be flagged ---- +test_that("derive_var_ontrtfl Test 15: if trt end date is missing, the obs may still be flagged", { # nolint + adcm <- tibble::tribble( + ~USUBJID, ~ASTDT, ~TRTSDT, ~TRTEDT, ~AENDT, + "P01", ymd("2018-03-15"), ymd("2019-01-01"), NA, ymd("2022-12-01"), + "P02", ymd("2020-04-30"), ymd("2019-01-01"), NA, ymd("2022-03-15"), + "P03", ymd("2020-04-30"), ymd("2019-01-01"), NA, NA, + ) %>% + as.data.frame() + + # all flags should be "Y" because span_period flag is "Y" + expect_snapshot( + derive_var_ontrtfl( + adcm, + start_date = ASTDT, + end_date = AENDT, + ref_start_date = TRTSDT, + ref_end_date = TRTEDT, + span_period = "Y" + ) + ) + + # first obs started before treatment, and it should NOT be flagged + expect_snapshot( + derive_var_ontrtfl( + adcm, + start_date = ASTDT, + end_date = AENDT, + ref_start_date = TRTSDT, + ref_end_date = TRTEDT + ) + ) +})