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 #2142 Supersede get_summary_records() and enhance derive_summary_records() #2158

Merged
merged 43 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
1b7e9ab
feat: #2142 intiial superseding step
Oct 6, 2023
c06bc8c
Merge branch 'main' into 2142_derive_summary_records_get_summary_reco…
zdz2101 Oct 6, 2023
6050dc9
rough draft without missing_values implementation
Oct 6, 2023
13cbdba
Merge branch '2142_derive_summary_records_get_summary_records_mods' o…
Oct 6, 2023
836cb0a
rough draft of new enhancement
Oct 6, 2023
e5a01a3
Merge branch 'main' into 2142_derive_summary_records_get_summary_reco…
zdz2101 Oct 13, 2023
0e6ec6c
feat: #2142 get a good clean slate
Oct 24, 2023
70836b5
Merge branch 'main' into 2142_derive_summary_records_get_summary_reco…
zdz2101 Oct 24, 2023
346f595
feat: #2142 working enhanced function
Oct 26, 2023
19001b5
Merge branch 'main' into 2142_derive_summary_records_get_summary_reco…
zdz2101 Oct 26, 2023
5a6263e
feat: #2142 add appropriate test suite
Oct 26, 2023
843d852
feat: #2142 run styler, lintr, add news and roxygen documentation
Oct 26, 2023
2ea2c50
chore: #2142 spelling/grammar
Oct 26, 2023
252e3c2
chore: #2142 fix test
Oct 26, 2023
bf9470f
should we inform superseded
Oct 26, 2023
1adf60c
retain deprecated arguments to pass cicd
Oct 26, 2023
7ba57b6
chore: #2142 add remotes for admiraldev for proper branching strategy
Oct 27, 2023
fc7e5f3
min dev versioning
Oct 27, 2023
ef2ac94
Update DESCRIPTION
ddsjoberg Oct 27, 2023
4210f7e
Merge branch 'main' into 2142_derive_summary_records_get_summary_reco…
zdz2101 Oct 31, 2023
053a419
chore: #2142 address feedback
Oct 31, 2023
0b4ef60
upversion our description page to match current version up on github
jerryekohe Oct 31, 2023
b61cc15
Merge branch 'main' into 2142_derive_summary_records_get_summary_reco…
zdz2101 Nov 2, 2023
d27bba1
Merge branch 'main' into 2142_derive_summary_records_get_summary_reco…
bms63 Nov 2, 2023
6ee8500
docs: little note for running website versions
bms63 Nov 2, 2023
19ced77
feat: #2142 rename filter to filter_add
zdz2101 Nov 3, 2023
48cbbc6
Update R/derive_summary_records.R
zdz2101 Nov 3, 2023
53bdf63
feat: #2142 get checks appropriately running
zdz2101 Nov 3, 2023
affe938
Merge branch '2142_derive_summary_records_get_summary_records_mods' o…
zdz2101 Nov 3, 2023
a15e58a
roxygen stuff and vignettes
zdz2101 Nov 3, 2023
00ae97a
Merge branch 'main' into 2142_derive_summary_records_get_summary_reco…
zdz2101 Nov 3, 2023
5c690f7
chore: #2142 roxygen stuff
zdz2101 Nov 3, 2023
01bc54d
get past check-templates
zdz2101 Nov 3, 2023
0f7a8ab
finally get past templates
zdz2101 Nov 3, 2023
eaf9892
feat: #2142 clear up missing_values usage
zdz2101 Nov 6, 2023
5c3748a
chore: #2142 rename filter to filter_add internally in codebase too
zdz2101 Nov 6, 2023
89a673e
chore: #2142 adopt and address all other feedback
zdz2101 Nov 6, 2023
3794c1d
Merge branch 'main' into 2142_derive_summary_records_get_summary_reco…
zdz2101 Nov 6, 2023
85f4f9e
missed a renaming
zdz2101 Nov 6, 2023
b3fc993
feat: #2142 remove extra fluff for missing values
zdz2101 Nov 7, 2023
2f24282
update news blurb
zdz2101 Nov 7, 2023
b6d61b6
Merge branch 'main' into 2142_derive_summary_records_get_summary_reco…
zdz2101 Nov 7, 2023
a0dc08a
chore: #2142 update documentation based on feedback
zdz2101 Nov 8, 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
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Depends: R (>= 4.0)
Remotes: pharmaverse/admiraldev
Imports:
admiraldev (>= 0.4.0),
admiraldev (>= 0.5.0.9000),
dplyr (>= 0.8.4),
hms (>= 0.5.3),
lifecycle (>= 0.1.0),
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
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)

