diff --git a/R/derive_merged.R b/R/derive_merged.R index 7fbe55ab61..5a767b83a4 100644 --- a/R/derive_merged.R +++ b/R/derive_merged.R @@ -385,18 +385,26 @@ derive_vars_merged <- function(dataset, select(!!!by_vars_right, !!!replace_values_by_names(new_vars)) } + if (!is.null(exist_flag)) { + add_data <- mutate( + add_data, + !!exist_flag := TRUE + ) + } + if (!is.null(missing_values)) { - exist_flag_var <- get_new_tmp_var(add_data, prefix = "tmp_exist_flag") + missing_values_var <- get_new_tmp_var(add_data, prefix = "tmp_missing_flag") } else { - exist_flag_var <- exist_flag + missing_values_var <- missing_values } - - if (!is.null(exist_flag_var)) { + if (!is.null(missing_values_var)) { add_data <- mutate( add_data, - !!exist_flag_var := true_value + !!missing_values_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 <- @@ -420,20 +428,22 @@ derive_vars_merged <- function(dataset, } dataset <- left_join(dataset, add_data, by = vars2chr(by_vars)) - if (!is.null(missing_values)) { + if (!is.null(missing_values_var)) { update_missings <- map2( syms(names(missing_values)), missing_values, - ~ expr(if_else(is.na(!!exist_flag_var), !!.y, !!.x)) + ~ expr(if_else(is.na(!!missing_values_var), !!.y, !!.x)) ) names(update_missings) <- names(missing_values) dataset <- dataset %>% mutate(!!!update_missings) } - if (!is.null(exist_flag_var)) { + + if (!is.null(exist_flag)) { dataset <- dataset %>% - mutate(!!exist_flag_var := ifelse(is.na(!!exist_flag_var), false_value, !!exist_flag_var)) + mutate(!!exist_flag := ifelse(is.na(!!exist_flag), false_value, true_value)) } + dataset %>% remove_tmp_vars() } diff --git a/tests/testthat/test-derive_merged.R b/tests/testthat/test-derive_merged.R index 337a2fc3ef..fe2ab77dac 100644 --- a/tests/testthat/test-derive_merged.R +++ b/tests/testthat/test-derive_merged.R @@ -193,8 +193,36 @@ test_that("derive_vars_merged Test 7: expressions for new_vars and missing_value ) }) -## Test 8: use new variables in filter_add and order ---- -test_that("derive_vars_merged Test 8: use new variables in filter_add and order", { + +## Test 8: expressions for new_vars and missing_values and exist_flags ---- +test_that("derive_vars_merged Test 8: expressions for new_vars and 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 = "Yes", + false_value = "No" + ) + + expected <- adsl %>% + mutate(LASTVIS = c("WEEK 2", "BASELINE", "WEEK 4", "UNKNOWN"), + matched = c("Yes", "Yes", "Yes", "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, @@ -232,8 +260,8 @@ test_that("derive_vars_merged Test 8: use new variables in filter_add and order" ) }) -## Test 9: warning if not unique w.r.t the by variables and the order ---- -test_that("derive_vars_merged Test 9: 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", { expect_warning( actual <- derive_vars_merged(advs, dataset_add = adsl2, @@ -246,8 +274,8 @@ test_that("derive_vars_merged Test 9: warning if not unique w.r.t the by variabl ) }) -## Test 10: error if not unique w.r.t the by variables and the order ---- -test_that("derive_vars_merged Test 10: 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, @@ -261,8 +289,8 @@ test_that("derive_vars_merged Test 10: error if not unique w.r.t the by variable ) }) -## Test 11: error if variables in missing_values but not in new_vars ---- -test_that("derive_vars_merged Test 11: 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, @@ -279,8 +307,8 @@ test_that("derive_vars_merged Test 11: error if variables in missing_values but }) # 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, @@ -300,8 +328,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, @@ -322,8 +350,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)", @@ -360,8 +388,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)", @@ -396,8 +424,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)", @@ -433,8 +461,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)", @@ -472,8 +500,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, @@ -499,8 +527,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, @@ -531,8 +559,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, @@ -559,7 +587,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,