From c0ab881f7e33af8f0b1437f4dc0c680929b60509 Mon Sep 17 00:00:00 2001 From: Daniel Sjoberg Date: Thu, 2 Nov 2023 10:05:20 -0700 Subject: [PATCH 1/2] Exit website version article early (#2216) * Update website-versions.Rmd * Update website-versions.Rmd --- vignettes/articles/website-versions.Rmd | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/vignettes/articles/website-versions.Rmd b/vignettes/articles/website-versions.Rmd index 696c5b22a9..8b1763402b 100644 --- a/vignettes/articles/website-versions.Rmd +++ b/vignettes/articles/website-versions.Rmd @@ -17,8 +17,15 @@ base_url <- "https://pharmaverse.github.io/admiral/" # include the trailing backslash! # get list of all files in the `gh-pages` branch -df_all_files <- gert::git_ls(ref = "gh-pages") +df_all_files <- tryCatch(gert::git_ls(ref = "gh-pages"), error = function(x) FALSE) +# if a local user (not on CI) does not have a copy of the `gh-pages` branch, exit silently +if (!isTRUE(as.logical(Sys.getenv("CI"))) && isFALSE(df_all_files)) { + knitr::knit_exit() +} +``` + +```{r include=FALSE} # extract all folders in the root of the branch all_folders <- sub("/.*", "", df_all_files$path)[grepl( From 4fd22ce2ee13b7da03630fdef4955877a54304bd Mon Sep 17 00:00:00 2001 From: Jeff Dickinson Date: Fri, 3 Nov 2023 08:37:23 -0500 Subject: [PATCH 2/2] Closes #2139 Make derive_extreme_records() consistent (#2192) * #2139 chore: Make dataset_add mandatory for derive_extreme_records() * #2139 Add mandatory dataset_add to examples * #2130 Chore: Update derive_extreme_records from review comments --------- Co-authored-by: Jeffrey Dickinson --- NEWS.md | 2 + R/derive_extreme_records.R | 53 ++++++-------------- inst/templates/ad_adlb.R | 12 ++--- inst/templates/ad_advs.R | 1 + man/derive_extreme_records.Rd | 35 ++++++------- tests/testthat/test-derive_extreme_records.R | 17 +++---- vignettes/bds_finding.Rmd | 2 + 7 files changed, 47 insertions(+), 75 deletions(-) diff --git a/NEWS.md b/NEWS.md index 46306889d1..8506a67081 100644 --- a/NEWS.md +++ b/NEWS.md @@ -19,6 +19,8 @@ for the event number, which can be used in `order`. (#2140) ## Breaking Changes +- `derive_extreme_records()` the `dataset_add` argument is now mandatory. (#2139) + - In `derive_summary_records()` and `get_summary_records()` the arguments `analysis_var` and `summary_fun` were deprecated in favor of `set_values_to`. (#1792) diff --git a/R/derive_extreme_records.R b/R/derive_extreme_records.R index f63698d17e..af507e9ab5 100644 --- a/R/derive_extreme_records.R +++ b/R/derive_extreme_records.R @@ -1,18 +1,14 @@ #' Add the First or Last Observation for Each By Group as New Records #' #' Add the first or last observation for each by group as new observations. The -#' new observations can be selected from the input dataset or an additional -#' dataset. This function can be used for adding the maximum or minimum value -#' as a separate visit. All variables of the selected observation are kept. This -#' distinguishes `derive_extreme_records()` from `derive_summary_records()`, +#' new observations can be selected from the additional dataset. This function can +#' be used for adding the maximum or minimum value as a separate visit. +#' All variables of the selected observation are kept. This distinguishes +#' `derive_extreme_records()` from `derive_summary_records()`, #' where only the by variables are populated for the new records. #' #' @param dataset `r roxygen_param_dataset()` #' -#' If `dataset_add` is not specified, the new records are selected from the -#' input dataset. In this case the variables specified by `by_vars` and -#' `order` are expected. -#' #' @param dataset_ref Reference dataset #' #' The variables specified for `by_vars` are expected. For each @@ -21,18 +17,12 @@ #' #' @param dataset_add Additional dataset #' -#' Observations from the specified dataset are added as new records to the -#' input dataset (`dataset`). -#' -#' All observations in the specified dataset fulfilling the condition -#' specified by `filter_source` are considered. If `mode` and `order` are -#' specified, the first or last observation within each by group, defined by -#' `by_vars`, is selected. -#' -#' If the argument is not specified, the input dataset (`dataset`) is used. +#' The additional dataset, which determines the by groups returned in the input dataset, +#' based on the groups that exist in this dataset after being subset by `filter_add`. #' -#' The variables specified by the `by_vars` and `order` argument (if -#' applicable) are expected. +#' The variables specified in the `by_vars` and `filter_add` parameters are expected +#' in this dataset. If `mode` and `order` are specified, the first or last observation +#' within each by group, defined by `by_vars`, is selected. #' #' @param by_vars Grouping variables #' @@ -149,6 +139,7 @@ #' # Specify the variables that need to be kept in the new records. #' derive_extreme_records( #' adlb, +#' dataset_add = adlb, #' by_vars = exprs(USUBJID), #' order = exprs(AVAL, AVISITN), #' mode = "first", @@ -165,6 +156,7 @@ #' # AVISITN. Set AVISITN = 98 and DTYPE = MAXIMUM for these new records. #' derive_extreme_records( #' adlb, +#' dataset_add = adlb, #' by_vars = exprs(USUBJID), #' order = exprs(desc(AVAL), AVISITN), #' mode = "first", @@ -179,6 +171,7 @@ #' # Set AVISITN = 99 and DTYPE = LOV for these new records. #' derive_extreme_records( #' adlb, +#' dataset_add = adlb, #' by_vars = exprs(USUBJID), #' order = exprs(AVISITN), #' mode = "last", @@ -254,7 +247,7 @@ #' ) #' ) derive_extreme_records <- function(dataset = NULL, - dataset_add = NULL, + dataset_add, dataset_ref = NULL, by_vars = NULL, order = NULL, @@ -271,21 +264,13 @@ derive_extreme_records <- function(dataset = NULL, assert_expr_list(order, optional = TRUE) assert_expr_list(keep_source_vars, optional = TRUE) - if (is.null(dataset_add)) { - expected_vars <- expr_c(by_vars, extract_vars(order)) - } else { - expected_vars <- by_vars - } - assert_data_frame( dataset, - required_vars = expected_vars, optional = TRUE ) assert_data_frame( dataset_add, - required_vars = expr_c(by_vars, extract_vars(order)), - optional = TRUE + required_vars = expr_c(by_vars, extract_vars(order)) ) assert_data_frame( dataset_ref, @@ -307,18 +292,8 @@ derive_extreme_records <- function(dataset = NULL, exist_flag <- assert_symbol(enexpr(exist_flag), optional = TRUE) filter_add <- assert_filter_cond(enexpr(filter_add), optional = TRUE) assert_varval_list(set_values_to) - if (is.null(dataset) && is.null(dataset_add)) { - abort(paste( - "Neither `dataset` nor `dataset_add` is specified.", - "At least one of them must be specified.", - sep = "\n" - )) - } # Create new observations - if (is.null(dataset_add)) { - dataset_add <- dataset - } new_add_obs <- filter_if(dataset_add, filter_add) if (!is.null(order)) { diff --git a/inst/templates/ad_adlb.R b/inst/templates/ad_adlb.R index d89dc59cb2..b412ae4491 100644 --- a/inst/templates/ad_adlb.R +++ b/inst/templates/ad_adlb.R @@ -380,11 +380,11 @@ adlb <- adlb %>% adlb <- adlb %>% # get MINIMUM value derive_extreme_records( + dataset_add = adlb, by_vars = exprs(STUDYID, USUBJID, PARAMCD, BASETYPE), order = exprs(AVAL, ADT, AVISITN), mode = "first", - # "AVISITN < 9997" to evaluate only real visits - filter_add = (!is.na(AVAL) & ONTRTFL == "Y" & AVISITN < 9997), + filter_add = (!is.na(AVAL) & ONTRTFL == "Y"), set_values_to = exprs( AVISITN = 9997, AVISIT = "POST-BASELINE MINIMUM", @@ -393,11 +393,11 @@ adlb <- adlb %>% ) %>% # get MAXIMUM value derive_extreme_records( + dataset_add = adlb, by_vars = exprs(STUDYID, USUBJID, PARAMCD, BASETYPE), order = exprs(desc(AVAL), ADT, AVISITN), mode = "first", - # "AVISITN < 9997" to evaluate only real visits - filter_add = (!is.na(AVAL) & ONTRTFL == "Y" & AVISITN < 9997), + filter_add = (!is.na(AVAL) & ONTRTFL == "Y"), set_values_to = exprs( AVISITN = 9998, AVISIT = "POST-BASELINE MAXIMUM", @@ -406,11 +406,11 @@ adlb <- adlb %>% ) %>% # get LOV value derive_extreme_records( + dataset_add = adlb, by_vars = exprs(STUDYID, USUBJID, PARAMCD, BASETYPE), order = exprs(ADT, AVISITN), mode = "last", - # "AVISITN < 9997" to evaluate only real visits - filter_add = (ONTRTFL == "Y" & AVISITN < 9997), + filter_add = (ONTRTFL == "Y"), set_values_to = exprs( AVISITN = 9999, AVISIT = "POST-BASELINE LAST", diff --git a/inst/templates/ad_advs.R b/inst/templates/ad_advs.R index 85f00152c6..af0b41b3c2 100644 --- a/inst/templates/ad_advs.R +++ b/inst/templates/ad_advs.R @@ -250,6 +250,7 @@ advs <- advs %>% # Assign TRTA, TRTP # Create End of Treatment Record derive_extreme_records( + dataset_add = advs, by_vars = exprs(STUDYID, USUBJID, PARAMCD, ATPTN), order = exprs(ADT, AVISITN, AVAL), mode = "last", diff --git a/man/derive_extreme_records.Rd b/man/derive_extreme_records.Rd index 48b88a38ac..69aa892b3f 100644 --- a/man/derive_extreme_records.Rd +++ b/man/derive_extreme_records.Rd @@ -6,7 +6,7 @@ \usage{ derive_extreme_records( dataset = NULL, - dataset_add = NULL, + dataset_add, dataset_ref = NULL, by_vars = NULL, order = NULL, @@ -21,26 +21,16 @@ derive_extreme_records( ) } \arguments{ -\item{dataset}{Input dataset - -If \code{dataset_add} is not specified, the new records are selected from the -input dataset. In this case the variables specified by \code{by_vars} and -\code{order} are expected.} +\item{dataset}{Input dataset} \item{dataset_add}{Additional dataset -Observations from the specified dataset are added as new records to the -input dataset (\code{dataset}). - -All observations in the specified dataset fulfilling the condition -specified by \code{filter_source} are considered. If \code{mode} and \code{order} are -specified, the first or last observation within each by group, defined by -\code{by_vars}, is selected. - -If the argument is not specified, the input dataset (\code{dataset}) is used. +The additional dataset, which determines the by groups returned in the input dataset, +based on the groups that exist in this dataset after being subset by \code{filter_add}. -The variables specified by the \code{by_vars} and \code{order} argument (if -applicable) are expected.} +The variables specified in the \code{by_vars} and \code{filter_add} parameters are expected +in this dataset. If \code{mode} and \code{order} are specified, the first or last observation +within each by group, defined by \code{by_vars}, is selected.} \item{dataset_ref}{Reference dataset @@ -140,10 +130,10 @@ added as new observations. } \description{ Add the first or last observation for each by group as new observations. The -new observations can be selected from the input dataset or an additional -dataset. This function can be used for adding the maximum or minimum value -as a separate visit. All variables of the selected observation are kept. This -distinguishes \code{derive_extreme_records()} from \code{derive_summary_records()}, +new observations can be selected from the additional dataset. This function can +be used for adding the maximum or minimum value as a separate visit. +All variables of the selected observation are kept. This distinguishes +\code{derive_extreme_records()} from \code{derive_summary_records()}, where only the by variables are populated for the new records. } \details{ @@ -185,6 +175,7 @@ adlb <- tribble( # Specify the variables that need to be kept in the new records. derive_extreme_records( adlb, + dataset_add = adlb, by_vars = exprs(USUBJID), order = exprs(AVAL, AVISITN), mode = "first", @@ -201,6 +192,7 @@ derive_extreme_records( # AVISITN. Set AVISITN = 98 and DTYPE = MAXIMUM for these new records. derive_extreme_records( adlb, + dataset_add = adlb, by_vars = exprs(USUBJID), order = exprs(desc(AVAL), AVISITN), mode = "first", @@ -215,6 +207,7 @@ derive_extreme_records( # Set AVISITN = 99 and DTYPE = LOV for these new records. derive_extreme_records( adlb, + dataset_add = adlb, by_vars = exprs(USUBJID), order = exprs(AVISITN), mode = "last", diff --git a/tests/testthat/test-derive_extreme_records.R b/tests/testthat/test-derive_extreme_records.R index 53d34bc804..a083da8404 100644 --- a/tests/testthat/test-derive_extreme_records.R +++ b/tests/testthat/test-derive_extreme_records.R @@ -23,6 +23,7 @@ test_that("derive_extreme_records Test 1: add last observation for each group", actual_output <- derive_extreme_records( input, + dataset_add = input, order = exprs(AVISITN, LBSEQ), by_vars = exprs(USUBJID), mode = "last", @@ -299,17 +300,13 @@ test_that("derive_extreme_records Test 5: latest evaluable tumor assessment date ) }) -## Test 6: error if no input data ---- -test_that("derive_extreme_records Test 6: error if no input data", { +## Test 6: error if no dataset_add ---- +test_that("derive_extreme_records Test 6: error if no dataset_add", { expect_error( derive_extreme_records( set_values_to = exprs(PARAMCD = "HELLO") ), - regexp = paste( - "Neither `dataset` nor `dataset_add` is specified.", - "At least one of them must be specified.", - sep = "\n" - ), + regexp = "argument \"dataset_add\" is missing, with no default", fixed = TRUE ) }) @@ -339,6 +336,7 @@ test_that("derive_extreme_records Test 7: keep vars in `keep_source_vars` in the actual_output <- derive_extreme_records( input, + dataset_add = input, order = exprs(AVISITN, LBSEQ), by_vars = exprs(USUBJID), mode = "last", @@ -377,6 +375,7 @@ test_that("derive_extreme_records Test 8: keep all vars in the new records when actual_output <- derive_extreme_records( input, + dataset_add = input, order = exprs(AVISITN, LBSEQ), by_vars = exprs(USUBJID), mode = "last", @@ -391,8 +390,8 @@ test_that("derive_extreme_records Test 8: keep all vars in the new records when ) }) -## Test 10: order vars from dataset_add ---- -test_that("derive_extreme_records Test 10: order vars from dataset_add", { +## Test 9: order vars from dataset_add ---- +test_that("derive_extreme_records Test 9: order vars from dataset_add", { bds <- tibble::tribble( ~USUBJID, ~PARAMCD, ~AVALC, "1", "PARAM", "1" diff --git a/vignettes/bds_finding.Rmd b/vignettes/bds_finding.Rmd index 20e2fbdf62..05a65d5494 100644 --- a/vignettes/bds_finding.Rmd +++ b/vignettes/bds_finding.Rmd @@ -927,6 +927,7 @@ assign a unique `AVISITN` value. ```{r eval=TRUE} advs_ex1 <- advs %>% derive_extreme_records( + dataset_add = advs, by_vars = exprs(STUDYID, USUBJID, PARAMCD), order = exprs(ADT, AVISITN, ATPTN, AVAL), mode = "last", @@ -956,6 +957,7 @@ and assign a unique `AVISITN` value. ```{r eval=TRUE} advs_ex1 <- advs %>% derive_extreme_records( + dataset_add = advs, by_vars = exprs(STUDYID, USUBJID, PARAMCD), order = exprs(AVAL, ADT, AVISITN, ATPTN), mode = "first",