Skip to content

Feature request: passing .alt_df_full to afuns when using build_table(lyt, df) #1089

@iaugusty

Description

@iaugusty

Would it be possible to consider passing the input dataframe df to .alt_df_full when build_table has been called with no alt_counts_df dataframe?

The current behaviour requires to set a default for .alt_df_full in the afun as demonstrated in following reprex.

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
adsl <- ex_adsl

myafun <- function(df, .var, .alt_df_full, .spl_context){
  xx <- .alt_df_full
  yy <- .spl_context$full_parent_df[[1]]
  in_rows(.list = list(full_parent_df = NROW(yy), 
                       .alt_df_full = NROW(xx),
                       df = NROW(df)),
          .labels = c("# (full_parent_df)",
                      "# (.alt_df_full)",
                      "# (df)"))
}

lyt <- basic_table() |> 
  split_cols_by("ARM") |> 
  analyze(vars = "STUDYID", afun = myafun)

build_table(lyt, adsl, alt_counts_df = adsl)
#>                      A: Drug X   B: Placebo   C: Combination
#> ————————————————————————————————————————————————————————————
#> # (full_parent_df)      400         400            400      
#> # (.alt_df_full)        400         400            400      
#> # (df)                  134         134            132
build_table(lyt, adsl)
#> Error: Error applying analysis function (var - STUDYID): argument ".alt_df_full" is missing, with no default
#>  occured at (row) path: root

# set default for .alt_df_full to NULL when not passed by rtables splitting machinery

myafun2 <- function(df, .var, .alt_df_full = NULL, .spl_context){
  xx <- .alt_df_full
  yy <- .spl_context$full_parent_df[[1]]
  in_rows(.list = list(full_parent_df = NROW(yy), 
                       .alt_df_full = NROW(xx),
                       df = NROW(df)),
          .labels = c("# (full_parent_df)",
                      "# (.alt_df_full)",
                      "# (df)"))
}

lyt2 <- basic_table() |> 
  split_cols_by("ARM") |> 
  analyze(vars = "STUDYID", afun = myafun2)

build_table(lyt2, adsl)
#>                      A: Drug X   B: Placebo   C: Combination
#> ————————————————————————————————————————————————————————————
#> # (full_parent_df)      400         400            400      
#> # (.alt_df_full)         0           0              0       
#> # (df)                  134         134            132
build_table(lyt2, adsl, alt_counts_df = adsl)
#>                      A: Drug X   B: Placebo   C: Combination
#> ————————————————————————————————————————————————————————————
#> # (full_parent_df)      400         400            400      
#> # (.alt_df_full)        400         400            400      
#> # (df)                  134         134            132

# set default for .alt_df_full to full_parent_df when not passed by rtables splitting machinery
# this would be the desired passing when no alt_counts_df has been supplied in build_table call

myafun3 <- function(df, .var, .alt_df_full = .spl_context$full_parent_df[[1]], .spl_context){
  xx <- .alt_df_full
  yy <- .spl_context$full_parent_df[[1]]
  in_rows(.list = list(full_parent_df = NROW(yy), 
                       .alt_df_full = NROW(xx),
                       df = NROW(df)),
          .labels = c("# (full_parent_df)",
                      "# (.alt_df_full)",
                      "# (df)"))
}

lyt3 <- basic_table() |> 
  split_cols_by("ARM") |> 
  analyze(vars = "STUDYID", afun = myafun3)

build_table(lyt3, adsl, alt_counts_df = adsl)
#>                      A: Drug X   B: Placebo   C: Combination
#> ————————————————————————————————————————————————————————————
#> # (full_parent_df)      400         400            400      
#> # (.alt_df_full)        400         400            400      
#> # (df)                  134         134            132
build_table(lyt3, adsl)
#>                      A: Drug X   B: Placebo   C: Combination
#> ————————————————————————————————————————————————————————————
#> # (full_parent_df)      400         400            400      
#> # (.alt_df_full)        400         400            400      
#> # (df)                  134         134            132

Created on 2026-05-12 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