rtables icon indicating copy to clipboard operation
rtables copied to clipboard

Split by multivar does not support other column splits

Open Melkiades opened this issue 2 years ago • 1 comments

This issue keeps track of a workaround currently able to solve the split_cols after split_cols_multivar

Error:

DM_tmp <- DM %>% 
    mutate(SEX = factor(SEX))
mean_lab <- function(x, .spl_context, ...) {
    label_from_context <- .spl_context$value[nrow(.spl_context)]
    in_rows(mean(x), .labels = label_from_context)
}
sd_lab <- function(x, .spl_context, ...) {
    label_from_context <- .spl_context$value[nrow(.spl_context)]
    in_rows(sd(x), .labels = label_from_context)
}

lyt <- basic_table() %>% 
    split_rows_by("STRATA1", child_labels = "hidden") %>%
    split_cols_by_multivar(vars = c("BMRKR1", "BMRKR1"), 
                           varlabels = c("M", "SD")) %>%
    split_cols_by("SEX", split_fun = drop_split_levels) %>% 
    analyze_colvars(afun = list(mean_lab, sd_lab), format = "xx.x")
lyt %>% build_table(DM_tmp)
Error: Error applying analysis function (var - NA): missing value where TRUE/FALSE needed
occured at (row) path: STRATA1[A]

Current workaround from PR #665 @BFalquet:

    ## Duplication hack -> This would need to use  split_cols_by_multivar(...)
    DM_tmp <- DM %>% 
        mutate(method = factor("Mean")) 
    
    DM_tmp <- rbind(DM_tmp, DM_tmp %>% 
                        mutate(method = factor("SD")))
    
    analysis_fun_fin <- function(x, .spl_context, labelstr = "", ...) {
        
        # Very smart internal checks for name reconstruction from path
        stopifnot(length(.spl_context$cur_col_id[[1]]) == 1L)
        stopifnot(.spl_context$cur_col_id[[1]] %in% names(.spl_context))
        
        if (any(.spl_context$cur_col_split_val[[2]] == "SD")) {
            res <- list("SOMETHING" = sd(x))
        } else if (any(.spl_context$cur_col_split_val[[2]] == "Mean")) {
            res <- list("SOMETHING" = mean(x))
        }
            
        in_rows(.list = res)
    }
    
    lyt <- basic_table() %>% 
        split_rows_by("STRATA1") %>%
        split_cols_by(var = "method") %>%
        split_cols_by("SEX", split_fun = drop_split_levels) %>%
        analyze(vars = "BMRKR1", afun = analysis_fun_fin, format = "xx.xxx")
    
    tbl <- lyt %>% build_table(DM_tmp)

The lack of this support should be either documented as not supported or added.

Melkiades avatar Jul 18 '23 09:07 Melkiades

We don't need multivar split for this, just a split which generates multiple facets that contain the full data, with different labels and we should be able to use extra args to control any differences in behavior. I think we need to refine this requirement more and really get down to the essence of what the desired table archetype is and how that maps to fundamental layout and table structure.

gmbecker avatar Jul 20 '23 01:07 gmbecker