Skip to content

[Bug]: usage of top_level_section_div in a layout applied to a zero rows dataset fails #1079

@iaugusty

Description

@iaugusty

Following reprex illustrates the failure to use build_table using a zero row input dataframe and a layout that includes option top_level_section_div = " ".

library(rtables)
#> Loading required package: formatters
#> 
#> Attaching package: 'formatters'
#> The following object is masked from 'package:base':
#> 
#>     %||%
#> Loading required package: magrittr
#> 
#> Attaching package: 'rtables'
#> The following object is masked from 'package:utils':
#> 
#>     str
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

packageVersion("rtables")
#> [1] '0.6.13'

adsl <- ex_adsl
adae <- ex_adae |> 
  mutate(TRTEMFL = "Y") |> 
  mutate(TRTEMFL = factor(TRTEMFL))

adae2 <- adae[0, ]

my_layout <- function(top_level_section_div = TRUE){
  if (top_level_section_div) {
    lyt_part1 <- basic_table(
      top_level_section_div = " ",
      show_colcounts = TRUE,
      colcount_format = "N=xx"
    ) 
  } else {
    lyt_part1 <- basic_table(
      show_colcounts = TRUE,
      colcount_format = "N=xx"
    )}
  
  lyt_part2 <- lyt_part1 |> 
    split_cols_by("ARM") |> 
    analyze("TRTEMFL")  %>%
    split_rows_by("AEBODSYS",
                  split_label = "System Organ Class",
                  split_fun = trim_levels_in_group("AEDECOD"),
                  label_pos = "topleft",
                  section_div = c(" "),
                  nested = FALSE
    ) %>%
    summarize_row_groups("AEBODSYS") %>%
    analyze("AEDECOD") %>%
    append_topleft("  Preferred Term, n (%)")
  
  lyt_part2
}

lyt_1 <- my_layout(top_level_section_div = FALSE)
lyt_2 <- my_layout(top_level_section_div = TRUE)

# a non-zero rows dataset can be used in both layouts
build_table(lyt_1, adae, adsl)
#> System Organ Class         A: Drug X     B: Placebo    C: Combination
#>   Preferred Term, n (%)      N=134         N=134           N=132     
#> —————————————————————————————————————————————————————————————————————
#> Y                             609           622             703      
#> cl A.1                    132 (98.5%)   130 (97.0%)     160 (121.2%) 
#>   dcd A.1.1.1.1               64             62              88      
#>   dcd A.1.1.1.2               68             68              72      
#>                                                                      
#> cl B.1                    56 (41.8%)     60 (44.8%)      62 (47.0%)  
#>   dcd B.1.1.1.1               56             60              62      
#>                                                                      
#> cl B.2                    129 (96.3%)   138 (103.0%)    143 (108.3%) 
#>   dcd B.2.1.2.1               65             62              66      
#>   dcd B.2.2.3.1               64             76              77      
#>                                                                      
#> cl C.1                    55 (41.0%)     63 (47.0%)      64 (48.5%)  
#>   dcd C.1.1.1.3               55             63              64      
#>                                                                      
#> cl C.2                    48 (35.8%)     53 (39.6%)      65 (49.2%)  
#>   dcd C.2.1.2.1               48             53              65      
#>                                                                      
#> cl D.1                    127 (94.8%)   106 (79.1%)     135 (102.3%) 
#>   dcd D.1.1.1.1               61             51              71      
#>   dcd D.1.1.4.2               66             55              64      
#>                                                                      
#> cl D.2                    62 (46.3%)     72 (53.7%)      74 (56.1%)  
#>   dcd D.2.1.5.3               62             72              74
build_table(lyt_2, adae, adsl)
#> System Organ Class         A: Drug X     B: Placebo    C: Combination
#>   Preferred Term, n (%)      N=134         N=134           N=132     
#> —————————————————————————————————————————————————————————————————————
#> Y                             609           622             703      
#>                                                                      
#> cl A.1                    132 (98.5%)   130 (97.0%)     160 (121.2%) 
#>   dcd A.1.1.1.1               64             62              88      
#>   dcd A.1.1.1.2               68             68              72      
#>                                                                      
#> cl B.1                    56 (41.8%)     60 (44.8%)      62 (47.0%)  
#>   dcd B.1.1.1.1               56             60              62      
#>                                                                      
#> cl B.2                    129 (96.3%)   138 (103.0%)    143 (108.3%) 
#>   dcd B.2.1.2.1               65             62              66      
#>   dcd B.2.2.3.1               64             76              77      
#>                                                                      
#> cl C.1                    55 (41.0%)     63 (47.0%)      64 (48.5%)  
#>   dcd C.1.1.1.3               55             63              64      
#>                                                                      
#> cl C.2                    48 (35.8%)     53 (39.6%)      65 (49.2%)  
#>   dcd C.2.1.2.1               48             53              65      
#>                                                                      
#> cl D.1                    127 (94.8%)   106 (79.1%)     135 (102.3%) 
#>   dcd D.1.1.1.1               61             51              71      
#>   dcd D.1.1.4.2               66             55              64      
#>                                                                      
#> cl D.2                    62 (46.3%)     72 (53.7%)      74 (56.1%)  
#>   dcd D.2.1.5.3               62             72              74

# no issue with a zero row input dataset for layout without top_level_section_div
build_table(lyt_1, adae2, adsl)
#> System Organ Class        A: Drug X   B: Placebo   C: Combination
#>   Preferred Term, n (%)     N=134       N=134          N=132     
#> —————————————————————————————————————————————————————————————————
#> Y                             0           0              0
# issue with a zero row input dataset for layout with top_level_section_div
build_table(lyt_2, adae2, adsl)
#> Error in ret$sect_div_from_path[[nrow(ret)]] <- path: attempt to select less than one element in OneIndex

Created on 2026-03-17 with reprex v2.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions