rtables icon indicating copy to clipboard operation
rtables copied to clipboard

conditionally add layouting instructions

Open waddella opened this issue 3 years ago • 1 comments

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?

waddella avatar Nov 16 '21 10:11 waddella

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 |>

waddella avatar Nov 16 '21 11:11 waddella