- `derive_summary_records()` was enhanced with the following optional arguments: `dataset_add`, `dataset_ref`, `missing_values`. These arguments respectively, generate summary variables from additional datasets, retain/add specific records from a reference dataset, and impute user-defined missing values. (#2142)

bundfussr marked this conversation as resolved.
Show resolved Hide resolved
- The `tmp_event_nr_var` argument was added to `derive_extreme_records()` to
allow more control of the selection of records. It creates a temporary variable
for the event number, which can be used in `order`. (#2140)


## Breaking Changes

- In `derive_summary_records()` and `get_summary_records()` the arguments
Expand Down
110 changes: 99 additions & 11 deletions R/derive_summary_records.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,19 @@
#' retain those common values in the newly derived records. Otherwise new value
#' will be set to `NA`.
#'
#' @param dataset `r roxygen_param_dataset(expected_vars = c("by_vars", "analysis_var"))`
#' @param dataset `r roxygen_param_dataset(expected_vars = c("by_vars"))`
#'
#' @param dataset_add Additional dataset
#'
#' The variables specified for `by_vars` are expected.
#' Observations from the specified dataset are going to be used to calculate and added
#' as new records to the input dataset (`dataset`).
#'
#' @param dataset_ref Reference dataset
#'
#' The variables specified for `by_vars` are expected. For each
#' observation of the specified dataset a new observation is added to the
#' input dataset.
#'
#' @param by_vars Variables to consider for generation of groupwise summary
#' records. Providing the names of variables in [exprs()] will create a
Expand All @@ -29,6 +41,34 @@
#' + `filter = (dplyr::n() > 2)` will filter n count of `by_vars` greater
#' than 2.
#'
#' @param set_values_to Variables to be set
#'
#' The specified variables are set to the specified values for the new
#' observations.
#'
#' Set a list of variables to some specified value for the new records
#' + LHS refer to a variable.
#' + RHS refers to the values to set to the variable. This can be a string, a
#' symbol, a numeric value, an expression or NA. If summary functions are
#' used, the values are summarized by the variables specified for `by_vars`.
#'
#' For example:
#' ```
#' set_values_to = exprs(
#' AVAL = sum(AVAL),
#' DTYPE = "AVERAGE",
#' )
#' ```
#'
#' @param missing_values Values for missing summary values
#'
#' For observations of the input dataset (`dataset`) or (`dataset_add`) which do not have an
#' complete mapping defined by the summarization defined in `set_values_to`. Only variables
zdz2101 marked this conversation as resolved.
Show resolved Hide resolved
#' specified for `set_values_to` can be specified for `missing_values`.
#'
#' *Permitted Values*: named list of expressions, e.g.,
#' `exprs(AVAL = -9999)`
#'
#' @inheritParams get_summary_records
#'
#' @return A data frame with derived records appended to original dataset.
Expand Down Expand Up @@ -125,18 +165,33 @@
#' ) %>%
#' arrange(USUBJID, AVISIT)
derive_summary_records <- function(dataset,
bundfussr marked this conversation as resolved.
Show resolved Hide resolved
dataset_add = NULL,
bundfussr marked this conversation as resolved.
Show resolved Hide resolved
dataset_ref = NULL,
by_vars,
filter = NULL,
bundfussr marked this conversation as resolved.
Show resolved Hide resolved
analysis_var,
summary_fun,
set_values_to) {
set_values_to = NULL,
bundfussr marked this conversation as resolved.
Show resolved Hide resolved
missing_values = NULL) {
assert_vars(by_vars)
filter <- assert_filter_cond(enexpr(filter), optional = TRUE)
assert_data_frame(
dataset,
required_vars = by_vars
required_vars = expr_c(by_vars)
bundfussr marked this conversation as resolved.
Show resolved Hide resolved
)
assert_data_frame(
dataset_add,
required_vars = expr_c(by_vars),
optional = TRUE
)
assert_data_frame(
dataset_ref,
required_vars = by_vars,
optional = TRUE
)

assert_varval_list(set_values_to)
assert_expr_list(missing_values, named = TRUE, optional = TRUE)

if (!missing(analysis_var) || !missing(summary_fun)) {
deprecate_warn(
Expand All @@ -149,14 +204,47 @@ derive_summary_records <- function(dataset,
set_values_to <- exprs(!!analysis_var := {{ summary_fun }}(!!analysis_var), !!!set_values_to)
}

# Summarise the analysis value and bind to the original dataset
bind_rows(
dataset,
get_summary_records(
dataset,
by_vars = by_vars,
filter = !!filter,
set_values_to = set_values_to
if (is.null(dataset_add)) {
dataset_add <- dataset
}

summary_records <- dataset_add %>%
group_by(!!!by_vars) %>%
filter_if(filter) %>%
summarise(!!!set_values_to) %>%
ungroup()

if (!is.null(missing_values)) {
update_missings <- map2(
syms(names(missing_values)),
missing_values,
~ expr(if_else(is.na(!!.x), !!.y, !!.x))
)
names(update_missings) <- names(missing_values)
summary_records <- summary_records %>%
mutate(!!!update_missings)
}
bundfussr marked this conversation as resolved.
Show resolved Hide resolved

df_return <- bind_rows(
dataset,
summary_records
)

if (!is.null(dataset_ref)) {
add_vars <- colnames(dataset_add)
ref_vars <- colnames(dataset_ref)

new_ref_obs <- anti_join(
select(dataset_ref, intersect(add_vars, ref_vars)),
select(summary_records, !!!by_vars),
by = map_chr(by_vars, as_name)
)

df_return <- bind_rows(
df_return,
new_ref_obs
)
}

return(df_return)
}
12 changes: 9 additions & 3 deletions R/get_summary_records.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#' Create Summary Records
#'
#' @description
#'
#' `r lifecycle::badge("superseded")`
#'
#' Development on `get_summary_records()` is complete, and for new code we recommend
#' switching to using the `dataset_add` argument in `derive_summary_records()`.
#'
#' It is not uncommon to have an analysis need whereby one needs to derive an
#' analysis value (`AVAL`) from multiple records. The ADaM basic dataset
#' structure variable `DTYPE` is available to indicate when a new derived
Expand Down Expand Up @@ -64,9 +70,9 @@
#'
#' @return A data frame of derived records.
#'
#' @family der_gen
#' @family superseded
#'
#' @keywords der_gen
#' @keywords superseded
#'
#' @seealso [derive_summary_records()], [derive_var_merged_summary()]
#'
Expand Down Expand Up @@ -154,7 +160,7 @@ get_summary_records <- function(dataset,
filter = NULL,
analysis_var,
summary_fun,
set_values_to) {
set_values_to = NULL) {
assert_vars(by_vars)
filter <- assert_filter_cond(enexpr(filter), optional = TRUE)
assert_data_frame(
Expand Down
3 changes: 1 addition & 2 deletions man/derive_extreme_event.Rd

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

3 changes: 1 addition & 2 deletions man/derive_extreme_records.Rd

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

4 changes: 4 additions & 0 deletions man/derive_param_extreme_record.Rd

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

31 changes: 27 additions & 4 deletions man/derive_summary_records.Rd

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

3 changes: 1 addition & 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.

3 changes: 1 addition & 2 deletions man/derive_var_joined_exist_flag.Rd

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

3 changes: 1 addition & 2 deletions man/derive_var_merged_exist_flag.Rd

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

3 changes: 1 addition & 2 deletions man/derive_var_merged_summary.Rd

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

3 changes: 1 addition & 2 deletions man/derive_var_obs_number.Rd

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

3 changes: 1 addition & 2 deletions man/derive_var_relative_flag.Rd

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

3 changes: 1 addition & 2 deletions man/derive_vars_joined.Rd

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

3 changes: 1 addition & 2 deletions man/derive_vars_merged.Rd

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

3 changes: 1 addition & 2 deletions man/derive_vars_merged_lookup.Rd

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

3 changes: 1 addition & 2 deletions man/derive_vars_transposed.Rd

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

Loading
Loading