# Example of a picky split function. The number of values in the column variable
# var decrees if we are going to print also the column with all observation
# or not.
picky_splitter <- function(var) {
# Main layout function
function(df, spl, vals, labels, trim) {
orig_vals <- vals
# Check for number of levels if all are selected
if (is.null(vals)) {
vec <- df[[var]]
vals <- unique(vec)
}
# Do a split with or without All obs
if (length(vals) == 1) {
do_base_split(spl = spl, df = df, vals = vals, labels = labels, trim = trim)
} else {
fnc_tmp <- add_overall_level("Overall", label = "All Obs", first = FALSE)
fnc_tmp(df = df, spl = spl, vals = orig_vals, trim = trim)
}
}
}
# Data sub-set
d1 <- subset(ex_adsl, ARM == "A: Drug X" | (ARM == "B: Placebo" & SEX == "F"))
d1 <- subset(d1, SEX %in% c("M", "F"))
d1$SEX <- factor(d1$SEX)
# This table uses the number of values in the SEX column to add the overall col or not
lyt <- basic_table() %>%
split_cols_by("ARM", split_fun = drop_split_levels) %>%
split_cols_by("SEX", split_fun = picky_splitter("SEX")) %>%
analyze("AGE", show_labels = "visible")
tbl <- build_table(lyt, d1)
tbl
Run the code above in your browser using DataLab