insightsengineering / rtables

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

cbinding issue with combo_df #891

Closed Melkiades closed 2 months ago

Melkiades commented 3 months ago

The following issue is coming from a similar one in the tlg catalog. it was working before col_counts PR. There are different ways to work this in (changing split_fun) but I am wondering if this is a bug @gmbecker

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
combodf <- tibble::tribble(
  ~valname, ~label, ~levelcombo, ~exargs,
  "all_X", "All Drug X", c("A: Drug X", "C: Combination"), list(),
  "all_pt", "All Patients", c("A: Drug X", "B: Placebo", "C: Combination"), list()
)
lyt <- basic_table(show_colcounts = TRUE) %>%
  split_cols_by(
    "ACTARM",
    split_fun = add_combo_levels(combodf)
  ) %>%
  analyze("BMRKR1")

result <- build_table(lyt, ex_adsl)

# Change the column order.
result <- cbind_rtables(result[, 1], result[, 3]) %>%
  cbind_rtables(result[, 4]) %>%
  cbind_rtables(result[, 2]) %>%
  cbind_rtables(result[, 5])
#> Error in kids[[kidmatch]]: subscript out of bounds

Created on 2024-06-25 with reprex v2.1.0

Melkiades commented 3 months ago

Also, is there a simpler way to resort the columns?

Melkiades commented 3 months ago

This is a way:

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
reorder_facets <- function(splret, spl, fulldf, ...) {
  ord <- c(1, 3, 4, 2, 5)
  make_split_result(
    splret$values[ord],
    splret$datasplit[ord],
    splret$labels[ord]
  )
}

custom_column_split_fun <- make_split_fun(
  post = list(
    add_combo_facet("all_X",
                    label = "All Drug X",
                    levels = c("A: Drug X", "C: Combination")
    ),
    add_combo_facet("all_pt",
                    label = "All Patients",
                    levels = c("A: Drug X", "B: Placebo", "C: Combination")
    ),
    reorder_facets
  )
)

lyt <- basic_table(show_colcounts = TRUE) %>%
  split_cols_by(
    "ACTARM",
    split_fun = custom_column_split_fun
  ) %>%
  analyze("BMRKR1")

build_table(lyt, ex_adsl)
#>        A: Drug X   C: Combination   All Drug X   B: Placebo   All Patients
#>         (N=134)       (N=132)        (N=266)      (N=134)       (N=400)   
#> ——————————————————————————————————————————————————————————————————————————
#> Mean     5.97           5.62           5.80         5.70          5.76

Created on 2024-06-28 with reprex v2.1.0

Is there one to do this in post-processing @gmbecker?

Melkiades commented 2 months ago

Closing this as the workaround solves the issue nicely and it is ultimately the right way to go for this.