diff --git a/NEWS.md b/NEWS.md index fe5e14d22a..593039fd66 100644 --- a/NEWS.md +++ b/NEWS.md @@ -9,10 +9,12 @@ ## Breaking Changes +- - The following function arguments are entering the next phase of the [deprecation process](https://pharmaverse.github.io/admiraldev/articles/programming_strategy.html#deprecation): (#2487) (#2595) **Phase 1 (message)** - + - (placeholder - waiting for BMS63) `ignore_seconds_flag` set to `TRUE` in functions `derive_vars_dtm` and `compute_tmf` + **Phase 2 (warning)** **Phase 3 (error)** diff --git a/R/derive_vars_dtm.R b/R/derive_vars_dtm.R index 1551a17d0d..37f821595a 100644 --- a/R/derive_vars_dtm.R +++ b/R/derive_vars_dtm.R @@ -4,6 +4,9 @@ #' The date and time can be imputed (see `date_imputation`/`time_imputation` arguments) #' and the date/time imputation flag (`'--DTF'`, `'--TMF'`) can be added. #' +#' NOTE: The default value for `ignore_seconds_flag` is `TRUE` (as of v 1.4?). +#' An error will be thrown if `--DTC` contains seconds. SEE examples. +#' #' In `{admiral}` we don't allow users to pick any single part of the date/time to #' impute, we only enable to impute up to a highest level, i.e. you couldn't #' choose to say impute months, but not days. @@ -58,7 +61,6 @@ #' #' mhdt <- tribble( #' ~MHSTDTC, -#' "2019-07-18T15:25:40", #' "2019-07-18T15:25", #' "2019-07-18", #' "2019-02", @@ -74,6 +76,29 @@ #' highest_imputation = "M" #' ) #' +#' # Repeat, but with `ignore_seconds_flag = FALSE` +#' derive_vars_dtm( +#' mhdt, +#' new_vars_prefix = "AST", +#' dtc = MHSTDTC, +#' highest_imputation = "M", +#' ignore_seconds_flag = FALSE +#' ) +#' +#' # Error is thrown when data includes seconds, but `ignore_seconds_flag` +#' # is default value (TRUE) +#' mhdt <- tribble( +#' ~MHSTDTC, +#' "2019-07-18T15:25:40", +#' ) +#' try( +#' derive_vars_dtm( +#' mhdt, +#' new_vars_prefix = "AST", +#' dtc = MHSTDTC, +#' highest_imputation = "M" +#' )) +#' #' # Impute AE end date to the last date and ensure that the imputed date is not #' # after the death or data cut off date #' adae <- tribble( @@ -135,7 +160,7 @@ derive_vars_dtm <- function(dataset, # nolint: cyclocomp_linter min_dates = NULL, max_dates = NULL, preserve = FALSE, - ignore_seconds_flag = FALSE) { + ignore_seconds_flag = TRUE) { # check and quote arguments assert_character_scalar(new_vars_prefix) assert_vars(max_dates, optional = TRUE) @@ -711,7 +736,7 @@ restrict_imputed_dtc_dtm <- function(dtc, #' compute_tmf <- function(dtc, dtm, - ignore_seconds_flag = FALSE) { + ignore_seconds_flag = TRUE) { assert_date_vector(dtm) assert_character_vector(dtc) assert_logical_scalar(ignore_seconds_flag) diff --git a/man/derive_vars_dtm.Rd b/man/derive_vars_dtm.Rd index 702a408d1a..8fc579c4e6 100644 --- a/man/derive_vars_dtm.Rd +++ b/man/derive_vars_dtm.Rd @@ -15,7 +15,7 @@ derive_vars_dtm( min_dates = NULL, max_dates = NULL, preserve = FALSE, - ignore_seconds_flag = FALSE + ignore_seconds_flag = TRUE ) } \arguments{ @@ -184,7 +184,7 @@ library(lubridate) mhdt <- tribble( ~MHSTDTC, - "2019-07-18T15:25:40", + "2019-07-18T15:25", "2019-07-18T15:25", "2019-07-18", "2019-02", diff --git a/tests/testthat/_snaps/derive_vars_dtm.md b/tests/testthat/_snaps/derive_vars_dtm.md index 55b5807c76..a92b9e32cb 100644 --- a/tests/testthat/_snaps/derive_vars_dtm.md +++ b/tests/testthat/_snaps/derive_vars_dtm.md @@ -10,8 +10,8 @@ # derive_vars_dtm Test 22: No re-derivation is done if --DTF variable already exists Code - actual_output <- derive_vars_dtm(mutate(input, ASTDTF = c(NA, NA, NA, NA, "D", - "MD", "M")), new_vars_prefix = "AST", dtc = XXSTDTC, highest_imputation = "M", + actual_output <- derive_vars_dtm(mutate(input, ASTDTF = c(NA, NA, NA, "D", "MD", + "M")), new_vars_prefix = "AST", dtc = XXSTDTC, highest_imputation = "M", date_imputation = "first") Message The `ASTDTF` variable is already present in the input dataset and will not be re-derived. @@ -64,7 +64,14 @@ Code derive_vars_dtm(input, new_vars_prefix = "AST", dtc = XXSTDTC, ignore_seconds_flag = TRUE) - Condition - Error in `derive_vars_dtm()`: - ! Seconds detected in data while `ignore_seconds_flag` is invoked + Output + # A tibble: 6 x 3 + XXSTDTC ASTDTM ASTTMF + + 1 2019-07-18T15:25 2019-07-18 15:25:00 + 2 2019-07-18T15 2019-07-18 15:00:00 M + 3 2019-07-18 2019-07-18 00:00:00 H + 4 2019-02 NA + 5 2019 NA + 6 2019---07 NA diff --git a/tests/testthat/test-derive_vars_dtm.R b/tests/testthat/test-derive_vars_dtm.R index ba811e93f3..377e3d328b 100644 --- a/tests/testthat/test-derive_vars_dtm.R +++ b/tests/testthat/test-derive_vars_dtm.R @@ -345,7 +345,8 @@ test_that("compute_tmf Test 14: compute TMF", { expect_equal( compute_tmf( dtc = input_dtc, - dtm = input_dtm + dtm = input_dtm, + ignore_seconds_flag = FALSE ), expected_output ) @@ -383,7 +384,6 @@ test_that("compute_tmf Test 16: ignore_seconds_flag = TRUE", { input <- tibble::tribble( ~XXSTDTC, - "2019-07-18T15:25:40", "2019-07-18T15:25", "2019-07-18T15", "2019-07-18", @@ -396,8 +396,7 @@ input <- tibble::tribble( test_that("derive_vars_dtm Test 17: default behavior", { expected_output <- tibble::tribble( ~XXSTDTC, ~ASTDTM, ~ASTTMF, - "2019-07-18T15:25:40", ymd_hms("2019-07-18T15:25:40"), NA_character_, - "2019-07-18T15:25", ymd_hms("2019-07-18T15:25:00"), "S", + "2019-07-18T15:25", ymd_hms("2019-07-18T15:25:00"), NA, "2019-07-18T15", ymd_hms("2019-07-18T15:00:00"), "M", "2019-07-18", ymd_hms("2019-07-18T00:00:00"), "H", "2019-02", ymd_hms(NA), NA_character_, @@ -422,8 +421,7 @@ test_that("derive_vars_dtm Test 17: default behavior", { test_that("derive_vars_dtm Test 18: date imputed to first, auto DTF/TMF", { expected_output <- tibble::tribble( ~XXSTDTC, ~ASTDTM, ~ASTDTF, ~ASTTMF, - "2019-07-18T15:25:40", ymd_hms("2019-07-18T15:25:40"), NA_character_, NA_character_, - "2019-07-18T15:25", ymd_hms("2019-07-18T15:25:00"), NA_character_, "S", + "2019-07-18T15:25", ymd_hms("2019-07-18T15:25:00"), NA_character_, NA, "2019-07-18T15", ymd_hms("2019-07-18T15:00:00"), NA_character_, "M", "2019-07-18", ymd_hms("2019-07-18T00:00:00"), NA_character_, "H", "2019-02", ymd_hms("2019-02-01T00:00:00"), "D", "H", @@ -450,7 +448,6 @@ test_that("derive_vars_dtm Test 18: date imputed to first, auto DTF/TMF", { test_that("derive_vars_dtm Test 19: date and time imputed to last, no DTF/TMF", { expected_output <- tibble::tribble( ~XXSTDTC, ~AENDTM, - "2019-07-18T15:25:40", ymd_hms("2019-07-18T15:25:40"), "2019-07-18T15:25", ymd_hms("2019-07-18T15:25:59"), "2019-07-18T15", ymd_hms("2019-07-18T15:59:59"), "2019-07-18", ymd_hms("2019-07-18T23:59:59"), @@ -480,7 +477,6 @@ test_that("derive_vars_dtm Test 19: date and time imputed to last, no DTF/TMF", test_that("derive_vars_dtm Test 20: date and time imputed to last, DTF only", { expected_output <- tibble::tribble( ~XXSTDTC, ~AENDTM, ~AENDTF, - "2019-07-18T15:25:40", ymd_hms("2019-07-18T15:25:40"), NA_character_, "2019-07-18T15:25", ymd_hms("2019-07-18T15:25:59"), NA_character_, "2019-07-18T15", ymd_hms("2019-07-18T15:59:59"), NA_character_, "2019-07-18", ymd_hms("2019-07-18T23:59:59"), NA_character_, @@ -510,8 +506,7 @@ test_that("derive_vars_dtm Test 20: date and time imputed to last, DTF only", { test_that("derive_vars_dtm Test 21: date imputed to MID, time to first, TMF only", { expected_output <- tibble::tribble( ~XXSTDTC, ~ASTDTM, ~ASTTMF, - "2019-07-18T15:25:40", ymd_hms("2019-07-18T15:25:40"), NA_character_, - "2019-07-18T15:25", ymd_hms("2019-07-18T15:25:00"), "S", + "2019-07-18T15:25", ymd_hms("2019-07-18T15:25:00"), NA, "2019-07-18T15", ymd_hms("2019-07-18T15:00:00"), "M", "2019-07-18", ymd_hms("2019-07-18T00:00:00"), "H", "2019-02", ymd_hms("2019-02-15T00:00:00"), "H", @@ -540,8 +535,7 @@ test_that("derive_vars_dtm Test 21: date imputed to MID, time to first, TMF only test_that("derive_vars_dtm Test 22: No re-derivation is done if --DTF variable already exists", { expected_output <- tibble::tribble( ~XXSTDTC, ~ASTDTM, ~ASTDTF, ~ASTTMF, - "2019-07-18T15:25:40", ymd_hms("2019-07-18T15:25:40"), NA_character_, NA_character_, - "2019-07-18T15:25", ymd_hms("2019-07-18T15:25:00"), NA_character_, "S", + "2019-07-18T15:25", ymd_hms("2019-07-18T15:25:00"), NA_character_, NA, "2019-07-18T15", ymd_hms("2019-07-18T15:00:00"), NA_character_, "M", "2019-07-18", ymd_hms("2019-07-18T00:00:00"), NA_character_, "H", "2019-02", ymd_hms("2019-02-01T00:00:00"), "D", "H", @@ -552,7 +546,7 @@ test_that("derive_vars_dtm Test 22: No re-derivation is done if --DTF variable a expect_snapshot( actual_output <- derive_vars_dtm( - mutate(input, ASTDTF = c(NA, NA, NA, NA, "D", "MD", "M")), + mutate(input, ASTDTF = c(NA, NA, NA, "D", "MD", "M")), new_vars_prefix = "AST", dtc = XXSTDTC, highest_imputation = "M", @@ -781,13 +775,22 @@ test_that("derive_vars_dtm Test 30: Supplying both min/max dates for highest_imp }) ## Test 31: catch ignore_seconds_flag error ---- + +input <- tibble::tribble( + ~XXSTDTC, + "2019-07-18T15:25:40", + "2019-07-18T15", + "2019-07-18", + "2019-02", + "2019", + "2019---07" +) test_that("derive_vars_dtm Test 31: catch ignore_seconds_flag error", { expect_snapshot( derive_vars_dtm( input, new_vars_prefix = "AST", dtc = XXSTDTC, - ignore_seconds_flag = TRUE ), error = TRUE ) diff --git a/vignettes/imputation.Rmd b/vignettes/imputation.Rmd index 8042bc837f..58d444e865 100644 --- a/vignettes/imputation.Rmd +++ b/vignettes/imputation.Rmd @@ -312,7 +312,7 @@ imputation flags are valid for both the datetime and the date variable. ```{r} ae <- tribble( ~AESTDTC, - "2019-08-09T12:34:56", + "2019-08-09T12:34", "2019-04-12", "2010-09", NA_character_ @@ -363,7 +363,7 @@ no date is imputed the date imputation flag is not created. ```{r} ae <- tribble( ~AESTDTC, - "2019-08-09T12:34:56", + "2019-08-09T12:34", "2019-04-12", "2010-09", NA_character_ @@ -406,7 +406,8 @@ ae <- tribble( highest_imputation = "M", date_imputation = "first", time_imputation = "first", - min_dates = exprs(TRTSDTM) + min_dates = exprs(TRTSDTM), + ignore_seconds_flag = FALSE ) ``` ```{r, echo=FALSE} @@ -438,7 +439,8 @@ ae <- tribble( highest_imputation = "M", date_imputation = "last", time_imputation = "last", - max_dates = exprs(DTHDT, DCUTDT) + max_dates = exprs(DTHDT, DCUTDT), + ignore_seconds_flag = FALSE ) ``` ```{r, echo=FALSE} @@ -488,7 +490,8 @@ vs <- tribble( derivation = derive_vars_dtm, args = params( dtc = VSDTC, - new_vars_prefix = "A" + new_vars_prefix = "A", + ignore_seconds_flag = FALSE ), derivation_slice( filter = VSTPT == "PRE-DOSE", diff --git a/vignettes/pk_adnca.Rmd b/vignettes/pk_adnca.Rmd index 7f0f7d8b3e..a9493728cc 100644 --- a/vignettes/pk_adnca.Rmd +++ b/vignettes/pk_adnca.Rmd @@ -172,7 +172,8 @@ pc_dates <- pc %>% derive_vars_dtm( new_vars_prefix = "A", dtc = PCDTC, - time_imputation = "00:00:00" + time_imputation = "00:00:00", + ignore_seconds_flag = FALSE ) %>% # Derive dates and times from date/times derive_vars_dtm_to_dt(exprs(ADTM)) %>%