Skip to content

[Bug]: Accessing columns by path doesn't work when table has an overall column. #908

@hafen

Description

@hafen

Using the example found here (minus the secondary column splitting split_cols_by("SEX")):

.simpleCap <- function(x) {
  if (length(x) > 1) {
    return(sapply(x, .simpleCap))
  }
  s <- strsplit(tolower(x), " ")[[1]]
  paste(toupper(substring(s, 1, 1)), substring(s, 2), sep = "", collapse = " ")
}

adsl2 <- ex_adsl |>
  filter(SEX %in% c("M", "F") & RACE %in% (levels(RACE)[1:3])) |>
  mutate(ethnicity = .simpleCap(gsub("(.*)OR.*", "\\1", RACE)), RACE = factor(RACE))

lyt2 <- basic_table() |>
  split_cols_by("ARM") |>
  split_rows_by("RACE", labels_var = "ethnicity", split_fun = drop_split_levels) |>
  summarize_row_groups() |>
  analyze(c("AGE", "STRATA1"))

tbl2 <- build_table(lyt2, adsl2)

tbl2
#             A: Drug X    B: Placebo   C: Combination
# ————————————————————————————————————————————————————
# Asian       66 (54.1%)   66 (55.5%)     71 (59.2%)  
#   AGE                                               
#     Mean      32.50        36.68          36.99     
#   STRATA1                                           
#     A           21           24             18      
#     B           20           22             25      
#     C           25           20             28      
# Black       30 (24.6%)   28 (23.5%)     28 (23.3%)  
#   AGE                                               
#     Mean      34.27        34.93          33.71     
#   STRATA1                                           
#     A           7            11             10      
#     B           11           7              8       
#     C           12           10             10      
# White       26 (21.3%)   25 (21.0%)     21 (17.5%)  
#   AGE                                               
#     Mean      36.15        33.12          31.95     
#   STRATA1                                           
#     A           8            6              8       
#     B           9            12             7       
#     C           9            7              6 

Adding a column footnote works for this, for example:

col_paths_summary(tbl2)
# label             path               
# —————————————————————————————————————
# A: Drug X         ARM, A: Drug X     
# B: Placebo        ARM, B: Placebo    
# C: Combination    ARM, C: Combination
fnotes_at_path(tbl2, rowpath = NULL, c("ARM", "B: Placebo")) <- c("this is a placebo")
tbl2
#             A: Drug X    B: Placebo {1}   C: Combination
# ————————————————————————————————————————————————————————
# Asian       66 (54.1%)     66 (55.5%)       71 (59.2%)  
#   AGE                                                   
#     Mean      32.50          36.68            36.99     
# ...
#     C           9              7                6       
# ————————————————————————————————————————————————————————

# {1} - this is a placebo
# ————————————————————————————————————————————————————————

However, if we add an overall column, this no longer works:

lyt3 <- lyt2 |> add_overall_col("Total")
tbl3 <- build_table(lyt3, adsl2)
tbl3
#             A: Drug X    B: Placebo   C: Combination      Total   
# ——————————————————————————————————————————————————————————————————
# Asian       66 (54.1%)   66 (55.5%)     71 (59.2%)     203 (56.2%)
#   AGE                                                             
#     Mean      32.50        36.68          36.99           35.43   
# ...
#     C           9            7              6              22  
col_paths_summary(tbl3)
# label             path               
# —————————————————————————————————————
# A: Drug X         ARM, A: Drug X     
# B: Placebo        ARM, B: Placebo    
# C: Combination    ARM, C: Combination
# Total             Total, Total
fnotes_at_path(tbl3, rowpath = NULL, c("ARM", "B: Placebo")) <- c("this is a placebo")
# Error in col_fnotes_at_path(coltree(ttrp), colpath, fnotes = value) : 
#   Path appears invalid at step: ARM

Also, possibly related, note that the actual example found here has its footnote numbered as NA in the output found in the article.

I encounter this using rtables 0.6.9 installed from CRAN as well as v0.6.9.9005 installed from GitHub.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions