insightsengineering / teal.modules.clinical

Provides teal modules for the standard clinical trials outputs
https://insightsengineering.github.io/teal.modules.clinical/
Other
32 stars 17 forks source link

[Feature Request]: Display output in the order of the coded selected variables #1096

Open Nolan-Steed opened 6 months ago

Nolan-Steed commented 6 months ago

Feature description

It would be nice if the selected variables in the summarize_vars argument would be displayed in the order that they are programmed. For example in this code below, I have selected c('SEX', 'COUNTRY', 'AGE','RACE') and I want the demographic table to be displayed in that order. But the order that is displayed is not in this order, it is displayed by the order of the variables in the choices argument, if that makes sense. Often studies want the ability to have a large choices selection and it doesn't make sense to order this list manually. So I want the demographic table to be in the order 'SEX', 'COUNTRY', 'AGE','RACE', but it is actually displayed in the order 'AGE', 'SEX', 'RACE', 'COUNTRY'.

library(shiny)
library(teal.widgets)
library(teal.modules.clinical)

get_fact_vars <- function(data) {
  fact_vars <- names(data)
  return(fact_vars)
}

cs <- choices_selected(
  choices = variable_choices('ADS', subset = get_fact_vars),
  selected = c('SEX', 'COUNTRY', 'AGE','RACE')
)

## Data reproducible code
data <- teal_data()
data <- within(data, {
  library(scda)
  ADSL <- synthetic_cdisc_dataset("latest", "adsl")

  # Include `EOSDY` and `DCSREAS` variables below because they contain missing data.
  stopifnot(
    any(is.na(ADSL$EOSDY)),
    any(is.na(ADSL$DCSREAS))
  )
})
datanames <- "ADSL"
datanames(data) <- datanames
join_keys(data) <- default_cdisc_join_keys[datanames]

## Setup App
app <- init(
  data = data,
  modules = modules(
    tm_t_summary(
      label = "Demographic Table",
      dataname = "ADSL",
      arm_var = choices_selected(c("ARM", "ARMCD"), "ARM"),
      summarize_vars = cs,
      useNA = "ifany"
    )
  )
)

shinyApp(app$ui, app$server)
sessionInfo()
R version 4.3.0 (2023-04-21 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22621)

Matrix products: default

locale:
[1] LC_COLLATE=English_Europe.utf8  LC_CTYPE=English_Europe.utf8   
[3] LC_MONETARY=English_Europe.utf8 LC_NUMERIC=C                   
[5] LC_TIME=English_Europe.utf8    

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] teal_0.15.1.9003          teal.slice_0.5.0.9004     teal.data_0.5.0          
[4] teal.code_0.5.0.9001      shiny_1.8.0               teal.transform_0.5.0.9000
[7] teal.widgets_0.4.2.9005  

loaded via a namespace (and not attached):
 [1] jsonlite_1.8.8         dplyr_1.1.4            compiler_4.3.0        
 [4] promises_1.2.0.1       tidyselect_1.2.0       Rcpp_1.0.10           
 [7] jquerylib_0.1.4        later_1.3.1            fastmap_1.1.1         
[10] mime_0.12              R6_2.5.1               generics_0.1.3        
[13] backports_1.4.1        checkmate_2.3.1        tibble_3.2.1          
[16] logger_0.2.2           bslib_0.6.1            pillar_1.9.0          
[19] rlang_1.1.1            utf8_1.2.4             cachem_1.0.8          
[22] httpuv_1.6.11          sass_0.4.8             memoise_2.0.1         
[25] cli_3.6.1              magrittr_2.0.3         shinyWidgets_0.7.6    
[28] digest_0.6.33          rstudioapi_0.15.0      xtable_1.8-4          
[31] lifecycle_1.0.4        teal.logger_0.1.3.9007 vctrs_0.6.5           
[34] glue_1.6.2             fansi_1.0.6            tools_4.3.0           
[37] pkgconfig_2.0.3        ellipsis_0.3.2         htmltools_0.5.7    

Code of Conduct

Contribution Guidelines

Security Policy

m7pr commented 6 months ago

This is a follow-up after https://github.com/insightsengineering/teal.transform/issues/205 where we discovered that order of the output can be also orchestrated by manually editing cs$choices, like

cs$choices[1:4] <- cs$choices[c(2, 4, 1, 3)]