Skip to content

.spl_context has 0 rows and missing columns when no row split is defined. #1114

Description

@wwojciech

Hi,

I’m writing a custom cfun whose behavior depends on the current column, but .spl_context is empty when there is no row-split defined. See below:

c_test <- function(x, labelstr = "", .spl_context) {
  cat("nrow(.spl_context) is:", nrow(.spl_context), "\n")
  cat("colnames(.spl_context) are:", colnames(.spl_context), "\n\n")

  rcell(1, label = labelstr)
}

lyt <- basic_table() |>
  split_cols_by("ARM") |>
  summarize_row_groups(format = NULL, cfun = c_test)

tbl <- build_table(lyt, DM)

nrow(.spl_context) is: 0 
colnames(.spl_context) are: split value full_parent_df all_cols_n A: Drug X B: Placebo C: Combination 

nrow(.spl_context) is: 0 
colnames(.spl_context) are: split value full_parent_df all_cols_n A: Drug X B: Placebo C: Combination 

nrow(.spl_context) is: 0 
colnames(.spl_context) are: split value full_parent_df all_cols_n A: Drug X B: Drug X B: Placebo C: Combination 

I’m wondering what the reasoning is behind the fact that, when there is no row split in the layout, .spl_context has 0 rows and some of its usual columns are missing. Is this the expected behavior? I would have expected .spl_context to contain all of its usual columns, and also with a single row representing the root row-split context.

I understand that using summarize_row_groups() without any row split in the layout is a legitimate use case. For example:

lyt2 <- basic_table() |>
  split_cols_by("ARM") |>
  summarize_row_groups(label_fstr = "Overall")
tbl2 <- build_table(lyt2, DM)
tbl2
           A: Drug X      B: Placebo    C: Combination
——————————————————————————————————————————————————————
Overall   121 (100.0%)   106 (100.0%)    129 (100.0%)

In this case, summarize_row_groups() is effectively operating at the root level, so I would expect the root context to be represented in .spl_context, even though there are no row splits.

PS. I know I can work around this by using analyze(some_var, afun = c_test).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions