From f155506ead26e2615539cdd6419e1b4fa4764716 Mon Sep 17 00:00:00 2001 From: Stefan Bundfuss Date: Wed, 30 Oct 2024 11:37:05 +0000 Subject: [PATCH] #2510 enhance_derive_param_computed: add note if no records were added due to NAs --- R/derive_param_computed.R | 16 +++++++ man/derive_param_computed.Rd | 5 ++- .../testthat/_snaps/derive_param_computed.md | 27 +++++++++++- tests/testthat/test-derive_param_computed.R | 43 +++++++++++++++++-- 4 files changed, 84 insertions(+), 7 deletions(-) diff --git a/R/derive_param_computed.R b/R/derive_param_computed.R index dcc117bb6..d0a0147ed 100644 --- a/R/derive_param_computed.R +++ b/R/derive_param_computed.R @@ -122,6 +122,9 @@ #' If the argument is set to a list of variables, observations are added even #' if some of specified variables are `NA` (see Example 1c). #' +#' *Permitted Values:* `TRUE`, `FALSE`, or a list of variables created by +#' `exprs()` e.g. `exprs(ADTF, ATMF)` +#' #' @details For each group (with respect to the variables specified for the #' `by_vars` parameter) an observation is added to the output dataset if the #' filtered input dataset (`dataset`) or the additional dataset @@ -390,6 +393,7 @@ derive_param_computed <- function(dataset = NULL, } else { na_vars <- analysis_vars_chr } + nobs_before <- nrow(hori_data) hori_data <- filter( hori_data, !!!parse_exprs(map_chr( @@ -397,6 +401,18 @@ derive_param_computed <- function(dataset = NULL, ~ str_c("!is.na(", .x, ")") )) ) + if (nobs_before > 0 && nrow(hori_data) == 0) { + cli_inform(c( + paste( + "No computed records were added because for all potential computed", + "records at least one of the contributing values was {.val {NA}}." + ), + paste( + "If this is not expected, please check the input data and the value of", + "the {.arg keep_nas} argument." + ) + )) + } } # add computed variables like AVAL and constant variables like PARAMCD diff --git a/man/derive_param_computed.Rd b/man/derive_param_computed.Rd index be423bc11..df0d0cfa3 100644 --- a/man/derive_param_computed.Rd +++ b/man/derive_param_computed.Rd @@ -131,7 +131,10 @@ If the argument is set to \code{TRUE}, observations are added even if some of the values contributing to the computed value are \code{NA} (see Example 1b). If the argument is set to a list of variables, observations are added even -if some of specified variables are \code{NA} (see Example 1c).} +if some of specified variables are \code{NA} (see Example 1c). + +\emph{Permitted Values:} \code{TRUE}, \code{FALSE}, or a list of variables created by +\code{exprs()} e.g. \code{exprs(ADTF, ATMF)}} } \value{ The input dataset with the new parameter added. Note, a variable will only diff --git a/tests/testthat/_snaps/derive_param_computed.md b/tests/testthat/_snaps/derive_param_computed.md index c45ea2b6a..0cd3d78b4 100644 --- a/tests/testthat/_snaps/derive_param_computed.md +++ b/tests/testthat/_snaps/derive_param_computed.md @@ -9,7 +9,30 @@ Error in `derive_param_computed()`: ! Argument `keep_nas` must be either TRUE, FALSE, or a list of , but is a number. -# assert_parameters_argument Test 12: error if argument is of wrong type +# derive_param_computed Test 12: inform if no new records due to NAs + + Code + derive_param_computed(advs, by_vars = exprs(USUBJID, AVISIT), parameters = c( + "SYSBP", "DIABP"), set_values_to = exprs(AVAL = (AVAL.SYSBP + 2 * + AVAL.DIABP) / 3, PARAMCD = "MAP", PARAM = "Mean Arterial Pressure (mmHg)", ADT = ADT.SYSBP, + ADTF = ADTF.SYSBP)) + Message + No computed records were added because for all potential computed records at least one of the contributing values was NA. + If this is not expected, please check the input data and the value of the `keep_nas` argument. + Output + # A tibble: 8 x 7 + USUBJID PARAMCD PARAM AVAL AVISIT ADT ADTF + + 1 01-701-1015 DIABP Diastolic Blood Pressure (m~ 51 BASEL~ 2024-01-10 + 2 01-701-1015 DIABP Diastolic Blood Pressure (m~ 50 WEEK 2 2024-01-24 + 3 01-701-1015 SYSBP Systolic Blood Pressure (mm~ 121 BASEL~ 2024-01-10 + 4 01-701-1015 SYSBP Systolic Blood Pressure (mm~ 121 WEEK 2 2024-01-24 + 5 01-701-1028 DIABP Diastolic Blood Pressure (m~ 79 BASEL~ 2024-01-10 + 6 01-701-1028 DIABP Diastolic Blood Pressure (m~ 80 WEEK 2 2024-01-24 + 7 01-701-1028 SYSBP Systolic Blood Pressure (mm~ 130 BASEL~ 2024-01-10 + 8 01-701-1028 SYSBP Systolic Blood Pressure (mm~ NA WEEK 2 2024-01-24 + +# assert_parameters_argument Test 13: error if argument is of wrong type Code assert_parameters_argument(myparameters <- c(1, 2, 3)) @@ -17,7 +40,7 @@ Error in `assert_parameters_argument()`: ! `myparameters <- c(1, 2, 3)` must be a character vector or a list of expressions but it is a double vector. -# get_hori_data Test 13: error if variables with more than one dot +# get_hori_data Test 14: error if variables with more than one dot Code get_hori_data(input, parameters = exprs(SYSBP, DIABP), by_vars = exprs(USUBJID, diff --git a/tests/testthat/test-derive_param_computed.R b/tests/testthat/test-derive_param_computed.R index 832a9e162..e51276fee 100644 --- a/tests/testthat/test-derive_param_computed.R +++ b/tests/testthat/test-derive_param_computed.R @@ -490,9 +490,44 @@ test_that("derive_param_computed Test 11: error if keep_nas is invalid", { ) }) +## Test 12: inform if no new records due to NAs ---- +test_that("derive_param_computed Test 12: inform if no new records due to NAs", { + advs <- tibble::tribble( + ~USUBJID, ~PARAMCD, ~PARAM, ~AVAL, ~AVISIT, + "01-701-1015", "DIABP", "Diastolic Blood Pressure (mmHg)", 51, "BASELINE", + "01-701-1015", "DIABP", "Diastolic Blood Pressure (mmHg)", 50, "WEEK 2", + "01-701-1015", "SYSBP", "Systolic Blood Pressure (mmHg)", 121, "BASELINE", + "01-701-1015", "SYSBP", "Systolic Blood Pressure (mmHg)", 121, "WEEK 2", + "01-701-1028", "DIABP", "Diastolic Blood Pressure (mmHg)", 79, "BASELINE", + "01-701-1028", "DIABP", "Diastolic Blood Pressure (mmHg)", 80, "WEEK 2", + "01-701-1028", "SYSBP", "Systolic Blood Pressure (mmHg)", 130, "BASELINE", + "01-701-1028", "SYSBP", "Systolic Blood Pressure (mmHg)", NA, "WEEK 2" + ) %>% + mutate( + ADT = case_when( + AVISIT == "BASELINE" ~ as.Date("2024-01-10"), + AVISIT == "WEEK 2" ~ as.Date("2024-01-24") + ), + ADTF = NA_character_ + ) + + expect_snapshot(derive_param_computed( + advs, + by_vars = exprs(USUBJID, AVISIT), + parameters = c("SYSBP", "DIABP"), + set_values_to = exprs( + AVAL = (AVAL.SYSBP + 2 * AVAL.DIABP) / 3, + PARAMCD = "MAP", + PARAM = "Mean Arterial Pressure (mmHg)", + ADT = ADT.SYSBP, + ADTF = ADTF.SYSBP + ) + )) +}) + # assert_parameters_argument ---- -## Test 12: error if argument is of wrong type ---- -test_that("assert_parameters_argument Test 12: error if argument is of wrong type", { +## Test 13: error if argument is of wrong type ---- +test_that("assert_parameters_argument Test 13: error if argument is of wrong type", { expect_snapshot( assert_parameters_argument(myparameters <- c(1, 2, 3)), error = TRUE @@ -500,8 +535,8 @@ test_that("assert_parameters_argument Test 12: error if argument is of wrong typ }) # get_hori_data ---- -## Test 13: error if variables with more than one dot ---- -test_that("get_hori_data Test 13: error if variables with more than one dot", { +## Test 14: error if variables with more than one dot ---- +test_that("get_hori_data Test 14: error if variables with more than one dot", { input <- tibble::tribble( ~USUBJID, ~PARAMCD, ~PARAM, ~AVAL, ~AVALU, ~VISIT, "01-701-1015", "DIABP", "Diastolic Blood Pressure (mmHg)", 51, "mmHg", "BASELINE",