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

[Question]: About passing external variables to modules #1221

Open pzhang-cims opened 3 weeks ago

pzhang-cims commented 3 weeks ago

What is your question?

Hi there,

Thanks for the effort of developing teal.modules.clinical. I have a general question that how to pass external variables into the modules.

For example, consider a module using following code

    tm_t_summary(
      label = "ADSL", 
      dataname = "ADSL",
      arm_var = choices_selected(
        choices= variable_choices("ADSL",c('TRT01A', 'TRT01P'))
        selected = variable_choices("ADSL",'TRT01P')
      ),
      summarize_vars = choices_selected(
        variable_choices("ADSL")
      ),
      useNA = "ifany"
    )

In this case, we fix the variables of 'TRT01A', and 'TRT01P' as choices and 'TRT01P' as selected. We are wondering, if there is a way that, we can pass variable from external file, or server environment that, say, a variable called arm_selected = c('TRT01A','TRT01P') and we will have choices= variable_choices("ADSL", arm_selected). I have tried multiple ways:

1) By creating a module server for teal_data_module. But, if we define arm_selected' inside of it, we cannot call it in to the module 2) By definingarm_selected' within a teal_data(), similarly it cannot pass to the module.

Would you please advise if this use case is plausible in teal.modules.clinical?

Code of Conduct

Contribution Guidelines

Security Policy

pzhang-cims commented 3 weeks ago

We tried the approach from https://insightsengineering.github.io/teal.modules.clinical/latest-tag/reference/tm_t_events_summary.html. But this seems not work for a data from teal_data_module()

pzhang-cims commented 3 weeks ago

I would like to answer for myself that which is a temporary solution:

1) In your teal data module, if you have any external variables, you can put it into global environment, e.g.

list2env(var_select, envir = .GlobalEnv) to global environment

2) then in variable_choices(), with subsect function, using the variable name in globalenv()

e.g. choices=variable_choices("ADSL",subset = function(data){globalenv()$base_var$trt_option}),

pzhang-cims commented 3 weeks ago

If only one variable is allowed by variable_choices, then one can go to the module by using

tmp<-tm_t_events(....) set value for tmp$ui_args$hlt$select$selected, tmp$server_args$hlt$select$selected by global variables.

Melkiades commented 2 days ago

I would like to answer for myself that which is a temporary solution:

  1. In your teal data module, if you have any external variables, you can put it into global environment, e.g.

list2env(var_select, envir = .GlobalEnv) to global environment

  1. then in variable_choices(), with subsect function, using the variable name in globalenv()

e.g. choices=variable_choices("ADSL",subset = function(data){globalenv()$base_var$trt_option}),

I am happy this "hack" works :) but I am sure someone from the core team can help you best. I am not sure if the data can be changed within {teal.data} as you mentioned above. Any suggestions? @donyunardi @vedhav

pzhang-cims commented 2 days ago

Thank you @Melkiades for catching this up! I understand that {teal} is developed for study-level purpose. Developer can deploy one app to one sever for study. From our side, we want to see if one hosted server can work for different studies. It's good to see we have a function teal_data_module() to allow data upload. We might want to see if external files can be uploaded (e.g., mapping file), so that a server can be generalized for multiple studies, given limited server

donyunardi commented 2 days ago

Functional subset is the proper way to do this when using teal_data_module, but unfortunately, we have a bug on this functionality.

We'll planning to tackle this soon, in the meantime, I think your temporary solution (surfacing the object globally) sounds plausible. 👍🏼

pzhang-cims commented 2 days ago

@donyunardi thank you for keeping us updated and good to know it's on the track. If possible let me know when such updates reflect on CRAN so that we can adapt accordingly. Can't wait to see that.