Skip to content

Commit

Permalink
#2510 enhance_derive_param_computed: add note if no records were adde…
Browse files Browse the repository at this point in the history
…d due to NAs
  • Loading branch information
bundfussr committed Oct 30, 2024
1 parent b99d063 commit f155506
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
16 changes: 16 additions & 0 deletions R/derive_param_computed.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -390,13 +393,26 @@ 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(
na_vars,
~ 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
Expand Down
5 changes: 4 additions & 1 deletion man/derive_param_computed.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 25 additions & 2 deletions tests/testthat/_snaps/derive_param_computed.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,38 @@
Error in `derive_param_computed()`:
! Argument `keep_nas` must be either TRUE, FALSE, or a list of <symbol>, 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
<chr> <chr> <chr> <dbl> <chr> <date> <chr>
1 01-701-1015 DIABP Diastolic Blood Pressure (m~ 51 BASEL~ 2024-01-10 <NA>
2 01-701-1015 DIABP Diastolic Blood Pressure (m~ 50 WEEK 2 2024-01-24 <NA>
3 01-701-1015 SYSBP Systolic Blood Pressure (mm~ 121 BASEL~ 2024-01-10 <NA>
4 01-701-1015 SYSBP Systolic Blood Pressure (mm~ 121 WEEK 2 2024-01-24 <NA>
5 01-701-1028 DIABP Diastolic Blood Pressure (m~ 79 BASEL~ 2024-01-10 <NA>
6 01-701-1028 DIABP Diastolic Blood Pressure (m~ 80 WEEK 2 2024-01-24 <NA>
7 01-701-1028 SYSBP Systolic Blood Pressure (mm~ 130 BASEL~ 2024-01-10 <NA>
8 01-701-1028 SYSBP Systolic Blood Pressure (mm~ NA WEEK 2 2024-01-24 <NA>

# assert_parameters_argument Test 13: error if argument is of wrong type

Code
assert_parameters_argument(myparameters <- c(1, 2, 3))
Condition
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,
Expand Down
43 changes: 39 additions & 4 deletions tests/testthat/test-derive_param_computed.R
Original file line number Diff line number Diff line change
Expand Up @@ -490,18 +490,53 @@ 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
)
})

# 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",
Expand Down

0 comments on commit f155506

Please sign in to comment.