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 #2125 add true/false_value arguments for appropriate functions #2136

Merged
merged 37 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
1831431
feat: #2125 intial draft of new truefalse val args
Sep 27, 2023
6c2bfea
add word to globals
Sep 27, 2023
8d179bf
chore: #2125 address cicd checks
Sep 27, 2023
85d89c0
feat: #2125 add test for t/f value for derive var extreme flag
Sep 27, 2023
92bc048
feat: #2125 add test for derive_merged truefalse value
Sep 27, 2023
b8e4f03
chore: #2125 address rcmd error
Sep 27, 2023
179283d
address errors
Sep 28, 2023
48861ae
address styler
Sep 28, 2023
d56b235
feat: #2125 change default true/false to "Y"/NA
Sep 28, 2023
de1306e
chore: #2125 add news blurb
Sep 28, 2023
03d93d6
catch up to main/address merge conflicts
Oct 6, 2023
2d70d69
address merge conflicts
Oct 6, 2023
8d27e82
forgot to remove a snippet
Oct 6, 2023
2281ff5
Merge branch 'main' into 2125_add_truefalse_value_flags
zdz2101 Oct 9, 2023
b0f7f74
Merge branch 'main' into 2125_add_truefalse_value_flags
zdz2101 Oct 11, 2023
0274688
feat: #2125 add T/F values for derive_vars_joined as well
Oct 11, 2023
466cd05
feat: #2125 align derive_extreme_records too and fix test suite
Oct 11, 2023
15ea0f0
did that fix it
Oct 11, 2023
729bef8
implications of this
Oct 11, 2023
d8cfbf4
feat: #2125 add back exist_flag toggle
Oct 11, 2023
4f49476
Merge branch 'main' into 2125_add_truefalse_value_flags
zdz2101 Oct 11, 2023
bd50f22
update news blurb
Oct 11, 2023
1ada1e6
fix the rlang stuff
Oct 12, 2023
c5a6ae2
less code modification
Oct 12, 2023
18d4119
add news blurb
Oct 12, 2023
f34ae89
fix documentation
Oct 12, 2023
13ea0a6
feat: #2125 separate exist_flag and missing_value functionality
Oct 12, 2023
9b4172f
super edge case to account for in feedback
Oct 12, 2023
d7d2f0c
revert renaming of the temp flag for lookup
Oct 12, 2023
b384bcc
run styler
Oct 12, 2023
92aaf07
cleaner code changes
Oct 12, 2023
368f4af
lintr
Oct 12, 2023
90a77e3
chore: #2125 change news blurb and fix deprecation messaging
Oct 13, 2023
e3453e7
cleaner to read
Oct 13, 2023
95e398d
chore: #2125 use tmp var for derive_vars_merged_lookup
Oct 13, 2023
458096b
#2125 add_truefalse_value_flags: simplify code
bundfussr Oct 13, 2023
e2fb3ae
feat: #2125 styler & add test for deprecation warning
Oct 13, 2023
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
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## Updates of Existing Functions

