diff --git a/R/derive_param_exposure.R b/R/derive_param_exposure.R index d1e8b1d564..27cb9bdeee 100644 --- a/R/derive_param_exposure.R +++ b/R/derive_param_exposure.R @@ -8,6 +8,13 @@ #' `PARAMCD` is expected as well, #' + Either `ASTDTM` and `AENDTM` or `ASTDT` and `AENDT` are also expected. #' +#' @param dataset_add Additional dataset +#' +#' The variables specified for `by_vars` are expected. +#' Observations from the specified dataset are going to be used to calculate and added +#' as new records to the input dataset (`dataset`). +#' +#' #' @param filter Filter condition #' #' The specified condition is applied to the input dataset before deriving the @@ -95,6 +102,7 @@ #' # Cumulative dose #' adex %>% #' derive_param_exposure( +#' dataset_add = adex, #' by_vars = exprs(USUBJID), #' set_values_to = exprs(PARAMCD = "TDOSE", PARCAT1 = "OVERALL"), #' input_code = "DOSE", @@ -106,6 +114,7 @@ #' # average dose in w2-24 #' adex %>% #' derive_param_exposure( +#' dataset_add = adex, #' by_vars = exprs(USUBJID), #' filter = VISIT %in% c("WEEK 2", "WEEK 24"), #' set_values_to = exprs(PARAMCD = "AVDW224", PARCAT1 = "WEEK2-24"), @@ -118,6 +127,7 @@ #' # Any dose adjustment? #' adex %>% #' derive_param_exposure( +#' dataset_add = adex, #' by_vars = exprs(USUBJID), #' set_values_to = exprs(PARAMCD = "TADJ", PARCAT1 = "OVERALL"), #' input_code = "ADJ", diff --git a/R/derive_summary_records.R b/R/derive_summary_records.R index a9bece644a..ea4ffeb7b1 100644 --- a/R/derive_summary_records.R +++ b/R/derive_summary_records.R @@ -112,6 +112,7 @@ #' # Summarize the average of the triplicate ECG interval values (AVAL) #' derive_summary_records( #' adeg, +#' dataset_add = adeg, #' by_vars = exprs(USUBJID, PARAM, AVISIT), #' set_values_to = exprs( #' AVAL = mean(AVAL, na.rm = TRUE), @@ -123,6 +124,7 @@ #' # Derive more than one summary variable #' derive_summary_records( #' adeg, +#' dataset_add = adeg, #' by_vars = exprs(USUBJID, PARAM, AVISIT), #' set_values_to = exprs( #' AVAL = mean(AVAL), @@ -156,6 +158,7 @@ #' # by group #' derive_summary_records( #' adeg, +#' dataset_add = adeg, #' by_vars = exprs(USUBJID, PARAM, AVISIT), #' filter = n() > 2, #' set_values_to = exprs( @@ -172,17 +175,11 @@ derive_summary_records <- function(dataset, filter_add = NULL, analysis_var, summary_fun, - set_values_to = NULL, + set_values_to, missing_values = NULL) { assert_vars(by_vars) - assert_data_frame( - dataset, - required_vars = expr_c(by_vars) - ) - assert_data_frame( - dataset_add, - required_vars = expr_c(by_vars), - ) + assert_data_frame(dataset, required_vars = by_vars) + assert_data_frame(dataset_add, required_vars = by_vars) assert_data_frame( dataset_ref, required_vars = by_vars, diff --git a/man/derive_param_exposure.Rd b/man/derive_param_exposure.Rd index 4073aa867c..21f11a511f 100644 --- a/man/derive_param_exposure.Rd +++ b/man/derive_param_exposure.Rd @@ -25,6 +25,12 @@ The variables specified by the \code{by_vars} and \code{analysis_var} argument(s \item Either \code{ASTDTM} and \code{AENDTM} or \code{ASTDT} and \code{AENDT} are also expected. }} +\item{dataset_add}{Additional dataset + +The variables specified for \code{by_vars} are expected. +Observations from the specified dataset are going to be used to calculate and added +as new records to the input dataset (\code{dataset}).} + \item{by_vars}{Grouping variables For each group defined by \code{by_vars} an observation is added to the output @@ -119,6 +125,7 @@ adex <- tribble( # Cumulative dose adex \%>\% derive_param_exposure( + dataset_add = adex, by_vars = exprs(USUBJID), set_values_to = exprs(PARAMCD = "TDOSE", PARCAT1 = "OVERALL"), input_code = "DOSE", @@ -130,6 +137,7 @@ adex \%>\% # average dose in w2-24 adex \%>\% derive_param_exposure( + dataset_add = adex, by_vars = exprs(USUBJID), filter = VISIT \%in\% c("WEEK 2", "WEEK 24"), set_values_to = exprs(PARAMCD = "AVDW224", PARCAT1 = "WEEK2-24"), @@ -142,6 +150,7 @@ adex \%>\% # Any dose adjustment? adex \%>\% derive_param_exposure( + dataset_add = adex, by_vars = exprs(USUBJID), set_values_to = exprs(PARAMCD = "TADJ", PARCAT1 = "OVERALL"), input_code = "ADJ", diff --git a/man/derive_summary_records.Rd b/man/derive_summary_records.Rd index cca5c59232..ecad30758f 100644 --- a/man/derive_summary_records.Rd +++ b/man/derive_summary_records.Rd @@ -13,7 +13,7 @@ derive_summary_records( filter_add = NULL, analysis_var, summary_fun, - set_values_to = NULL, + set_values_to, missing_values = NULL ) } @@ -86,7 +86,7 @@ For example: \item{missing_values}{Values for missing summary values -For observations of the input dataset (\code{dataset}) or (\code{dataset_add}) which do not have an +For observations of the input dataset (\code{dataset}) or (\code{dataset_add}) which do not have a complete mapping defined by the summarization defined in \code{set_values_to}. Only variables specified for \code{set_values_to} can be specified for \code{missing_values}. @@ -139,6 +139,7 @@ adeg <- tribble( # Summarize the average of the triplicate ECG interval values (AVAL) derive_summary_records( adeg, + dataset_add = adeg, by_vars = exprs(USUBJID, PARAM, AVISIT), set_values_to = exprs( AVAL = mean(AVAL, na.rm = TRUE), @@ -150,6 +151,7 @@ derive_summary_records( # Derive more than one summary variable derive_summary_records( adeg, + dataset_add = adeg, by_vars = exprs(USUBJID, PARAM, AVISIT), set_values_to = exprs( AVAL = mean(AVAL), @@ -183,6 +185,7 @@ adeg <- tribble( # by group derive_summary_records( adeg, + dataset_add = adeg, by_vars = exprs(USUBJID, PARAM, AVISIT), filter = n() > 2, set_values_to = exprs( diff --git a/tests/testthat/test-compute_scale.R b/tests/testthat/test-compute_scale.R index 74ae6d7ec9..0db6250394 100644 --- a/tests/testthat/test-compute_scale.R +++ b/tests/testthat/test-compute_scale.R @@ -95,6 +95,7 @@ test_that("compute_scale Test 5: compute_scale() works as expected within expect_equal( derive_summary_records( input, + dataset_add = input, by_vars = exprs(STUDYID, USUBJID, AVISIT, AVISITN), filter_add = (PARAMCD %in% c("ITEM1", "ITEM2", "ITEM3")), set_values_to = exprs( diff --git a/tests/testthat/test-derive_param_exposure.R b/tests/testthat/test-derive_param_exposure.R index 6b5c7c7ae3..ca80f75a60 100644 --- a/tests/testthat/test-derive_param_exposure.R +++ b/tests/testthat/test-derive_param_exposure.R @@ -128,6 +128,7 @@ test_that("derive_param_exposure Test 2: works with DT variables", { actual_output <- input_no_dtm %>% derive_param_exposure( + dataset_add = input_no_dtm, by_vars = exprs(USUBJID), input_code = "DOSE", analysis_var = AVAL, @@ -135,6 +136,7 @@ test_that("derive_param_exposure Test 2: works with DT variables", { set_values_to = exprs(PARAMCD = "TDOSE", PARCAT1 = "OVERALL") ) %>% derive_param_exposure( + dataset_add = input_no_dtm, by_vars = exprs(USUBJID), input_code = "DOSE", analysis_var = AVAL, @@ -142,6 +144,7 @@ test_that("derive_param_exposure Test 2: works with DT variables", { set_values_to = exprs(PARAMCD = "AVDOSE", PARCAT1 = "OVERALL") ) %>% derive_param_exposure( + dataset_add = input_no_dtm, by_vars = exprs(USUBJID), input_code = "ADJ", analysis_var = AVALC, @@ -162,6 +165,7 @@ test_that("derive_param_exposure Test 3: Errors", { expect_error( input <- input %>% derive_param_exposure( + dataset_add = input, by_vars = exprs(USUBJID), input_code = "DOSE", analysis_var = AVAL, @@ -174,6 +178,7 @@ test_that("derive_param_exposure Test 3: Errors", { expect_error( input <- input %>% derive_param_exposure( + dataset_add = input, by_vars = exprs(USUBJID), input_code = "DOSED", analysis_var = AVAL, @@ -191,6 +196,8 @@ test_that("derive_param_exposure Test 3: Errors", { input <- input %>% select(-starts_with("AST"), -starts_with("AEN")) %>% derive_param_exposure( + dataset = ., + dataset_add = ., by_vars = exprs(USUBJID), input_code = "DOSE", analysis_var = AVAL, diff --git a/vignettes/bds_exposure.Rmd b/vignettes/bds_exposure.Rmd index 0258e1fc8a..fd88cd7913 100644 --- a/vignettes/bds_exposure.Rmd +++ b/vignettes/bds_exposure.Rmd @@ -361,6 +361,7 @@ For example, to calculate the total dose by subject and treatment, ```{r eval=TRUE, echo=TRUE} adex <- derive_param_exposure( adex, + dataset_add = adex, by_vars = exprs(STUDYID, USUBJID, !!!adsl_vars), input_code = "DOSE", analysis_var = AVAL, @@ -428,6 +429,7 @@ adex <- adex %>% summary_fun = function(x) if_else(sum(!is.na(x)) > 0, "Y", NA_character_) ) ), + dataset_add = adex, by_vars = exprs(STUDYID, USUBJID, !!!adsl_vars) ) diff --git a/vignettes/bds_finding.Rmd b/vignettes/bds_finding.Rmd index 20e2fbdf62..64e13c31bc 100644 --- a/vignettes/bds_finding.Rmd +++ b/vignettes/bds_finding.Rmd @@ -989,9 +989,10 @@ Set `DTYPE` to `AVERAGE`. ```{r eval=TRUE} advs_ex2 <- derive_summary_records( advs, + dataset_add = advs, by_vars = exprs(STUDYID, USUBJID, PARAMCD, VISITNUM, ADT), set_values_to = exprs( - AVAL = mean(AVAL), + AVAL = mean(AVAL, na.rm = TRUE), DTYPE = "AVERAGE" ) ) diff --git a/vignettes/questionnaires.Rmd b/vignettes/questionnaires.Rmd index dc24569f09..652c0036e0 100644 --- a/vignettes/questionnaires.Rmd +++ b/vignettes/questionnaires.Rmd @@ -141,6 +141,8 @@ adgad7 <- adqs %>% # Select records to keep in the GAD-7 ADaM filter(PARCAT1 == "GAD-7 V2") %>% derive_summary_records( + dataset = ., + dataset_add = ., by_vars = exprs(STUDYID, USUBJID, AVISIT, ADT, ADY, TRTSDT, DTHCAUS), # Select records contributing to total score filter_add = str_detect(PARAMCD, "GAD020[1-7]"), @@ -169,6 +171,8 @@ adgdssf <- adqs %>% # Select records to keep in the GDS-SF ADaM filter(PARCAT1 == "GDS SHORT FORM") %>% derive_summary_records( + dataset = ., + dataset_add = ., by_vars = exprs(STUDYID, USUBJID, AVISIT, ADT, ADY, TRTSDT, DTHCAUS), # Select records contributing to total score filter_add = str_detect(PARAMCD, "GDS02[01][0-9]"), @@ -518,6 +522,7 @@ be derived by `derive_summary_records()`. ```{r} adgdssf <- adgdssf %>% derive_summary_records( + dataset_add = adgdssf, filter_add = str_detect(PARAMCD, "GDS02[01][0-9]"), by_vars = exprs(USUBJID, AVISIT), set_values_to = exprs( @@ -567,6 +572,8 @@ adgdssf <- adgdssf %>% ) ) %>% derive_summary_records( + dataset = ., + dataset_add = ., filter_add = str_detect(PARAMCD, "GDS02[01][0-9]"), by_vars = exprs(USUBJID, AVISIT), set_values_to = exprs(