Skip to content

Commit

Permalink
Closes #1697 add keep_source_vars arg@devel (#2070)
Browse files Browse the repository at this point in the history
* #1697 Added `keep_vars_source` arg in `derive_extreme_records()`, added relevent unit tests and updated NEWS.md.

* Updated pkgdown.yml

* Undoing renv.lock update

* #1697 Updated default from `NULL` to `exprs(everything())`

* #1697 Updated wordlist to include 'tidyselect'

* #1697 Moved `select()` after `process_set_values_to()`

* #1697 Updated `keep_vars_source` to `keep_source_vars` in `derive_extreme_records()`

* Update NEWS.md

Co-authored-by: Zelos Zhu <[email protected]>

* Update NEWS.md

Co-authored-by: Zelos Zhu <[email protected]>

* Update R/derive_extreme_records.R

Co-authored-by: Zelos Zhu <[email protected]>

* #1697 Reverted changes made to `derive_var_merged_exist_flag()` and updated `derive_extreme_records()`

* #1697 Updated 'Details' section to include `keep_source_vars`

---------

Co-authored-by: Zelos Zhu <[email protected]>
  • Loading branch information
gg106046 and zdz2101 authored Sep 7, 2023
1 parent c16d77d commit bdcfe12
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 8 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ assessment. (#1960)
created even if values contributing to the computed values are `NA`.

- The function `derive_vars_dy()` is updated to avoid potential error when the input `dataset` with columns ending with `temp`. (#2012)
- Argument `keep_source_vars` was added to `derive_extreme_records()` which
specifies which variables in the new observations should be kept. (#1697)

- Templates, vignettes, and other uses of `{admiral.test}` SDTM data are updated to use `{pharmaversesdtm}` instead. (#2040)

Expand Down Expand Up @@ -189,6 +191,7 @@ now. This affects `derive_param_tte()`. (#1727)
- The `analysis_value` argument was enhanced such that any variable of the
form `<variable>.<parameter>` can be used, e.g., `QSORRES.CHSF13`.


## Breaking Changes

- `create_query_data()` and `derive_vars_query()` updated to rename variables in
Expand Down
26 changes: 22 additions & 4 deletions R/derive_extreme_records.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@
#'
#' *Permitted Values*: a condition
#'
#' @param keep_source_vars Variables to be kept in the new records
#'
#' A named list or tidyselect expressions created by `exprs()` defining the
#' variables to be kept for the new records. The variables specified for
#' `by_vars` and `set_values_to` need not be specified here as they are kept
#' automatically.
#'
#'
#' @inheritParams filter_extreme
#' @inheritParams derive_summary_records
#'
Expand All @@ -109,6 +117,9 @@
#' but not in the selected records are added.
#' 1. The variables specified by the `set_values_to` argument are added to
#' the selected observations.
#' 1. The variables specified by the `keep_source_vars` argument are selected
#' along with the variables specified in `by_vars` and `set_values_to`
#' arguments.
#' 1. The observations are added to input dataset.
#'
#'
Expand Down Expand Up @@ -138,12 +149,14 @@
#' # Add a new record for each USUBJID storing the minimum value (first AVAL).
#' # If multiple records meet the minimum criterion, take the first value by
#' # AVISITN. Set AVISITN = 97 and DTYPE = MINIMUM for these new records.
#' # Specify the variables that need to be kept in the new records.
#' derive_extreme_records(
#' adlb,
#' by_vars = exprs(USUBJID),
#' order = exprs(AVAL, AVISITN),
#' mode = "first",
#' filter_add = !is.na(AVAL),
#' keep_source_vars = exprs(AVAL),
#' set_values_to = exprs(
#' AVISITN = 97,
#' DTYPE = "MINIMUM"
Expand Down Expand Up @@ -254,6 +267,7 @@ derive_extreme_records <- function(dataset = NULL,
exist_flag = NULL,
true_value = "Y",
false_value = "N",
keep_source_vars = exprs(everything()),
set_values_to,
filter) {
if (!missing(filter)) {
Expand All @@ -268,6 +282,8 @@ derive_extreme_records <- function(dataset = NULL,
# Check input arguments
assert_vars(by_vars, optional = is.null(dataset_ref))
assert_expr_list(order, optional = TRUE)
assert_expr_list(keep_source_vars, optional = TRUE)

assert_data_frame(
dataset,
required_vars = expr_c(
Expand Down Expand Up @@ -333,10 +349,12 @@ derive_extreme_records <- function(dataset = NULL,
new_obs <- new_add_obs
}

new_obs <- process_set_values_to(
new_obs,
set_values_to = set_values_to
)
new_obs <- new_obs %>%
process_set_values_to(
set_values_to = set_values_to
) %>%
select(!!!by_vars, names(set_values_to), !!!keep_source_vars)


# Create output dataset
bind_rows(dataset, new_obs)
Expand Down
6 changes: 2 additions & 4 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ ATOXGR
AVAL
AVISITN
Abercrombie

Akinyi
Alkalosis
Alanine
Expand Down Expand Up @@ -184,7 +183,6 @@ Pooja
Pre
QD
QID
Quosures
README
Rahal
Ratford
Expand Down Expand Up @@ -230,6 +228,7 @@ Thanikachalam
Timepoint
ULN
USUBJID
USUBJIDs
Upadhyay
VAD
Vignesh
Expand Down Expand Up @@ -307,7 +306,6 @@ mmHg
mmol
modularized
msec
nolint
occds
onwards
parttime
Expand All @@ -325,14 +323,14 @@ rlang
roche
roxygen
scalable
str
submittable
subperiod
subperiods
subscale
summarization
th
thromboplastin
tidyselect
tidyverse
tidyselect
timeframe
Expand Down
13 changes: 13 additions & 0 deletions man/derive_extreme_records.Rd

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

77 changes: 77 additions & 0 deletions tests/testthat/test-derive_extreme_records.R
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,80 @@ test_that("derive_extreme_records Test 7: error if no input data", {
fixed = TRUE
)
})

## Test 8: keep vars in `keep_source_vars` in the new records ----
test_that("derive_extreme_records Test 8: keep vars in `keep_source_vars` in the new records", {
input <- tibble::tribble(
~USUBJID, ~AVISITN, ~AVAL, ~LBSEQ,
1, 1, 12, 1,
1, 3, 9, 2,
2, 2, 42, 1,
3, 3, 14, 1,
3, 3, 10, 2
)

expected_output <- bind_rows(
input,
tibble::tribble(
~USUBJID, ~AVISITN, ~AVAL, ~LBSEQ,
1, 3, 9, 2,
2, 2, 42, 1,
3, 3, 10, 2
) %>%
select(USUBJID, AVISITN, AVAL) %>%
mutate(DTYPE = "LOV")
)

actual_output <- derive_extreme_records(
input,
order = exprs(AVISITN, LBSEQ),
by_vars = exprs(USUBJID),
mode = "last",
keep_source_vars = exprs(AVISITN, AVAL),
set_values_to = exprs(DTYPE = "LOV")
)

expect_dfs_equal(
base = expected_output,
compare = actual_output,
keys = c("USUBJID", "AVISITN", "LBSEQ", "DTYPE")
)
})

## Test 9: keep all vars in the new records when `keep_source_vars` is 'exprs(everything())' ----
test_that("derive_extreme_records Test 9: keep all vars in the new records when `keep_source_vars` is 'exprs(everything())'", { # nolint
input <- tibble::tribble(
~USUBJID, ~AVISITN, ~AVAL, ~LBSEQ,
1, 1, 12, 1,
1, 3, 9, 2,
2, 2, 42, 1,
3, 3, 14, 1,
3, 3, 10, 2
)

expected_output <- bind_rows(
input,
tibble::tribble(
~USUBJID, ~AVISITN, ~AVAL, ~LBSEQ,
1, 3, 9, 2,
2, 2, 42, 1,
3, 3, 10, 2
) %>%
mutate(DTYPE = "LOV")
)

actual_output <- derive_extreme_records(
input,
order = exprs(AVISITN, LBSEQ),
by_vars = exprs(USUBJID),
mode = "last",
keep_source_vars = exprs(everything()),
set_values_to = exprs(DTYPE = "LOV")
)

expect_dfs_equal(
base = expected_output,
compare = actual_output,
keys = c("USUBJID", "AVISITN", "LBSEQ", "DTYPE")
)
})

0 comments on commit bdcfe12

Please sign in to comment.