diff --git a/R/derive_vars_cat.R b/R/derive_vars_cat.R index 1e2d61d20..e504be73b 100644 --- a/R/derive_vars_cat.R +++ b/R/derive_vars_cat.R @@ -183,13 +183,15 @@ derive_vars_cat <- function(dataset, error = function(e) { # Catch the error and append your own message cli_abort( - paste( - "Failed to convert {.arg definition} to {.cls tibble}.", - "{.arg definition} should be specified similarly to how you would", - "specify a {.cls tibble} using the {.fun tibble::tribble}` function so it", - "can be converted to {.cls tibble}` using {.fun tibble::tribble}`." - ), - e$message + c( + paste( + "Failed to convert {.arg definition} to {.cls tibble}.", + "{.arg definition} should be specified similarly to how you would", + "specify a {.cls tibble} using the {.fun tibble::tribble} function so it", + "can be converted to {.cls tibble} using {.fun tibble::tribble}." + ), + e$message + ) ) } ) diff --git a/tests/testthat/_snaps/derive_vars_cat.md b/tests/testthat/_snaps/derive_vars_cat.md index 372243c03..616ac7960 100644 --- a/tests/testthat/_snaps/derive_vars_cat.md +++ b/tests/testthat/_snaps/derive_vars_cat.md @@ -4,12 +4,8 @@ Did you forget to specify `by_vars`, or are you rerunning your code? -# derive_vars_cat Test 5: Error when definition is not an exprs object - - Argument `definition` must be a list of expressions but is a tibble. - i To create a list of expressions use `exprs()`. - # derive_vars_cat Test 13: definition has wrong shape - Failed to convert `definition` to `tibble`. `definition` should be specified similarly to how you would specify a `tibble` using the `tribble()` function so it can be converted to `tibble` using `tribble()`. + Failed to convert `definition` to . `definition` should be specified similarly to how you would specify a using the `tibble::tribble()` function so it can be converted to using `tibble::tribble()`. + Data must be rectangular. diff --git a/tests/testthat/test-derive_vars_cat.R b/tests/testthat/test-derive_vars_cat.R index 6eb0a44cc..d4642565f 100644 --- a/tests/testthat/test-derive_vars_cat.R +++ b/tests/testthat/test-derive_vars_cat.R @@ -1,28 +1,3 @@ -# Load the advs dataset -advs <- tibble::tribble( - ~USUBJID, ~VSTEST, ~AVAL, - "01-701-1015", "Height", 147.32, - "01-701-1015", "Weight", 53.98, - "01-701-1023", "Height", 162.56, - "01-701-1023", "Weight", 78.47, - "01-701-1028", "Height", 177.8, - "01-701-1028", "Weight", 98.88, - "01-701-1033", "Height", 175.26, - "01-701-1033", "Weight", 88.45, - "01-701-1034", "Height", NA, - "01-701-1034", "Weight", NA, - "01-701-1047", "Height", NA, - "01-701-1047", "Weight", NA, - "01-701-1097", "Height", 168.91, - "01-701-1097", "Weight", 78.02, - "01-701-1111", "Height", 158.24, - "01-701-1111", "Weight", 60.33, - "01-701-1115", "Height", 181.61, - "01-701-1115", "Weight", 78.7, - "01-701-1118", "Height", 180.34, - "01-701-1118", "Weight", 71.67 -) %>% arrange(VSTEST) - expected_result <- tibble::tribble( ~USUBJID, ~VSTEST, ~AVAL, ~AVALCAT1, ~AVALCA1N, "01-701-1015", "Height", 147.32, "<160", 2, @@ -46,6 +21,10 @@ expected_result <- tibble::tribble( "01-701-1115", "Weight", 78.7, NA, NA, "01-701-1118", "Weight", 71.67, NA, NA ) + +advs <- expected_result %>% select( + USUBJID, VSTEST, AVAL +) ## Test 1: Basic functionality without by_vars ---- test_that("derive_vars_cat Test 1: Basic functionality without by_vars", { # Define the condition and categories @@ -124,8 +103,9 @@ test_that("derive_vars_cat Test 5: Error when definition is not an exprs object" "AVAL < 160", "<160", 2 ) # Snapshot the error message - expect_snapshot_error( - derive_vars_cat(advs, definition) + expect_error( + derive_vars_cat(advs, definition), + class = "assert_expr_list" ) }) @@ -152,8 +132,8 @@ test_that("derive_vars_cat Test 6: Error when required columns are missing from test_that("derive_vars_cat Test 7: Correct behavior when no conditions are met", { # Define conditions that do not match any rows definition <- exprs( - ~condition, ~AVALCAT1, ~AVALCA1N, - VSTEST == "Height" & AVAL < 0, "<0", 1 + ~condition, ~AVALCAT1, ~AVALCA1N, + VSTEST == "Height" & AVAL < 0, "<0", 1 ) expected_result <- tibble::tribble( diff --git a/vignettes/bds_exposure.Rmd b/vignettes/bds_exposure.Rmd index 0b4028564..673f08e76 100644 --- a/vignettes/bds_exposure.Rmd +++ b/vignettes/bds_exposure.Rmd @@ -41,7 +41,7 @@ otherwise specified.* * [Create 1:1 Mapping Records](#onetoone) * [Create Summary Records](#summaryrec) * [Assign `PARAMCD`, `PARAMN`, etc. from Reference Tables](#paramcd) -* [Derive Categorization Variables (`AVALCATx`)](#cat) +* [Derive Categorization Variables (`AVALCATy`)](#cat) * [Assign `ASEQ`](#aseq) * [Add ADSL variables `ASEQ`](#adsl_vars) * [Add Labels and Attributes](#attributes) @@ -514,18 +514,18 @@ TNDOSINT | Overall dose intensity (%) | 12 ```{r eval=TRUE, include=FALSE, echo=FALSE} param_lookup <- tribble( - ~PARAMCD, ~PARAM, ~PARAMN, - "DURD", "Study drug duration during constant dosing interval (days)", 1, - "DOSE", "Dose administered during constant dosing interval (mg)", 2, - "PLDOSE", "Planned dose during constant dosing interval (mg)", 3, - "ADJ", "Dose adjusted during constant dosing interval", 4, - "ADJAE", "Dose adjusted due to AE during constant dosing interval", 5, - "TDURD", "Overall duration (days)", 6, - "TDOSE", "Total dose administered (mg)", 7, - "TPDOSE", "Total planned dose (mg)", 9, - "TADJ", "Dose adjusted during study", 10, - "TADJAE", "Dose adjusted during study due to AE", 11, - "TNDOSINT", "Overall dose intensity (%)", 12 + ~PARAMCD, ~PARAM, ~PARAMN, + "DURD", "Study drug duration during constant dosing interval (days)", 1, + "DOSE", "Dose administered during constant dosing interval (mg)", 2, + "PLDOSE", "Planned dose during constant dosing interval (mg)", 3, + "ADJ", "Dose adjusted during constant dosing interval", 4, + "ADJAE", "Dose adjusted due to AE during constant dosing interval", 5, + "TDURD", "Overall duration (days)", 6, + "TDOSE", "Total dose administered (mg)", 7, + "TPDOSE", "Total planned dose (mg)", 9, + "TADJ", "Dose adjusted during study", 10, + "TADJAE", "Dose adjusted during study due to AE", 11, + "TNDOSINT", "Overall dose intensity (%)", 12 ) ``` @@ -549,17 +549,16 @@ We can use the `derive_vars_cat()` function to derive the categorization variabl ```{r eval=TRUE, echo=TRUE} avalcax_lookup <- exprs( - ~PARAMCD, ~condition, ~AVALCAT1, - "TDURD", AVAL >= 90, ">= 90 days", + ~PARAMCD, ~condition, ~AVALCAT1, + "TDURD", AVAL >= 90, ">= 90 days", "TDURD", AVAL >= 30 & AVAL < 90, ">= 30 and < 90 days", - "TDURD", AVAL < 30, "< 30 days", - "TDOSE", AVAL < 1000, "< 1000 mg", - "TDOSE", AVAL >= 1000, ">= 1000 mg", - "TPDOSE", AVAL < 1000, "< 1000 mg", - "TPDOSE", AVAL >= 1000, ">= 1000 mg" + "TDURD", AVAL < 30, "< 30 days", + "TDOSE", AVAL < 1000, "< 1000 mg", + "TDOSE", AVAL >= 1000, ">= 1000 mg", + "TPDOSE", AVAL < 1000, "< 1000 mg", + "TPDOSE", AVAL >= 1000, ">= 1000 mg" ) - adex <- adex %>% derive_vars_cat( definition = avalcax_lookup, diff --git a/vignettes/bds_finding.Rmd b/vignettes/bds_finding.Rmd index b3fe3b2a7..ea28cb87f 100644 --- a/vignettes/bds_finding.Rmd +++ b/vignettes/bds_finding.Rmd @@ -44,7 +44,7 @@ otherwise specified.* * [Derive Analysis Flags (e.g. `ANL01FL`)](#analysisrec) * [Assign Treatment (`TRTA`, `TRTP`)](#treatment) * [Assign `ASEQ`](#aseq) -* [Derive Categorization Variables (`AVALCATx`)](#cat) +* [Derive Categorization Variables (`AVALCATy`)](#cat) * [Derive Criterion Variables (`CRITy`, `CRITyFL`, `CRITyFLN`)](#crit_vars) * [Add ADSL variables](#adsl_vars) * [Derive New Rows](#additional) @@ -214,16 +214,16 @@ TEMP | TEMP | Temperature (C) | 7 | Vital Sign | 2 This lookup may now be joined to the source data: ```{r eval=TRUE, include=FALSE} -param_lookup <- tribble( - ~VSTESTCD, ~PARAMCD, ~PARAM, ~PARAMN, ~PARCAT1, ~PARCAT1N, - "HEIGHT", "HEIGHT", "Height (cm)", 1, "Subject Characteristic", 1, - "WEIGHT", "WEIGHT", "Weight (kg)", 2, "Subject Characteristic", 1, - "DIABP", "DIABP", "Diastolic Blood Pressure (mmHg)", 3, "Vital Sign", 2, - "MAP", "MAP", "Mean Arterial Pressure (mmHg)", 4, "Vital Sign", 2, - "BSA", "BSA", "Body Surface Area (m^2)", 5, "Vital Sign", 2, - "PULSE", "PULSE", "Pulse Rate (beats/min)", 6, "Vital Sign", 2, - "SYSBP", "SYSBP", "Systolic Blood Pressure (mmHg)", 7, "Vital Sign", 2, - "TEMP", "TEMP", "Temperature (C)", 8, "Vital Sign", 2 +param_lookup <- tibble::tribble( + ~VSTESTCD, ~PARAMCD, ~PARAM, ~PARAMN, ~PARCAT1, ~PARCAT1N, + "HEIGHT", "HEIGHT", "Height (cm)", 1, "Subject Characteristic", 1, + "WEIGHT", "WEIGHT", "Weight (kg)", 2, "Subject Characteristic", 1, + "DIABP", "DIABP", "Diastolic Blood Pressure (mmHg)", 3, "Vital Sign", 2, + "MAP", "MAP", "Mean Arterial Pressure (mmHg)", 4, "Vital Sign", 2, + "BSA", "BSA", "Body Surface Area (m^2)", 5, "Vital Sign", 2, + "PULSE", "PULSE", "Pulse Rate (beats/min)", 6, "Vital Sign", 2, + "SYSBP", "SYSBP", "Systolic Blood Pressure (mmHg)", 7, "Vital Sign", 2, + "TEMP", "TEMP", "Temperature (C)", 8, "Vital Sign", 2 ) attr(param_lookup$VSTESTCD, "label") <- "Vital Signs Test Short Name" ``` @@ -345,11 +345,11 @@ created with a function call. See example below for `PARAMCD` = `QTCF`. ```{r eval=FALSE} adeg <- tibble::tribble( - ~USUBJID, ~EGSTRESU, ~PARAMCD, ~AVAL, ~VISIT, - "P01", "msec", "QT", 350, "CYCLE 1 DAY 1", - "P01", "msec", "QT", 370, "CYCLE 2 DAY 1", - "P01", "msec", "RR", 842, "CYCLE 1 DAY 1", - "P01", "msec", "RR", 710, "CYCLE 2 DAY 1" + ~USUBJID, ~EGSTRESU, ~PARAMCD, ~AVAL, ~VISIT, + "P01", "msec", "QT", 350, "CYCLE 1 DAY 1", + "P01", "msec", "QT", 370, "CYCLE 2 DAY 1", + "P01", "msec", "RR", 842, "CYCLE 1 DAY 1", + "P01", "msec", "RR", 710, "CYCLE 2 DAY 1" ) adeg <- derive_param_qtc( @@ -366,11 +366,11 @@ for lab differentials converted to absolute values. See example below: ```{r eval=FALSE} adlb <- tibble::tribble( - ~USUBJID, ~PARAMCD, ~AVAL, ~PARAM, ~VISIT, - "P01", "WBC", 33, "Leukocyte Count (10^9/L)", "CYCLE 1 DAY 1", - "P01", "WBC", 38, "Leukocyte Count (10^9/L)", "CYCLE 2 DAY 1", - "P01", "LYMLE", 0.90, "Lymphocytes (fraction of 1)", "CYCLE 1 DAY 1", - "P01", "LYMLE", 0.70, "Lymphocytes (fraction of 1)", "CYCLE 2 DAY 1" + ~USUBJID, ~PARAMCD, ~AVAL, ~PARAM, ~VISIT, + "P01", "WBC", 33, "Leukocyte Count (10^9/L)", "CYCLE 1 DAY 1", + "P01", "WBC", 38, "Leukocyte Count (10^9/L)", "CYCLE 2 DAY 1", + "P01", "LYMLE", 0.90, "Lymphocytes (fraction of 1)", "CYCLE 1 DAY 1", + "P01", "LYMLE", 0.70, "Lymphocytes (fraction of 1)", "CYCLE 2 DAY 1" ) derive_param_wbc_abs( @@ -515,10 +515,10 @@ and `ref_end_date = AP01EDT`. advs_pre <- select(advs, -ONTRTFL) advs <- tibble::tribble( - ~USUBJID, ~ASTDT, ~AP01SDT, ~AP01EDT, ~AENDT, + ~USUBJID, ~ASTDT, ~AP01SDT, ~AP01EDT, ~AENDT, "P01", ymd("2020-03-15"), ymd("2020-01-01"), ymd("2020-03-01"), ymd("2020-12-01"), "P02", ymd("2019-04-30"), ymd("2020-01-01"), ymd("2020-03-01"), ymd("2020-03-15"), - "P03", ymd("2019-04-30"), ymd("2020-01-01"), ymd("2020-03-01"), NA, + "P03", ymd("2019-04-30"), ymd("2020-01-01"), ymd("2020-03-01"), NA, ) ``` @@ -852,9 +852,9 @@ We can use the `derive_vars_cat()` function to derive the categorization variabl ```{r eval=TRUE} avalcat_lookup <- exprs( - ~PARAMCD, ~condition, ~AVALCAT1, ~AVALCA1N, - "HEIGHT", AVAL > 140, ">140 cm", 1, - "HEIGHT", AVAL <= 140, "<= 140 cm", 2 + ~PARAMCD, ~condition, ~AVALCAT1, ~AVALCA1N, + "HEIGHT", AVAL > 140, ">140 cm", 1, + "HEIGHT", AVAL <= 140, "<= 140 cm", 2 ) advs <- advs %>% derive_vars_cat(