rtables
rtables copied to clipboard
conditionally add layouting instructions
Often we call:
lyt <- basic_table() %>%
split_cols_by(armvar)
if (!is.null(lbl_overall))
lyt <- lyt %>% add_overall_col(label = lbl_overall)
lyt <- lyt %>%
analyze("AGE", mean) # cont. layout building
It would be nice to call:
lyt <- basic_table() %>%
split_cols_by(armvar) %>%
ifelse_layout(!is.null(lbl_overall), function(lyt) add_overall_col(lyt, label = lbl_overall)) %>%
analyze("AGE", mean)
where
ifelse_layout <- function(lyt, test, yes_lyt_fun, no_lyt_fun = identity) {
if (test)
yes_lyt_fun(lyt)
else
no_lyt_fun(lyt)
}
Further we with a function:
lyt_fun <- function(fun, ...) {
function(lyt) {
fun(lyt, ...)
}
}
we could write the code above as:
lyt <- basic_table() %>%
split_cols_by(armvar) %>%
ifelse_layout(!is.null(lbl_overall), lyt_fun(add_overall_col, label = lbl_overall)) %>%
analyze("AGE", mean)
@gmbecker what do think? Do you have alternate suggestions, and should we add something like ifelse_layout
and lyt_fun
to rtables
?
it's a general piping topic, but I'd rather not use the .
as suggested here
let's come up with a solution that is geared towards layout creation in rtables
and works with |>