Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #2012_derive_vars_dy #2013

Merged
merged 8 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

- The function `derive_var_extreme_flag()` has a new function argument, `flag_all` that additionally flags all records if the first or last record is not unique. (#1979)

- The function `derive_vars_dy()` is updated to avoid potential error when the input `dataset` with columns ending with `temp`. (#2012)


## Breaking Changes
- The following functions, which were deprecated in previous `{admiral}` versions, have been removed: (#1950)

Expand Down
32 changes: 11 additions & 21 deletions R/derive_vars_dy.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,21 @@ derive_vars_dy <- function(dataset,
abort(err_msg)
}

dy_vars <- if_else(
# named vector passed to `.names` in `across()` to derive name of dy_vars
dy_vars <- set_names(if_else(
source_names == "",
str_replace_all(vars2chr(source_vars), "(DT|DTM)$", "DY"),
source_names
)
), vars2chr(source_vars))

warn_if_vars_exist(dataset, dy_vars)

if (n_vars > 1L) {
dataset %>%
mutate(
across(
.cols = vars2chr(unname(source_vars)),
.fns = list(temp = ~
compute_duration(start_date = !!reference_date, end_date = .))
)
) %>%
rename_with(
.cols = ends_with("temp"),
.fn = ~dy_vars
)
} else {
dataset %>%
mutate(
!!sym(dy_vars) :=
compute_duration(start_date = !!reference_date, end_date = !!source_vars[[1]])
dataset %>%
mutate(
across(
.cols = vars2chr(unname(source_vars)),
.fns = ~ compute_duration(start_date = !!reference_date, end_date = .x),
.names = "{dy_vars}"
)
}
)
}
32 changes: 32 additions & 0 deletions tests/testthat/test-derive_vars_dy.R
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,35 @@ test_that("derive_vars_dy Test 9: Single named --DT input when ref date is --DTM
keys = c("STUDYID", "USUBJID")
)
})

## Test 10: no error if input with variable end with `_temp` ----
test_that("derive_vars_dy Test 10: no error if input with variable end with `_temp`", {
datain <- tibble::tribble(
~STUDYID, ~USUBJID, ~TRTSDTM, ~ASTDT, ~test_temp,
"TEST01", "PAT01", "2014-01-17T23:59:59", "2014-01-18", "test"
) %>%
mutate(
TRTSDTM = lubridate::as_datetime(TRTSDTM),
ASTDT = lubridate::ymd(ASTDT)
)

expected_output <- tibble::tribble(
~STUDYID, ~USUBJID, ~TRTSDTM, ~ASTDT, ~test_temp, ~ASTDY,
"TEST01", "PAT01", "2014-01-17T23:59:59", "2014-01-18", "test", 2
) %>%
mutate(
TRTSDTM = lubridate::as_datetime(TRTSDTM),
ASTDT = lubridate::ymd(ASTDT)
)

actual_output <- derive_vars_dy(datain,
reference_date = TRTSDTM,
source_vars = exprs(ASTDT)
)

expect_dfs_equal(
expected_output,
actual_output,
keys = c("STUDYID", "USUBJID")
)
})
Loading