Skip to content

Commit

Permalink
feat: #2142 working enhanced function
Browse files Browse the repository at this point in the history
  • Loading branch information
Zelos Zhu committed Oct 26, 2023
1 parent 70836b5 commit 346f595
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 65 deletions.
34 changes: 15 additions & 19 deletions R/derive_summary_records.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ derive_summary_records <- function(dataset,
required_vars = expr_c(by_vars),
optional = TRUE
)
assert_varval_list(set_values_to, optional = TRUE)
assert_expr_list(missing_values, named = TRUE, 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 @@ -170,29 +170,15 @@ 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
# )
# analysis_var <- assert_symbol(enexpr(analysis_var))
# assert_s3_class(summary_fun, "function")
# set_values_to <- exprs(!!analysis_var := {{ summary_fun }}(!!analysis_var), !!!set_values_to)
# }

if (is.null(dataset_add)) {
dataset_add <- dataset
}

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

df_return <- bind_rows(
dataset,
Expand All @@ -215,6 +201,16 @@ derive_summary_records <- function(dataset,
)
}

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

return(df_return)
}
14 changes: 7 additions & 7 deletions R/get_summary_records.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ get_summary_records <- function(dataset,
summary_fun,
set_values_to = NULL) {
# lifecycle::signal_stage("superseded", "get_summary_records()", "derive_summary_records(dataset_add= )")
inform(
message = paste0(
"`get_summary_records()` has been superseded, ",
"please use the `dataset_add` argument in `derive_summary_records()`",
sep = ""
)
)
# inform(
# message = paste0(
# "`get_summary_records()` has been superseded, ",
# "please use the `dataset_add` argument in `derive_summary_records()`",
# sep = ""
# )
# )
assert_vars(by_vars)
filter <- assert_filter_cond(enexpr(filter), optional = TRUE)
assert_data_frame(
Expand Down
6 changes: 2 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.

18 changes: 3 additions & 15 deletions man/get_summary_records.Rd

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

38 changes: 38 additions & 0 deletions tests/testthat/test-derive_summary_records.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,41 @@ test_that("derive_summary_records Test 4: deprecation warning for analysis_var a
keys = c("subj", "visit", "seq", "type")
)
})

test_that("make sure dataset_add works", {
input <- tibble::tribble(
~subj, ~visit, ~val, ~seq,
"1", 1, 10, 1,
"1", 1, 14, 2,
"1", 1, 9, 3,
"1", 2, 11, 4,
"2", 2, NA_real_, 1
)
input_add <- tibble::tribble(
~subj, ~visit, ~add_val, ~seq,
"1", 1, 100, 1,
"1", 1, 140, 2,
"1", 1, 90, 3
)
expected_output<- bind_rows(
input,
tibble::tribble(
~subj, ~visit, ~val, ~type,
"1", 1, 110, "AVERAGE"
)
)
actual_output <- input %>%
derive_summary_records(
dataset_add = input_add,
by_vars = exprs(subj, visit),
set_values_to = exprs(
val = mean(add_val, na.rm = TRUE),
type = "AVERAGE"
)
)
expect_dfs_equal(
base = expected_output,
compare = actual_output,
keys = c("subj", "visit", "seq", "type")
)
})

0 comments on commit 346f595

Please sign in to comment.