Skip to content

Commit

Permalink
rework for generalization
Browse files Browse the repository at this point in the history
  • Loading branch information
Melkiades committed Oct 16, 2023
1 parent a8f475a commit 6f876df
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 150 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ Collate:
'prop_diff.R'
'prop_diff_test.R'
'prune_occurrences.R'
'reflast.R'
'response_biomarkers_subgroups.R'
'response_subgroups.R'
'riskdiff.R'
Expand All @@ -160,3 +159,4 @@ Collate:
'utils_factor.R'
'utils_grid.R'
'utils_rtables.R'
'utils_split_funs.R'
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export(has_fraction_in_cols)
export(has_fractions_difference)
export(imputation_rule)
export(keep_content_rows)
export(keep_level_order)
export(keep_rows)
export(logistic_regression_cols)
export(logistic_summary_by_flag)
Expand Down
69 changes: 0 additions & 69 deletions R/reflast.R

This file was deleted.

108 changes: 108 additions & 0 deletions R/utils_split_funs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#' Custom Split Functions
#'
#' @description
#' Collection of useful functions that are expanding on the core list of functions
#' provided by `rtables`. See `?rtables::custom_split_funs` and [rtables::make_split_fun()]
#' for more information on how to make a custom split function. All these functions
#' work with [split_rows_by()] argument `split_fun` to modify the way the split
#' happens.
#'
#' @seealso [rtables::make_split_fun()]
#'
#' @name utils_split_funs
NULL

#' @describeIn utils_split_funs split function to place reference group facet last
#' during post-processing stage.
#'
#' @return
#' * `ref_group_last` returns an utility function that puts the reference group
#' as last and needs to be assigned to `split_fun`.
#'
#' @examples
#' library(dplyr)
#'
#' dat <- data.frame(
#' x = factor(letters[1:5], levels = letters[5:1]),
#' y = 1:5
#' )
#' # ref_group_last
#'
#' # With rtables layout functions
#' basic_table() %>%
#' split_cols_by("x", ref_group = "c", split_fun = ref_group_last) %>%
#' analyze("y") %>%
#' build_table(dat)
#'
#' # With tern layout funcitons
#' adtte_f <- tern_ex_adtte %>%
#' filter(PARAMCD == "OS") %>%
#' mutate(
#' AVAL = day2month(AVAL),
#' is_event = CNSR == 0
#' )
#'
#' basic_table() %>%
#' split_cols_by(var = "ARMCD", ref_group = "ARM B", split_fun = ref_group_last) %>%
#' add_colcounts() %>%
#' surv_time(
#' vars = "AVAL",
#' var_labels = "Survival Time (Months)",
#' is_event = "is_event",
#' ) %>%
#' build_table(df = adtte_f)
#'
#' @export
ref_group_last <- make_split_fun(
post = list(
function(splret, spl, fulldf) {
if (!"ref_group_value" %in% slotNames(spl)) {
stop("Reference group is undefined.")
}

spl_var <- spl@payload # can be substituted by splret$labels
init_lvls <- levels(fulldf[[spl_var]])

if (!all(names(splret$values) %in% init_lvls)) {
stop("This split function does not work with combination facets.")
}

ref_group_pos <- which(init_lvls == spl@ref_group_value)
reord_lvls <- c(init_lvls[-ref_group_pos], init_lvls[ref_group_pos])
ord <- match(reord_lvls, names(splret$values))

make_split_result(
splret$values[ord],
splret$datasplit[ord],
splret$labels[ord]
)
}
)
)
#' @describeIn utils_split_funs split function to keep original order of factor
#' levels in the split.
#'
#' @return
#' * `keep_level_order` returns an utility function that keeps the original levels'.
#' It needs to be assigned to `split_fun`.
#'
#' @examples
#' # keep_level_order --------
#' # Even if default would bring ref_group first, the original order puts it last
#' basic_table() %>%
#' split_cols_by("Species", ref_group = "virginica", split_fun = keep_level_order) %>%
#' analyze("Sepal.Length") %>%
#' build_table(iris)
#'
#' @export
keep_level_order <- make_split_fun(
post = list(
function(splret, spl, fulldf, ...) {
# browser() if you enter here the order of splret seems already correct
ord <- order(names(splret$values))
make_split_result(splret$values[ord],
splret$datasplit[ord],
splret$labels[ord])
}
)
)
57 changes: 0 additions & 57 deletions man/ref_group_last.Rd

This file was deleted.

85 changes: 85 additions & 0 deletions man/utils_split_funs.Rd

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

Loading

0 comments on commit 6f876df

Please sign in to comment.