-
Notifications
You must be signed in to change notification settings - Fork 4
new unified get_ref_info #423
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
base: dev
Are you sure you want to change the base?
Changes from all commits
7edda8c
a4a59f3
bba7576
2ed9cd8
9f32e7c
35098b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,25 @@ | ||
| #' @title Obtain Reference Information for a Global Reference Group | ||
| #' | ||
| #' @description `r lifecycle::badge("stable")` | ||
| #' Obtain reference information for a global reference group. | ||
| #' | ||
| #' This helper function can be used in custom analysis functions, by passing | ||
| #' an extra argument `ref_path` which defines a global reference group by | ||
| #' the corresponding column split hierarchy levels. | ||
| #' | ||
| #' @param ref_path (`character`)\cr reference group specification as an `rtables` | ||
| #' `colpath`, see details. | ||
| #' @param .spl_context (`data.frame`)\cr see [rtables::spl_context]. | ||
| #' @param .var (`character`)\cr the variable being analyzed, | ||
| #' see [rtables::additional_fun_params]. | ||
| #' @param ref_path (`character`) | ||
| #' Reference group specification as an `rtables` `colpath`; see Details. | ||
| #' @param .spl_context (`data.frame`) | ||
| #' Ancestor split-state information passed by `rtables`. | ||
| #' @param .var (`character`) | ||
| #' The variable being analyzed; see [rtables::additional_fun_params]. | ||
| #' | ||
| #' @return A list with `ref_group` and `in_ref_col`, which can be used as | ||
| #' `.ref_group` and `.in_ref_col` as if being directly passed to an analysis | ||
| #' function by `rtables`, see [rtables::additional_fun_params]. | ||
| #' @return | ||
| #' * `get_ref_info()` returns a list with: | ||
| #' * `ref_group`: the reference group data (a `data.frame` or vector depending | ||
| #' on `.var`), equivalent to `.ref_group` from [rtables::additional_fun_params]. | ||
| #' * `in_ref_col`: logical, whether the current column is the reference column, | ||
| #' equivalent to `.in_ref_col` from [rtables::additional_fun_params]. | ||
| #' * `trt_var`: the treatment variable name (last variable in `ref_path`). | ||
| #' * `ctrl_grp`: the reference group level (last level in `ref_path`). | ||
| #' * `cur_col_val`: the current column's value for `trt_var`. | ||
| #' | ||
| #' @details | ||
| #' The reference group is specified in `colpath` hierarchical fashion in | ||
|
|
@@ -73,34 +78,55 @@ | |
| #' build_table(lyt, dm) | ||
| get_ref_info <- function(ref_path, .spl_context, .var = NULL) { | ||
| if (is.null(ref_path)) { | ||
| return(list(ref_group = NULL, in_ref_col = NULL)) | ||
| return( | ||
| list(ref_group = NULL, in_ref_col = NULL, trt_var = NULL, ctrl_grp = NULL, cur_col_val = NULL) | ||
| ) | ||
| } | ||
|
|
||
| checkmate::assert_character(ref_path, min.len = 2L, names = "unnamed") | ||
| checkmate::assert_true(length(ref_path) %% 2 == 0) | ||
| checkmate::assert_true(length(ref_path) %% 2L == 0L) | ||
| checkmate::assert_data_frame(.spl_context) | ||
|
|
||
| leaf_sc <- .spl_context[nrow(.spl_context), ] | ||
| vars_indices <- seq(from = 1L, to = length(ref_path) - 1L, by = 2L) | ||
| level_indices <- seq(from = 2L, to = length(ref_path), by = 2L) | ||
| ref_path_levels <- paste(ref_path[level_indices], collapse = ".") | ||
| cur_col_path <- cur_col_split_path(.spl_context) | ||
| cur_col_vars <- cur_col_path[seq(1L, length(cur_col_path), by = 2L)] | ||
| ref_path_last <- utils::tail(ref_path, 2L) | ||
| last_var_pos <- match(ref_path_last[1L], cur_col_vars) | ||
| cur_col_last_val <- if (!is.na(last_var_pos)) { | ||
| cur_col_path[2L * last_var_pos] | ||
| } else { | ||
| NULL | ||
| } | ||
|
|
||
| # If ref_path variables are outside of the current column split variable. | ||
| is_ref_in_colvars <- identical(leaf_sc$cur_col_split[[1]], ref_path[vars_indices]) | ||
| if (!is_ref_in_colvars) { | ||
| return(list(ref_group = NULL, in_ref_col = NULL)) | ||
| ref_path_val_pos <- seq(2L, length(ref_path), by = 2L) | ||
| ref_path_any_vals <- ref_path | ||
| ref_path_any_vals[ref_path_val_pos] <- "*" | ||
| if (!in_column(ref_path_any_vals, .spl_context)) { | ||
| return( | ||
| list( | ||
| ref_group = NULL, | ||
| in_ref_col = NULL, | ||
| trt_var = ref_path_last[1L], | ||
| ctrl_grp = ref_path_last[2L], | ||
|
Comment on lines
+109
to
+110
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the name |
||
| cur_col_val = cur_col_last_val | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| # Prepare in_ref_col. | ||
| in_ref_col <- identical(leaf_sc$cur_col_split_val[[1]], ref_path[level_indices]) | ||
|
|
||
| # Prepare ref_group. | ||
| full_df <- leaf_sc$full_parent_df[[1]] | ||
| row_in_ref_group <- leaf_sc[[ref_path_levels]][[1]] | ||
| leaf_sc <- .spl_context[nrow(.spl_context), ] | ||
| full_df <- leaf_sc$full_parent_df[[1L]] | ||
| ref_path_levels <- paste(ref_path[ref_path_val_pos], collapse = ".") | ||
| row_in_ref_group <- leaf_sc[[ref_path_levels]][[1L]] | ||
| ref_group <- full_df[row_in_ref_group, ] | ||
| if (!is.null(.var)) { | ||
| ref_group <- ref_group[[.var]] | ||
| } | ||
|
|
||
| list(ref_group = ref_group, in_ref_col = in_ref_col) | ||
| list( | ||
| ref_group = ref_group, | ||
| in_ref_col = in_column(ref_path, .spl_context), | ||
| trt_var = ref_path_last[1L], | ||
| ctrl_grp = ref_path_last[2L], | ||
| cur_col_val = cur_col_last_val | ||
| ) | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
technically this is the most recent split's name, which is usually a variable name from the data, but its not guaranteed to be