From 095a4fdc50bb37dccc30761f7ef48b28b023d238 Mon Sep 17 00:00:00 2001 From: Zelos Zhu Date: Wed, 11 Oct 2023 08:46:09 -0700 Subject: [PATCH] Closes #2143 Loosen `restrict_derivation()` assertion (#2164) * feat: #2145 lighten restriction rough draft * feat: #2143 run addin and add news blurb * missed a conflict * missed another * chore: #2143 minor edit to roxygen documentation * chore: #2143 adopt recommended changes * chore: #2143 address feedback --------- Co-authored-by: Zelos Zhu Co-authored-by: Ben Straub --- NEWS.md | 3 ++ R/restrict_derivation.R | 8 +---- man/restrict_derivation.Rd | 5 +--- tests/testthat/test-restrict_derivation.R | 36 +++++++++++++++++++++-- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/NEWS.md b/NEWS.md index 87f509735e..482c86be82 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,10 +4,13 @@ ## Updates of Existing Functions +- `restrict_derivation()` now allows `{dplyr}` functions like `mutate` in the `derivation argument (#2143) + - `derive_summary_records()`, `derive_var_merged_summary()`, and `get_summary_records()` were enhanced such that more than one summary variable can be derived, e.g., `AVAL` as the sum and `ADT` as the maximum of the contributing records. (#1792) + ## Breaking Changes - In `derive_summary_records()` and `get_summary_records()` the arguments diff --git a/R/restrict_derivation.R b/R/restrict_derivation.R index ac0d6d5657..c130e71a5d 100644 --- a/R/restrict_derivation.R +++ b/R/restrict_derivation.R @@ -12,9 +12,6 @@ #' The function must provide the `dataset` argument and all arguments #' specified in the `params()` objects passed to the `arg` argument. #' -#' Please note that it is not possible to specify `{dplyr}` -#' functions like `mutate()` or `summarize()`. -#' #' @param args Arguments of the derivation #' #' A `params()` object is expected. @@ -78,11 +75,8 @@ restrict_derivation <- function(dataset, filter) { # Check input assert_data_frame(dataset) - assert_function(derivation, params = c("dataset")) assert_s3_class(args, "params", optional = TRUE) - if (!is.null(args)) { - assert_function(derivation, names(args)) - } + assert_function(derivation, names(args)) filter <- assert_filter_cond(enexpr(filter)) # Split input dataset diff --git a/man/restrict_derivation.Rd b/man/restrict_derivation.Rd index 399717f856..9afa13a8fd 100644 --- a/man/restrict_derivation.Rd +++ b/man/restrict_derivation.Rd @@ -15,10 +15,7 @@ A function that performs a specific derivation is expected. A derivation adds variables or observations to a dataset. The first argument of a derivation must expect a dataset and the derivation must return a dataset. The function must provide the \code{dataset} argument and all arguments -specified in the \code{params()} objects passed to the \code{arg} argument. - -Please note that it is not possible to specify \code{{dplyr}} -functions like \code{mutate()} or \code{summarize()}.} +specified in the \code{params()} objects passed to the \code{arg} argument.} \item{args}{Arguments of the derivation diff --git a/tests/testthat/test-restrict_derivation.R b/tests/testthat/test-restrict_derivation.R index 5c8eca4258..ab908dd524 100644 --- a/tests/testthat/test-restrict_derivation.R +++ b/tests/testthat/test-restrict_derivation.R @@ -1,5 +1,5 @@ # restrict_derivation ---- -## restrict_derivation Test 1: restrict derivation with parameters ---- +## Test 1: restrict derivation with parameters ---- test_that("restrict_derivation Test 1: restrict derivation with parameters", { adlb <- tibble::tribble( ~USUBJID, ~AVISITN, ~AVAL, ~ABLFL, @@ -28,7 +28,7 @@ test_that("restrict_derivation Test 1: restrict derivation with parameters", { ) }) -## restrict_derivation Test 2: restrict derivation without parameters ---- +## Test 2: restrict derivation without parameters ---- test_that("restrict_derivation Test 2: restrict derivation without parameters", { adlb <- tibble::tribble( ~USUBJID, ~AVISITN, ~AVAL, ~ABLFL, ~BASE, @@ -55,7 +55,7 @@ test_that("restrict_derivation Test 2: restrict derivation without parameters", ) }) -## restrict_derivation Test 3: access functions from the parent environment ---- +## Test 3: access functions from the parent environment ---- test_that("restrict_derivation Test 3: access functions from the parent environment", { my_derivation <- function(dataset, new_var) { mutate( @@ -81,3 +81,33 @@ test_that("restrict_derivation Test 3: access functions from the parent environm ) }) }) + +## Test 4: allow dplyr functions ---- +test_that("restrict_derivation Test 4: allow dplyr functions", { + adlb <- tibble::tribble( + ~USUBJID, ~AVISITN, ~AVAL, + "1", -1, 113, + "1", 0, 113, + "1", 3, 117, + "2", 0, 95, + "3", 0, 111, + "3", 1, 101, + "3", 2, 123 + ) + + actual <- restrict_derivation( + adlb, + derivation = mutate, + args = params(AVAL = AVAL + 1), + filter = USUBJID == "1" + ) + + expected <- adlb %>% + mutate(AVAL = ifelse(USUBJID == "1", AVAL + 1, AVAL)) + + expect_dfs_equal( + base = expected, + compare = actual, + keys = c("USUBJID", "AVISITN") + ) +})