insightsengineering / rtables

Reporting tables with R
https://insightsengineering.github.io/rtables/
Other
226 stars 48 forks source link

Split name missing from cur_col_id in split context #763

Open gmbecker opened 1 year ago

gmbecker commented 1 year ago

Currently the cur_col_idin the split context is pasted together from only the values of the column path the tabulation machinery is currently at.

This information is incomplete, particularly in cases where there are different facets in column space based on the same variable, e.g,

DM2 <- DM
DM2$active_trt <- factor(ifelse(DM2$ARM == "C: Combination", " ", "Cool Category"),
                         levels = c("Cool Category", " "))

myfun <- function(x, .N_col, .spl_context) {
    colid <- .spl_context$cur_col_id[[1]]
    print(colid)
    if(grepl(".", colid, fixed = TRUE))
        rcell(length(x)*c(1, 1/.N_col), format = "xx (xx.x%)")
    else
        rcell("some other stuff")
}

lyt3 <- basic_table() %>%
    split_cols_by("active_trt", split_fun = trim_levels_in_group("ARM")) %>%
    split_cols_by("ARM") %>%
    split_cols_by("ARM", split_fun = keep_split_levels(c("A: Drug X", "C: Combination")),
                  nested = FALSE) %>%
    split_rows_by("STRATA1") %>%
    analyze("ID", myfun)

build_table(lyt3, DM2)

gives us

[1] "Cool Category.A: Drug X"
[1] "Cool Category.B: Placebo"
[1] " .C: Combination"
[1] "A: Drug X"
[1] "C: Combination"
[1] "Cool Category.A: Drug X"
[1] "Cool Category.B: Placebo"
[1] " .C: Combination"
[1] "A: Drug X"
[1] "C: Combination"
[1] "Cool Category.A: Drug X"
[1] "Cool Category.B: Placebo"
[1] " .C: Combination"
[1] "A: Drug X"
[1] "C: Combination"
               Cool Category                                                            
          A: Drug X    B: Placebo   C: Combination      A: Drug X        C: Combination 
————————————————————————————————————————————————————————————————————————————————————————
A                                                                                       
  myfun   36 (29.8%)   33 (31.1%)     45 (34.9%)     some other stuff   some other stuff
B                                                                                       
  myfun   41 (33.9%)   40 (37.7%)     38 (29.5%)     some other stuff   some other stuff
C                                                                                       
  myfun   44 (36.4%)   33 (31.1%)     46 (35.7%)     some other stuff   some other stuff

Note I was able to monkey-hack something together here, but having access to the full path, either in pasted form in cur_col_id or preferably, imo, in a separate list column, would be a cleaner solution more in line with all of the o ther path-based interactions rtables supports

Melkiades commented 1 year ago

I think the path is available here: .spl_context$cur_col_split_val or .spl_context$cur_col_split. We could discuss having a .spl_context_rows and .splt_context_cols (and maybe making it an object with rows and cols getter/setters) but at the moment I do not know if it is worth the added complexity

gmbecker commented 1 year ago

@Melkiades unfortunately the current split and value are not sufficient. In the example I put above (which I can now say is a set of relative risk columns, since I found out that came from public FDA specs), I effectively need to know whether the current column split has a parent split or not.