- `derive_extreme_records()`, `derive_var_extreme_flag()`,`derive_vars_joined()` and `derive_vars_merged()` were enhanced with the arguments `true_value` and `false_value` to align with preexisting functions that had similar functionality (#2125)

- `restrict_derivation()` now allows `{dplyr}` functions like `mutate` in the `derivation argument (#2143)

- `derive_summary_records()`, `derive_var_merged_summary()`, and `get_summary_records()`
Expand All @@ -22,6 +24,10 @@ were enhanced such that more than one summary variable can be derived, e.g.,

- admiral now only supports R >= 4.0.0

- For the function `derive_vars_merged()`, the argument `match_flag` was renamed to `exist_flag` (#2125)

bundfussr marked this conversation as resolved.
Show resolved Hide resolved
- The default value for the `false_value` argument in `derive_extreme_records()` was changed to `NA_character_` (#2125)

- The following functions, which were deprecated in previous `{admiral}` versions, have been removed: (#2098)
- `derive_param_extreme_event()`
- `derive_vars_last_dose()`
Expand Down
2 changes: 1 addition & 1 deletion R/derive_extreme_records.R
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ derive_extreme_records <- function(dataset = NULL,
check_type = "warning",
exist_flag = NULL,
true_value = "Y",
false_value = "N",
false_value = NA_character_,
keep_source_vars = exprs(everything()),
set_values_to) {
# Check input arguments
Expand Down
6 changes: 6 additions & 0 deletions R/derive_joined.R
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ derive_vars_joined <- function(dataset,
filter_add = NULL,
filter_join = NULL,
mode = NULL,
exist_flag = NULL,
true_value = "Y",
false_value = NA_character_,
missing_values = NULL,
check_type = "warning") {
assert_vars(by_vars, optional = TRUE)
Expand Down Expand Up @@ -423,6 +426,9 @@ derive_vars_joined <- function(dataset,
new_vars = add_suffix_to_vars(new_vars, vars = common_vars, suffix = ".join"),
missing_values = missing_values,
check_type = check_type,
exist_flag = !!enexpr(exist_flag),
true_value = true_value,
false_value = false_value,
duplicate_msg = paste(
paste(
"After applying `filter_join` the joined dataset contains more",
Expand Down
72 changes: 59 additions & 13 deletions R/derive_merged.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,38 @@
#'
#' @param match_flag Match flag
#'
#' `r lifecycle::badge("deprecated")` Please use `exist_flag` instead.
#'
#' If the argument is specified (e.g., `match_flag = FLAG`), the specified
#' variable (e.g., `FLAG`) is added to the input dataset. This variable will
#' be `TRUE` for all selected records from `dataset_add` which are merged into
#' the input dataset, and `NA` otherwise.
#'
#' *Permitted Values*: Variable name
#'
#' @param exist_flag Exist flag
#'
#' If the argument is specified (e.g., `exist_flag = FLAG`), the specified
#' variable (e.g., `FLAG`) is added to the input dataset. This variable will
#' be the value provided in `true_value` for all selected records from `dataset_add`
#' which are merged into the input dataset, and the value provided in `false_value` otherwise.
#'
#' *Permitted Values*: Variable name
#'
#' @param true_value True value
#'
#' The value for the specified variable `exist_flag`, applicable to
#' the first or last observation (depending on the mode) of each by group.
#'
#' Permitted Values: An atomic scalar
#'
#' @param false_value False value
#'
#' The value for the specified variable `exist_flag`, NOT applicable to
#' the first or last observation (depending on the mode) of each by group.
#'
#' Permitted Values: An atomic scalar
#'
#' @param missing_values Values for non-matching observations
#'
#' For observations of the input dataset (`dataset`) which do not have a
Expand Down Expand Up @@ -198,7 +223,7 @@
#' mode = "last",
#' new_vars = exprs(LASTWGT = VSSTRESN, LASTWGTU = VSSTRESU),
#' filter_add = VSTESTCD == "WEIGHT",
#' match_flag = vsdatafl
#' exist_flag = vsdatafl
#' ) %>%
#' select(STUDYID, USUBJID, AGE, AGEU, LASTWGT, LASTWGTU, vsdatafl)
#'
Expand Down Expand Up @@ -282,7 +307,10 @@ derive_vars_merged <- function(dataset,
new_vars = NULL,
filter_add = NULL,
mode = NULL,
match_flag = NULL,
match_flag,
exist_flag = NULL,
true_value = "Y",
false_value = NA_character_,
missing_values = NULL,
check_type = "warning",
duplicate_msg = NULL) {
Expand All @@ -301,7 +329,17 @@ derive_vars_merged <- function(dataset,
extract_vars(new_vars)
)
)
match_flag <- assert_symbol(enexpr(match_flag), optional = TRUE)
if (!is_missing(enexpr(match_flag))) {
deprecate_warn(
"1.0.0",
"derive_vars_merged(match_flag =)",
"derive_vars_merged(exist_flag =)"
)
exist_flag <- assert_symbol(enexpr(match_flag), optional = TRUE)
}
exist_flag <- assert_symbol(enexpr(exist_flag), optional = TRUE)
assert_atomic_vector(true_value, optional = TRUE)
assert_atomic_vector(false_value, optional = TRUE)
assert_expr_list(missing_values, named = TRUE, optional = TRUE)
if (!is.null(missing_values)) {
invalid_vars <- setdiff(
Expand Down Expand Up @@ -347,18 +385,18 @@ derive_vars_merged <- function(dataset,
select(!!!by_vars_right, !!!replace_values_by_names(new_vars))
}

if (!is.null(missing_values)) {
if (!is.null(missing_values) || !is.null(exist_flag)) {
match_flag_var <- get_new_tmp_var(add_data, prefix = "tmp_match_flag")
} else {
match_flag_var <- match_flag
match_flag_var <- NULL
}

if (!is.null(match_flag_var)) {
add_data <- mutate(
add_data,
!!match_flag_var := TRUE
)
}

# check if there are any variables in both datasets which are not by vars
# in this case an error is issued to avoid renaming of varibles by left_join()
common_vars <-
Expand All @@ -382,18 +420,24 @@ derive_vars_merged <- function(dataset,
}
dataset <- left_join(dataset, add_data, by = vars2chr(by_vars))

if (!is.null(missing_values)) {
if (!is.null(match_flag_var)) {
update_missings <- map2(
syms(names(missing_values)),
missing_values,
~ expr(if_else(is.na(!!match_flag_var), !!.y, !!.x))
)
names(update_missings) <- names(missing_values)
dataset <- dataset %>%
mutate(!!!update_missings) %>%
remove_tmp_vars()
mutate(!!!update_missings)
}
dataset

if (!is.null(exist_flag)) {
dataset <- dataset %>%
mutate(!!exist_flag := ifelse(is.na(!!match_flag_var), false_value, true_value))
}

dataset %>%
remove_tmp_vars()
}

#' Merge an Existence Flag
Expand Down Expand Up @@ -651,6 +695,8 @@ derive_vars_merged_lookup <- function(dataset,
assert_logical_scalar(print_not_mapped)
filter_add <- assert_filter_cond(enexpr(filter_add), optional = TRUE)

tmp_lookup_flag <- get_new_tmp_var(dataset_add, prefix = "tmp_lookup_flag")

res <- derive_vars_merged(
dataset,
dataset_add,
Expand All @@ -659,14 +705,14 @@ derive_vars_merged_lookup <- function(dataset,
new_vars = new_vars,
mode = mode,
filter_add = !!filter_add,
match_flag = temp_match_flag,
exist_flag = !!tmp_lookup_flag,
check_type = check_type,
duplicate_msg = duplicate_msg
)

if (print_not_mapped) {
temp_not_mapped <- res %>%
filter(is.na(temp_match_flag)) %>%
filter(is.na(!!tmp_lookup_flag)) %>%
distinct(!!!by_vars_left)

if (nrow(temp_not_mapped) > 0) {
Expand All @@ -688,7 +734,7 @@ derive_vars_merged_lookup <- function(dataset,
}
}

res %>% select(-temp_match_flag)
res %>% remove_tmp_vars()
}

#' Get list of records not mapped from the lookup table.
Expand Down
26 changes: 22 additions & 4 deletions R/derive_var_extreme_flag.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#'
#' @param new_var Variable to add
#'
#' The specified variable is added to the output dataset. It is set to `"Y"`
#' for the first or last observation (depending on the mode) of each by group.
#' The specified variable is added to the output dataset. It is set to the value
#' set in `true_value` for the first or last observation (depending on the mode) of each by group.
#'
#' Permitted Values: list of name-value pairs
#'
Expand All @@ -25,6 +25,20 @@
#'
#' Permitted Values: `"first"`, `"last"`
#'
#' @param true_value True value
#'
#' The value for the specified variable `new_var`, applicable to
#' the first or last observation (depending on the mode) of each by group.
#'
#' Permitted Values: An atomic scalar
#'
#' @param false_value False value
#'
#' The value for the specified variable `new_var`, NOT applicable to
#' the first or last observation (depending on the mode) of each by group.
#'
#' Permitted Values: An atomic scalar
#'
#' @param flag_all Flag setting
#'
#' A logical value where if set to `TRUE`, all records are flagged
Expand Down Expand Up @@ -232,13 +246,17 @@ derive_var_extreme_flag <- function(dataset,
order,
new_var,
mode,
true_value = "Y",
false_value = NA_character_,
flag_all = FALSE,
check_type = "warning") {
new_var <- assert_symbol(enexpr(new_var))
assert_vars(by_vars)
assert_expr_list(order)
assert_data_frame(dataset, required_vars = exprs(!!!by_vars, !!!extract_vars(order)))
mode <- assert_character_scalar(mode, values = c("first", "last"), case_sensitive = FALSE)
assert_atomic_vector(true_value, optional = TRUE)
assert_atomic_vector(false_value, optional = TRUE)
flag_all <- assert_logical_scalar(flag_all)
check_type <- assert_character_scalar(
check_type,
Expand All @@ -263,11 +281,11 @@ derive_var_extreme_flag <- function(dataset,

if (mode == "first") {
data <- data %>%
mutate(!!new_var := if_else(!!tmp_obs_nr == 1, "Y", NA_character_))
mutate(!!new_var := if_else(!!tmp_obs_nr == 1, true_value, false_value))
} else {
data <- data %>%
group_by(!!!by_vars) %>%
mutate(!!new_var := if_else(!!tmp_obs_nr == n(), "Y", NA_character_)) %>%
mutate(!!new_var := if_else(!!tmp_obs_nr == n(), true_value, false_value)) %>%
ungroup()
}

Expand Down
1 change: 0 additions & 1 deletion R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ globalVariables(c(
"temp_DT",
"temp_from_var",
"temp_to_var",
"temp_match_flag",
"temp_dose_freq",
"temp_new_dose_no",
"temp_num_of_doses",
Expand Down
2 changes: 1 addition & 1 deletion man/derive_extreme_records.Rd

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

20 changes: 18 additions & 2 deletions man/derive_var_extreme_flag.Rd

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

26 changes: 26 additions & 0 deletions man/derive_vars_joined.Rd

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

Loading
Loading