diff --git a/NEWS.md b/NEWS.md index 2603c9dab8..cdb5e9b036 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,8 @@ ## Updates of Existing Functions +- `derive_extreme_records()`, `derive_var_extreme_flag()`,`derive_vars_joined()` and `derive_vars_merged()` were enhanced with the arguments `true_value` and `false_value` to align with preexisting functions that had similar functionality (#2125) + - `restrict_derivation()` now allows `{dplyr}` functions like `mutate` in the `derivation argument (#2143) - `derive_summary_records()`, `derive_var_merged_summary()`, and `get_summary_records()` @@ -22,6 +24,10 @@ were enhanced such that more than one summary variable can be derived, e.g., - admiral now only supports R >= 4.0.0 +- For the function `derive_vars_merged()`, the argument `match_flag` was renamed to `exist_flag` (#2125) + +- The default value for the `false_value` argument in `derive_extreme_records()` was changed to `NA_character_` (#2125) + - The following functions, which were deprecated in previous `{admiral}` versions, have been removed: (#2098) - `derive_param_extreme_event()` - `derive_vars_last_dose()` diff --git a/R/derive_extreme_records.R b/R/derive_extreme_records.R index 981e3d96ea..f63698d17e 100644 --- a/R/derive_extreme_records.R +++ b/R/derive_extreme_records.R @@ -263,7 +263,7 @@ derive_extreme_records <- function(dataset = NULL, check_type = "warning", exist_flag = NULL, true_value = "Y", - false_value = "N", + false_value = NA_character_, keep_source_vars = exprs(everything()), set_values_to) { # Check input arguments diff --git a/R/derive_joined.R b/R/derive_joined.R index 586f256360..f166e05031 100644 --- a/R/derive_joined.R +++ b/R/derive_joined.R @@ -329,6 +329,9 @@ derive_vars_joined <- function(dataset, filter_add = NULL, filter_join = NULL, mode = NULL, + exist_flag = NULL, + true_value = "Y", + false_value = NA_character_, missing_values = NULL, check_type = "warning") { assert_vars(by_vars, optional = TRUE) @@ -423,6 +426,9 @@ derive_vars_joined <- function(dataset, new_vars = add_suffix_to_vars(new_vars, vars = common_vars, suffix = ".join"), missing_values = missing_values, check_type = check_type, + exist_flag = !!enexpr(exist_flag), + true_value = true_value, + false_value = false_value, duplicate_msg = paste( paste( "After applying `filter_join` the joined dataset contains more", diff --git a/R/derive_merged.R b/R/derive_merged.R index 4ecc6d8562..0bd7e9b279 100644 --- a/R/derive_merged.R +++ b/R/derive_merged.R @@ -81,6 +81,8 @@ #' #' @param match_flag Match flag #' +#' `r lifecycle::badge("deprecated")` Please use `exist_flag` instead. +#' #' If the argument is specified (e.g., `match_flag = FLAG`), the specified #' variable (e.g., `FLAG`) is added to the input dataset. This variable will #' be `TRUE` for all selected records from `dataset_add` which are merged into @@ -88,6 +90,29 @@ #' #' *Permitted Values*: Variable name #' +#' @param exist_flag Exist flag +#' +#' If the argument is specified (e.g., `exist_flag = FLAG`), the specified +#' variable (e.g., `FLAG`) is added to the input dataset. This variable will +#' be the value provided in `true_value` for all selected records from `dataset_add` +#' which are merged into the input dataset, and the value provided in `false_value` otherwise. +#' +#' *Permitted Values*: Variable name +#' +#' @param true_value True value +#' +#' The value for the specified variable `exist_flag`, applicable to +#' the first or last observation (depending on the mode) of each by group. +#' +#' Permitted Values: An atomic scalar +#' +#' @param false_value False value +#' +#' The value for the specified variable `exist_flag`, NOT applicable to +#' the first or last observation (depending on the mode) of each by group. +#' +#' Permitted Values: An atomic scalar +#' #' @param missing_values Values for non-matching observations #' #' For observations of the input dataset (`dataset`) which do not have a @@ -198,7 +223,7 @@ #' mode = "last", #' new_vars = exprs(LASTWGT = VSSTRESN, LASTWGTU = VSSTRESU), #' filter_add = VSTESTCD == "WEIGHT", -#' match_flag = vsdatafl +#' exist_flag = vsdatafl #' ) %>% #' select(STUDYID, USUBJID, AGE, AGEU, LASTWGT, LASTWGTU, vsdatafl) #' @@ -282,7 +307,10 @@ derive_vars_merged <- function(dataset, new_vars = NULL, filter_add = NULL, mode = NULL, - match_flag = NULL, + match_flag, + exist_flag = NULL, + true_value = "Y", + false_value = NA_character_, missing_values = NULL, check_type = "warning", duplicate_msg = NULL) { @@ -301,7 +329,17 @@ derive_vars_merged <- function(dataset, extract_vars(new_vars) ) ) - match_flag <- assert_symbol(enexpr(match_flag), optional = TRUE) + if (!is_missing(enexpr(match_flag))) { + deprecate_warn( + "1.0.0", + "derive_vars_merged(match_flag =)", + "derive_vars_merged(exist_flag =)" + ) + exist_flag <- assert_symbol(enexpr(match_flag), optional = TRUE) + } + exist_flag <- assert_symbol(enexpr(exist_flag), optional = TRUE) + assert_atomic_vector(true_value, optional = TRUE) + assert_atomic_vector(false_value, optional = TRUE) assert_expr_list(missing_values, named = TRUE, optional = TRUE) if (!is.null(missing_values)) { invalid_vars <- setdiff( @@ -347,18 +385,18 @@ derive_vars_merged <- function(dataset, select(!!!by_vars_right, !!!replace_values_by_names(new_vars)) } - if (!is.null(missing_values)) { + if (!is.null(missing_values) || !is.null(exist_flag)) { match_flag_var <- get_new_tmp_var(add_data, prefix = "tmp_match_flag") } else { - match_flag_var <- match_flag + match_flag_var <- NULL } - if (!is.null(match_flag_var)) { add_data <- mutate( add_data, !!match_flag_var := TRUE ) } + # check if there are any variables in both datasets which are not by vars # in this case an error is issued to avoid renaming of varibles by left_join() common_vars <- @@ -382,7 +420,7 @@ derive_vars_merged <- function(dataset, } dataset <- left_join(dataset, add_data, by = vars2chr(by_vars)) - if (!is.null(missing_values)) { + if (!is.null(match_flag_var)) { update_missings <- map2( syms(names(missing_values)), missing_values, @@ -390,10 +428,16 @@ derive_vars_merged <- function(dataset, ) names(update_missings) <- names(missing_values) dataset <- dataset %>% - mutate(!!!update_missings) %>% - remove_tmp_vars() + mutate(!!!update_missings) } - dataset + + if (!is.null(exist_flag)) { + dataset <- dataset %>% + mutate(!!exist_flag := ifelse(is.na(!!match_flag_var), false_value, true_value)) + } + + dataset %>% + remove_tmp_vars() } #' Merge an Existence Flag @@ -651,6 +695,8 @@ derive_vars_merged_lookup <- function(dataset, assert_logical_scalar(print_not_mapped) filter_add <- assert_filter_cond(enexpr(filter_add), optional = TRUE) + tmp_lookup_flag <- get_new_tmp_var(dataset_add, prefix = "tmp_lookup_flag") + res <- derive_vars_merged( dataset, dataset_add, @@ -659,14 +705,14 @@ derive_vars_merged_lookup <- function(dataset, new_vars = new_vars, mode = mode, filter_add = !!filter_add, - match_flag = temp_match_flag, + exist_flag = !!tmp_lookup_flag, check_type = check_type, duplicate_msg = duplicate_msg ) if (print_not_mapped) { temp_not_mapped <- res %>% - filter(is.na(temp_match_flag)) %>% + filter(is.na(!!tmp_lookup_flag)) %>% distinct(!!!by_vars_left) if (nrow(temp_not_mapped) > 0) { @@ -688,7 +734,7 @@ derive_vars_merged_lookup <- function(dataset, } } - res %>% select(-temp_match_flag) + res %>% remove_tmp_vars() } #' Get list of records not mapped from the lookup table. diff --git a/R/derive_var_extreme_flag.R b/R/derive_var_extreme_flag.R index 120e8e4e47..f5c52f658c 100644 --- a/R/derive_var_extreme_flag.R +++ b/R/derive_var_extreme_flag.R @@ -14,8 +14,8 @@ #' #' @param new_var Variable to add #' -#' The specified variable is added to the output dataset. It is set to `"Y"` -#' for the first or last observation (depending on the mode) of each by group. +#' The specified variable is added to the output dataset. It is set to the value +#' set in `true_value` for the first or last observation (depending on the mode) of each by group. #' #' Permitted Values: list of name-value pairs #' @@ -25,6 +25,20 @@ #' #' Permitted Values: `"first"`, `"last"` #' +#' @param true_value True value +#' +#' The value for the specified variable `new_var`, applicable to +#' the first or last observation (depending on the mode) of each by group. +#' +#' Permitted Values: An atomic scalar +#' +#' @param false_value False value +#' +#' The value for the specified variable `new_var`, NOT applicable to +#' the first or last observation (depending on the mode) of each by group. +#' +#' Permitted Values: An atomic scalar +#' #' @param flag_all Flag setting #' #' A logical value where if set to `TRUE`, all records are flagged @@ -232,6 +246,8 @@ derive_var_extreme_flag <- function(dataset, order, new_var, mode, + true_value = "Y", + false_value = NA_character_, flag_all = FALSE, check_type = "warning") { new_var <- assert_symbol(enexpr(new_var)) @@ -239,6 +255,8 @@ derive_var_extreme_flag <- function(dataset, assert_expr_list(order) assert_data_frame(dataset, required_vars = exprs(!!!by_vars, !!!extract_vars(order))) mode <- assert_character_scalar(mode, values = c("first", "last"), case_sensitive = FALSE) + assert_atomic_vector(true_value, optional = TRUE) + assert_atomic_vector(false_value, optional = TRUE) flag_all <- assert_logical_scalar(flag_all) check_type <- assert_character_scalar( check_type, @@ -263,11 +281,11 @@ derive_var_extreme_flag <- function(dataset, if (mode == "first") { data <- data %>% - mutate(!!new_var := if_else(!!tmp_obs_nr == 1, "Y", NA_character_)) + mutate(!!new_var := if_else(!!tmp_obs_nr == 1, true_value, false_value)) } else { data <- data %>% group_by(!!!by_vars) %>% - mutate(!!new_var := if_else(!!tmp_obs_nr == n(), "Y", NA_character_)) %>% + mutate(!!new_var := if_else(!!tmp_obs_nr == n(), true_value, false_value)) %>% ungroup() } diff --git a/R/globals.R b/R/globals.R index d1e24ad67a..7a4855e8d2 100644 --- a/R/globals.R +++ b/R/globals.R @@ -104,7 +104,6 @@ globalVariables(c( "temp_DT", "temp_from_var", "temp_to_var", - "temp_match_flag", "temp_dose_freq", "temp_new_dose_no", "temp_num_of_doses", diff --git a/man/derive_extreme_records.Rd b/man/derive_extreme_records.Rd index 3063a5056f..a394c9b104 100644 --- a/man/derive_extreme_records.Rd +++ b/man/derive_extreme_records.Rd @@ -15,7 +15,7 @@ derive_extreme_records( check_type = "warning", exist_flag = NULL, true_value = "Y", - false_value = "N", + false_value = NA_character_, keep_source_vars = exprs(everything()), set_values_to ) diff --git a/man/derive_var_extreme_flag.Rd b/man/derive_var_extreme_flag.Rd index a1211565d3..0375ef0309 100644 --- a/man/derive_var_extreme_flag.Rd +++ b/man/derive_var_extreme_flag.Rd @@ -10,6 +10,8 @@ derive_var_extreme_flag( order, new_var, mode, + true_value = "Y", + false_value = NA_character_, flag_all = FALSE, check_type = "warning" ) @@ -32,8 +34,8 @@ Permitted Values: list of variables or functions of variables} \item{new_var}{Variable to add -The specified variable is added to the output dataset. It is set to \code{"Y"} -for the first or last observation (depending on the mode) of each by group. +The specified variable is added to the output dataset. It is set to the value +set in \code{true_value} for the first or last observation (depending on the mode) of each by group. Permitted Values: list of name-value pairs} @@ -43,6 +45,20 @@ Determines of the first or last observation is flagged. Permitted Values: \code{"first"}, \code{"last"}} +\item{true_value}{True value + +The value for the specified variable \code{new_var}, applicable to +the first or last observation (depending on the mode) of each by group. + +Permitted Values: An atomic scalar} + +\item{false_value}{False value + +The value for the specified variable \code{new_var}, NOT applicable to +the first or last observation (depending on the mode) of each by group. + +Permitted Values: An atomic scalar} + \item{flag_all}{Flag setting A logical value where if set to \code{TRUE}, all records are flagged diff --git a/man/derive_vars_joined.Rd b/man/derive_vars_joined.Rd index 1b3452d615..5092e89ab8 100644 --- a/man/derive_vars_joined.Rd +++ b/man/derive_vars_joined.Rd @@ -15,6 +15,9 @@ derive_vars_joined( filter_add = NULL, filter_join = NULL, mode = NULL, + exist_flag = NULL, + true_value = "Y", + false_value = NA_character_, missing_values = NULL, check_type = "warning" ) @@ -122,6 +125,29 @@ If the \code{order} argument is not specified, the \code{mode} argument is ignor \emph{Permitted Values}: \code{"first"}, \code{"last"}, \code{NULL}} +\item{exist_flag}{Exist flag + +If the argument is specified (e.g., \code{exist_flag = FLAG}), the specified +variable (e.g., \code{FLAG}) is added to the input dataset. This variable will +be the value provided in \code{true_value} for all selected records from \code{dataset_add} +which are merged into the input dataset, and the value provided in \code{false_value} otherwise. + +\emph{Permitted Values}: Variable name} + +\item{true_value}{True value + +The value for the specified variable \code{exist_flag}, applicable to +the first or last observation (depending on the mode) of each by group. + +Permitted Values: An atomic scalar} + +\item{false_value}{False value + +The value for the specified variable \code{exist_flag}, NOT applicable to +the first or last observation (depending on the mode) of each by group. + +Permitted Values: An atomic scalar} + \item{missing_values}{Values for non-matching observations For observations of the input dataset (\code{dataset}) which do not have a diff --git a/man/derive_vars_merged.Rd b/man/derive_vars_merged.Rd index ab959ed89f..dca45a6222 100644 --- a/man/derive_vars_merged.Rd +++ b/man/derive_vars_merged.Rd @@ -13,7 +13,10 @@ derive_vars_merged( new_vars = NULL, filter_add = NULL, mode = NULL, - match_flag = NULL, + match_flag, + exist_flag = NULL, + true_value = "Y", + false_value = NA_character_, missing_values = NULL, check_type = "warning", duplicate_msg = NULL @@ -94,6 +97,8 @@ If the \code{order} argument is not specified, the \code{mode} argument is ignor \item{match_flag}{Match flag +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Please use \code{exist_flag} instead. + If the argument is specified (e.g., \code{match_flag = FLAG}), the specified variable (e.g., \code{FLAG}) is added to the input dataset. This variable will be \code{TRUE} for all selected records from \code{dataset_add} which are merged into @@ -101,6 +106,29 @@ the input dataset, and \code{NA} otherwise. \emph{Permitted Values}: Variable name} +\item{exist_flag}{Exist flag + +If the argument is specified (e.g., \code{exist_flag = FLAG}), the specified +variable (e.g., \code{FLAG}) is added to the input dataset. This variable will +be the value provided in \code{true_value} for all selected records from \code{dataset_add} +which are merged into the input dataset, and the value provided in \code{false_value} otherwise. + +\emph{Permitted Values}: Variable name} + +\item{true_value}{True value + +The value for the specified variable \code{exist_flag}, applicable to +the first or last observation (depending on the mode) of each by group. + +Permitted Values: An atomic scalar} + +\item{false_value}{False value + +The value for the specified variable \code{exist_flag}, NOT applicable to +the first or last observation (depending on the mode) of each by group. + +Permitted Values: An atomic scalar} + \item{missing_values}{Values for non-matching observations For observations of the input dataset (\code{dataset}) which do not have a @@ -210,7 +238,7 @@ derive_vars_merged( mode = "last", new_vars = exprs(LASTWGT = VSSTRESN, LASTWGTU = VSSTRESU), filter_add = VSTESTCD == "WEIGHT", - match_flag = vsdatafl + exist_flag = vsdatafl ) \%>\% select(STUDYID, USUBJID, AGE, AGEU, LASTWGT, LASTWGTU, vsdatafl) diff --git a/tests/testthat/test-derive_extreme_records.R b/tests/testthat/test-derive_extreme_records.R index b3531cbad4..53d34bc804 100644 --- a/tests/testthat/test-derive_extreme_records.R +++ b/tests/testthat/test-derive_extreme_records.R @@ -89,9 +89,9 @@ test_that("derive_extreme_records Test 2: derive first PD date", { adrs, tibble::tribble( ~USUBJID, ~ADT, ~AVALC, - "1", ymd(""), "N", + "1", ymd(""), NA_character_, "2", ymd("2021-07-16"), "Y", - "3", ymd(""), "N" + "3", ymd(""), NA_character_ ) %>% mutate( STUDYID = "XX1234", diff --git a/tests/testthat/test-derive_joined.R b/tests/testthat/test-derive_joined.R index eeb1afcc23..f52587cef7 100644 --- a/tests/testthat/test-derive_joined.R +++ b/tests/testthat/test-derive_joined.R @@ -333,3 +333,37 @@ test_that("derive_vars_joined Test 10: order vars are selected properly in funct keys = c("day", "val", "first_val") ) }) + +## Test 11: Ensure exist_flag, true/false value arguments work ---- +test_that("derive_vars_joined Test 11: Ensure exist_flag, true/false value arguments work", { + expected <- tibble::tribble( + ~USUBJID, ~ADY, ~AVISIT, ~AWLO, ~AWHI, ~flag, + "1", -2, "BASELINE", -30, 1, "Yes", + "1", 3, "WEEK 1", 2, 7, "Yes", + "1", 24, "WEEK 4", 23, 30, "Yes", + "2", NA, NA, NA, NA, "No" + ) + + windows <- tibble::tribble( + ~AVISIT, ~AWLO, ~AWHI, + "BASELINE", -30, 1, + "WEEK 1", 2, 7, + "WEEK 2", 8, 15, + "WEEK 3", 16, 22, + "WEEK 4", 23, 30 + ) + + expect_dfs_equal( + base = expected, + comp = derive_vars_joined( + select(expected, USUBJID, ADY), + dataset_add = windows, + join_vars = exprs(AWHI, AWLO), + filter_join = AWLO <= ADY & ADY <= AWHI, + exist_flag = flag, + true_value = "Yes", + false_value = "No" + ), + keys = c("USUBJID", "ADY") + ) +}) diff --git a/tests/testthat/test-derive_merged.R b/tests/testthat/test-derive_merged.R index 5dd92200cb..c9bf4d8ad4 100644 --- a/tests/testthat/test-derive_merged.R +++ b/tests/testthat/test-derive_merged.R @@ -103,11 +103,11 @@ test_that("derive_vars_merged Test 3: merge last value and flag matched by group by_vars = exprs(STUDYID, USUBJID), new_vars = exprs(WEIGHTBL = AVAL), mode = "last", - match_flag = matched + exist_flag = matched ) expected <- adsl %>% mutate( WEIGHTBL = c(68, 88, 55, NA), - matched = c(TRUE, TRUE, TRUE, NA) + matched = c("Y", "Y", "Y", NA_character_) ) expect_dfs_equal( @@ -117,8 +117,32 @@ test_that("derive_vars_merged Test 3: merge last value and flag matched by group ) }) -## Test 4: error if variable in both datasets ---- -test_that("derive_vars_merged Test 4: error if variable in both datasets", { +## Test 4: merge last value and flag matched by groups ---- +test_that("derive_vars_merged Test 4: merge last value and flag matched by groups", { + actual <- derive_vars_merged(adsl, + dataset_add = advs, + order = exprs(AVAL), + by_vars = exprs(STUDYID, USUBJID), + new_vars = exprs(WEIGHTBL = AVAL), + mode = "last", + exist_flag = matched, + true_value = "Y", + false_value = "N" + ) + expected <- adsl %>% mutate( + WEIGHTBL = c(68, 88, 55, NA), + matched = c("Y", "Y", "Y", "N") + ) + + expect_dfs_equal( + base = expected, + compare = actual, + keys = c("USUBJID") + ) +}) + +## Test 5: error if variable in both datasets ---- +test_that("derive_vars_merged Test 5: error if variable in both datasets", { expect_error( derive_vars_merged(advs, dataset_add = adsl, @@ -128,8 +152,8 @@ test_that("derive_vars_merged Test 4: error if variable in both datasets", { ) }) -## Test 5: by_vars with rename ---- -test_that("derive_vars_merged Test 5: by_vars with rename", { +## Test 6: by_vars with rename ---- +test_that("derive_vars_merged Test 6: by_vars with rename", { actual <- derive_vars_merged(advs, dataset_add = adsl1, by_vars = exprs(STUDYID, USUBJID = ID), @@ -146,8 +170,8 @@ test_that("derive_vars_merged Test 5: by_vars with rename", { ) }) -## Test 6: expressions for new_vars and missing_values ---- -test_that("derive_vars_merged Test 6: expressions for new_vars and missing_values", { +## Test 7: expressions for new_vars and missing_values ---- +test_that("derive_vars_merged Test 7: expressions for new_vars and missing_values", { actual <- derive_vars_merged( adsl, dataset_add = advs, @@ -169,8 +193,38 @@ test_that("derive_vars_merged Test 6: expressions for new_vars and missing_value ) }) -## Test 7: use new variables in filter_add and order ---- -test_that("derive_vars_merged Test 7: use new variables in filter_add and order", { + +## Test 8: Use of missing_values and exist_flags ---- +test_that("derive_vars_merged Test 8: Use of missing_values and exist_flags", { + actual <- derive_vars_merged( + adsl, + dataset_add = advs, + by_vars = exprs(USUBJID), + order = exprs(AVISIT), + new_vars = exprs(LASTVIS = str_to_upper(AVISIT)), + mode = "last", + missing_values = exprs(LASTVIS = "UNKNOWN"), + exist_flag = matched, + true_value = NA, + false_value = "No" + ) + + expected <- adsl %>% + mutate( + LASTVIS = c("WEEK 2", "BASELINE", "WEEK 4", "UNKNOWN"), + matched = c(NA, NA, NA, "No") + ) + + + expect_dfs_equal( + base = expected, + compare = actual, + keys = "USUBJID" + ) +}) + +## Test 9: use new variables in filter_add and order ---- +test_that("derive_vars_merged Test 9: use new variables in filter_add and order", { expected <- tibble::tribble( ~USUBJID, ~TRTSDT, ~TRTSSEQ, "ST42-1", "2020-12-14", 2, @@ -208,8 +262,8 @@ test_that("derive_vars_merged Test 7: use new variables in filter_add and order" ) }) -## Test 8: warning if not unique w.r.t the by variables and the order ---- -test_that("derive_vars_merged Test 8: warning if not unique w.r.t the by variables and the order", { +## Test 10: warning if not unique w.r.t the by variables and the order ---- +test_that("derive_vars_merged Test 10: warning if not unique w.r.t the by variables and the order", { # nolint expect_warning( actual <- derive_vars_merged(advs, dataset_add = adsl2, @@ -222,8 +276,8 @@ test_that("derive_vars_merged Test 8: warning if not unique w.r.t the by variabl ) }) -## Test 9: error if not unique w.r.t the by variables and the order ---- -test_that("derive_vars_merged Test 9: error if not unique w.r.t the by variables and the order", { +## Test 11: error if not unique w.r.t the by variables and the order ---- +test_that("derive_vars_merged Test 11: error if not unique w.r.t the by variables and the order", { expect_error( actual <- derive_vars_merged(advs, dataset_add = adsl2, @@ -237,8 +291,8 @@ test_that("derive_vars_merged Test 9: error if not unique w.r.t the by variables ) }) -## Test 10: error if variables in missing_values but not in new_vars ---- -test_that("derive_vars_merged Test 10: error if variables in missing_values but not in new_vars", { +## Test 12: error if variables in missing_values but not in new_vars ---- +test_that("derive_vars_merged Test 12: error if variables in missing_values but not in new_vars", { expect_error( derive_vars_merged( adsl, @@ -254,9 +308,23 @@ test_that("derive_vars_merged Test 10: error if variables in missing_values but ) }) +test_that("deprecation messaging for match_flag", { + expect_warning( + derive_vars_merged(adsl, + dataset_add = advs, + order = exprs(AVAL), + by_vars = exprs(STUDYID, USUBJID), + new_vars = exprs(WEIGHTBL = AVAL), + mode = "last", + match_flag = matched + ), + class = "lifecycle_warning_deprecated" + ) +}) + # derive_var_merged_exist_flag ---- -## Test 11: merge existence flag ---- -test_that("derive_var_merged_exist_flag Test 11: merge existence flag", { +## Test 13: merge existence flag ---- +test_that("derive_var_merged_exist_flag Test 13: merge existence flag", { actual <- derive_var_merged_exist_flag( adsl, dataset_add = advs, @@ -276,8 +344,8 @@ test_that("derive_var_merged_exist_flag Test 11: merge existence flag", { ) }) -## Test 12: by_vars with rename ---- -test_that("derive_var_merged_exist_flag Test 12: by_vars with rename", { +## Test 14: by_vars with rename ---- +test_that("derive_var_merged_exist_flag Test 14: by_vars with rename", { actual <- derive_var_merged_exist_flag( adsl, dataset_add = advs1, @@ -298,8 +366,8 @@ test_that("derive_var_merged_exist_flag Test 12: by_vars with rename", { }) # derive_vars_merged_lookup ---- -## Test 13: merge lookup table ---- -test_that("derive_vars_merged_lookup Test 13: merge lookup table", { +## Test 15: merge lookup table ---- +test_that("derive_vars_merged_lookup Test 15: merge lookup table", { param_lookup <- tibble::tribble( ~VSTESTCD, ~VSTEST, ~PARAMCD, ~DESCRIPTION, "WEIGHT", "Weight", "WEIGHT", "Weight (kg)", @@ -336,8 +404,8 @@ test_that("derive_vars_merged_lookup Test 13: merge lookup table", { ## the lookup table -## Test 14: all by_vars have records in the lookup table ---- -test_that("derive_vars_merged_lookup Test 14: all by_vars have records in the lookup table", { +## Test 16: all by_vars have records in the lookup table ---- +test_that("derive_vars_merged_lookup Test 16: all by_vars have records in the lookup table", { param_lookup <- tibble::tribble( ~VSTESTCD, ~VSTEST, ~PARAMCD, ~DESCRIPTION, "WEIGHT", "Weight", "WEIGHT", "Weight (kg)", @@ -372,8 +440,8 @@ test_that("derive_vars_merged_lookup Test 14: all by_vars have records in the l ) }) -## Test 15: by_vars with rename ---- -test_that("derive_vars_merged_lookup Test 15: by_vars with rename", { +## Test 17: by_vars with rename ---- +test_that("derive_vars_merged_lookup Test 17: by_vars with rename", { param_lookup <- tibble::tribble( ~TESTCD, ~VSTEST, ~PARAMCD, ~DESCRIPTION, "WEIGHT", "Weight", "WEIGHT", "Weight (kg)", @@ -409,8 +477,8 @@ test_that("derive_vars_merged_lookup Test 15: by_vars with rename", { # get_not_mapped ---- -## Test 16: not all by_vars have records in the lookup table ---- -test_that("get_not_mapped Test 16: not all by_vars have records in the lookup table", { +## Test 18: not all by_vars have records in the lookup table ---- +test_that("get_not_mapped Test 18: not all by_vars have records in the lookup table", { param_lookup <- tibble::tribble( ~VSTESTCD, ~VSTEST, ~PARAMCD, ~DESCRIPTION, "WEIGHT", "Weight", "WEIGHT", "Weight (kg)", @@ -448,8 +516,8 @@ test_that("get_not_mapped Test 16: not all by_vars have records in the lookup ta }) # derive_var_merged_summary ---- -## Test 17: dataset == dataset_add, no filter ---- -test_that("derive_var_merged_summary Test 17: dataset == dataset_add, no filter", { +## Test 19: dataset == dataset_add, no filter ---- +test_that("derive_var_merged_summary Test 19: dataset == dataset_add, no filter", { expected <- tibble::tribble( ~AVISIT, ~ASEQ, ~AVAL, ~MEANVIS, "WEEK 1", 1, 10, 10, @@ -475,8 +543,8 @@ test_that("derive_var_merged_summary Test 17: dataset == dataset_add, no filter" ) }) -## Test 18: dataset != dataset_add, filter ---- -test_that("derive_var_merged_summary Test 18: dataset != dataset_add, filter", { +## Test 20: dataset != dataset_add, filter ---- +test_that("derive_var_merged_summary Test 20: dataset != dataset_add, filter", { expected <- tibble::tribble( ~USUBJID, ~MEANPBL, "1", 13.5, @@ -507,8 +575,8 @@ test_that("derive_var_merged_summary Test 18: dataset != dataset_add, filter", { ) }) -## Test 19: by_vars with rename ---- -test_that("derive_var_merged_summary Test 19: by_vars with rename", { +## Test 21: by_vars with rename ---- +test_that("derive_var_merged_summary Test 21: by_vars with rename", { expected <- tibble::tribble( ~AVISIT, ~ASEQ, ~AVAL, ~MEANVIS, "WEEK 1", 1, 10, 10, @@ -535,7 +603,8 @@ test_that("derive_var_merged_summary Test 19: by_vars with rename", { ) }) -test_that("derive_var_merged_summary Test 19: deprecation warning", { +## Test 22: deprecation warning ---- +test_that("derive_var_merged_summary Test 22: deprecation warning", { expected <- tibble::tribble( ~AVISIT, ~ASEQ, ~AVAL, ~MEANVIS, "WEEK 1", 1, 10, 10, diff --git a/tests/testthat/test-derive_var_extreme_flag.R b/tests/testthat/test-derive_var_extreme_flag.R index c918c3d146..61062b7191 100644 --- a/tests/testthat/test-derive_var_extreme_flag.R +++ b/tests/testthat/test-derive_var_extreme_flag.R @@ -205,3 +205,34 @@ test_that("derive_var_extreme_flag Test 8: additional case for missing order var keys = c("USUBJID", "AVISITN", "AVAL") ) }) + + +## Test 9: changing true/false flag value works ---- +test_that("derive_var_extreme_flag Test 9: changing true/false flag value works", { + input <- tibble::tribble( + ~USUBJID, ~AVISITN, ~AVAL, + 1, 1, 12, + 1, 3, 9, + 2, 2, 42, + 3, 3, 14, + 3, 3, 10 + ) + + expected_output <- input %>% mutate(firstfl = c("Yes", "No", "Yes", "Yes", "No")) + + actual_output <- derive_var_extreme_flag( + input, + by_vars = exprs(USUBJID), + order = exprs(AVISITN, desc(AVAL)), + new_var = firstfl, + mode = "first", + true_value = "Yes", + false_value = "No" + ) + + expect_dfs_equal( + base = expected_output, + compare = actual_output, + keys = c("USUBJID", "AVISITN", "AVAL") + ) +})