rtables icon indicating copy to clipboard operation
rtables copied to clipboard

Splitting by multivar label/position problem

Open Melkiades opened this issue 2 years ago • 3 comments

Original application design:

DM_tmp <- DM %>% 
    mutate(SEX = factor(SEX))
lyt <- basic_table() %>% 
    split_rows_by("STRATA1") %>%
    split_cols_by_multivar(vars = c("BMRKR1", "BMRKR1"), 
                           varlabels = c("M", "SD")) %>%
    analyze_colvars(afun = list(mean, sd), format = "xx.x")
lyt %>% build_table(DM_tmp)
      M    SD 
——————————————
A             
     6.0   3.0
B             
     5.9   3.2
C             
     5.7   3.4

In general, it is needed to have them on the row line and this is the only way to get it currently

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_cols_by_multivar(vars = c("BMRKR1", "BMRKR1"), 
                           varlabels = c("M", "SD")) %>%
    split_rows_by("STRATA1", child_labels = "hidden") %>%
    analyze_colvars(afun = list(mean_lab, sd_lab), format = "xx.x")
lyt %>% build_table(DM_tmp)
     M    SD 
—————————————
A   6.0   3.0
B   5.9   3.2
C   5.7   3.4

Originally this problem was solved with this:

mean_lab <- function(x, labelstr, ...) in_rows(mean(x$BMRKR1), .labels = labelstr) # xxx why it gets a data.frame now?
sd_lab <- function(x, labelstr, ...) in_rows(sd(x$BMRKR1), .labels = labelstr)
lyt <- basic_table() %>% 
    split_rows_by("STRATA1") %>%
    split_cols_by_multivar(vars = c("BMRKR1", "BMRKR1"), 
                           varlabels = c("M", "SD")) %>%
    summarize_row_groups(cfun = list("M" = mean_lab, "SD" = sd_lab), format = "xx.x") # xxx why w/o format is breaking?
lyt %>% build_table(DM_tmp)

I think the answer to both questions here were hidden by make_afun. The first one still puzzles me, I need to see to it still.

Is there a possibility to have this behavior otherwise? i.e. having the analysis methods on the columns without having to fiddle with the labels?

Melkiades avatar Jul 18 '23 09:07 Melkiades

@Melkiades please look at how qtable does this for how to do this in a way that creates non-degenerate tables that match the desired facet structure.

gmbecker avatar Aug 11 '23 23:08 gmbecker

@Melkiades please look at how qtable does this for how to do this in a way that creates non-degenerate tables that match the desired facet structure.

I cannot find the part that mimics the example table in qtable. There is no split_by_multivar, right? I am sorry if I am missing something obvious there

Melkiades avatar Aug 17 '23 16:08 Melkiades

another solution I just came up with for this that does not involve doing the mysterious "hidden":

library(rtables)
library(dplyr)
my_fnc <- function(analyzed_var, fnc, ...) {
  function(x, .df_row) {
    analyze_split <- as.factor(.df_row[[analyzed_var]])
    res_lst <- lapply(levels(analyze_split), function(i) {
      fnc(x[i == analyze_split], ...)
    })
    
    in_rows(.list = setNames(res_lst, levels(analyze_split)))
  }
}

lyt <- basic_table() %>% 
  split_cols_by_multivar(vars = c("BMRKR1", "BMRKR1"), 
                         varlabels = c("M", "SD")) %>%
  analyze_colvars(afun = list(my_fnc("STRATA1", mean), my_fnc("STRATA1", sd)), format = "xx.x")
lyt %>% build_table(DM)

Melkiades avatar Sep 01 '23 08:09 Melkiades