Skip to content

Commit

Permalink
Closes #2143 Loosen restrict_derivation() assertion (#2164)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Ben Straub <[email protected]>
  • Loading branch information
3 people authored Oct 11, 2023
1 parent 6b5e037 commit 095a4fd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions R/restrict_derivation.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions man/restrict_derivation.Rd

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

36 changes: 33 additions & 3 deletions tests/testthat/test-restrict_derivation.R
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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")
)
})

0 comments on commit 095a4fd

Please sign in to comment.