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

droplevels() not supported in tm_g_lineplot() #757

Closed weijie25 closed 1 year ago

weijie25 commented 1 year ago

Hi there,

Is there a way to drop levels of a factor in tm_g_lineplot? Right now I have different studies in ADSL and when I tried to visualize just one study, treatment arms in other studies are still shown in the legend. Thank you!

shajoezhu commented 1 year ago

Hi @weijie25 , thanks for raising the issue. Can you provide us some reproduceble code or screenshots for this?

weijie25 commented 1 year ago

Hi @weijie25 , thanks for raising the issue. Can you provide us some reproduceble code or screenshots for this?

Thanks for the reply. Here is a toy example:


> ## Uncomment the following 2 lines if you are running the sample app on BEE
> source("https://raw.github.roche.com/NEST/nest_on_bee/master/bee_nest_utils.R")
> bee_use_nest(release = "2022_10_13")
> 
> 
> options(shiny.useragg = FALSE)
> 
> library(tidyverse)
> library(scda)
> library(scda.2021)
> library(teal.modules.general)
> library(teal.modules.clinical)
> library(nestcolor)
> library(sparkline)
> 
> # Toy Teal App for Debugging ------------------------------
> 
> ## Generate Data - ADSL
> ADLB = data.frame(STUDYID=c(rep("STUDY1", 100), rep("STUDY2", 100)),
>                   USUBJID=c(rep(paste0("STUDY1-", 1:5), each=20), rep(paste0("STUDY2-", 1:5), each=20)),
>                   ACTARM=c(rep("S1 ARM1", 60), rep("S1 ARM2", 40), rep("S2 ARMA", 60), rep("S2 ARMB", 40))
>                   )
> 
> 
> 
> ADLB = ADLB %>%
>   group_by(STUDYID, USUBJID) %>%
>   mutate(PARAMCD = rep(c("TEST1", "TEST2"), each=10),
>          AVAL=rnorm(sd = 3, n=20),
>          AVALU="UNIT",
>          AVISIT=rep(c("Baseline", paste0("Week", 1:9)), 2))
> 
> ADLB = ADLB %>%
>   filter(AVISIT!="Baseline") %>%
>   group_by(STUDYID, USUBJID, PARAMCD) %>%
>   mutate(ATOXGR = ifelse(abs(AVAL)>4, 4*sign(AVAL) , as.integer(AVAL))) %>%
>   bind_rows(ADLB %>% filter(AVISIT=="Baseline"))
> 
> 
> ADLB=ADLB %>%
>   drop_na(ATOXGR) %>%
>   group_by(STUDYID, USUBJID, PARAMCD) %>%
>   mutate(WGRHIFL = ifelse(ATOXGR==max(ATOXGR), "Y", ""),
>          WGRLOFL = ifelse(ATOXGR==min(ATOXGR), "Y", "")) %>%
>   bind_rows(ADLB%>%filter(is.na(ATOXGR))) %>%
>   ungroup()
> 
> ADLB = ADLB %>%
>   mutate(ATOXGR = as.factor(ATOXGR),
>          WGRHIFL = as.factor(WGRHIFL),
>          WGRLOFL = as.factor(WGRLOFL),
>          PARAMCD = as.factor(PARAMCD),
>          ACTARM = as.factor(ACTARM)
>   ) %>%
>   filter(!is.na(ATOXGR))
> 
> # Hmisc::label(ADLB) = as.list(colnames(ADLB))
> 
> ADSL = ADLB %>% select(STUDYID, USUBJID, ACTARM) %>% distinct()
> 
> ## Setup App
> app <- teal::init(
>   data = cdisc_data(
>     cdisc_dataset("ADSL", ADSL),
>     # cdisc_dataset("ADAE", ADAE),
>     # cdisc_dataset("ADEX", ADEX, parent = "ADSL"),
>     cdisc_dataset("ADLB", ADLB),
>     # cdisc_dataset("ADVS", ADVS),
>     check = FALSE
>   ),
>   modules = modules(
>     tm_front_page(
>       label = "Study Information",
>       header_text = c("Info about data source" = "Random data are used that have been created with the 'scda' R package"),
>       tables = list(`NEST packages used` = data.frame(Packages = c("teal.modules.general", "teal.modules.clinical", "scda", "scda.2021")))
>     ),
>     modules(
>       label = "Lab Tables",
>       tm_t_summary_by(
>         label = "Labs Summary",
>         dataname = "ADLB",
>         arm_var = choices_selected(
>           choices = variable_choices(ADSL, c("ACTARM")),
>           selected = "ACTARM"
>         ),
>         by_vars = choices_selected(
>           choices = variable_choices(ADLB, c("PARAMCD", "AVISIT")),
>           selected = c("PARAMCD", "AVISIT"),
>           fixed = TRUE
>         ),
>         summarize_vars = choices_selected(
>           choices = variable_choices(ADLB, c("AVAL")),
>           selected = c("AVAL")
>         ),
>         paramcd = choices_selected(
>           choices = value_choices(ADLB, "PARAMCD"),
>           selected = "TEST1"
>         )
>       ),
>       tm_g_lineplot(
>         label = "Lab Summary Plot",
>         dataname = "ADLB",
>         strata = choices_selected(
>           choices = variable_choices(ADSL, c("ACTARM")),
>           selected = "ACTARM"
>         ),
>         x=choices_selected(choices = variable_choices(ADLB, "AVISIT"),
>                            selected = "AVISIT",
>                            fixed=TRUE),
>         y= choices_selected(choices = variable_choices(ADLB, c("AVAL")),
>                             selected = "AVAL",
>                             fixed=TRUE),
>         y_unit = choices_selected(choices = variable_choices(ADLB, "AVALU"),
>                                   selected = "AVALU",
>                                   fixed=TRUE),
>         conf_level = choices_selected(c(0.95, 0.9, 0.8), 0.95, keep_order = TRUE),
>         param=choices_selected(
>           value_choices(ADLB, "PARAMCD"),
>           selected = "TEST1")
>       ),
>       tm_t_abnormality_by_worst_grade(
>         label = "Laboratory test results with highest grade post-baseline",
>         dataname = "ADLB",
>         arm_var = choices_selected(
>           choices = variable_choices(ADSL, c("ACTARM")),
>           selected = "ACTARM"
>         ),
>         paramcd = choices_selected(
>           choices = value_choices(ADLB, "PARAMCD"),
>           selected = c("TEST1")
>         ),
>         add_total = FALSE
>       )
>     )
>   ),
>   header = div(
>     class = "",
>     style = "margin-bottom: 2px;",
>     tags$h1("Pediatric Safety Data Mart Dashboard", tags$span("SPA", class = "pull-right"))
>   ),
>   footer = tags$p(class = "text-muted", "Source: teal.gallery package"),
>   # Set initial filter state as safety-evaluable population
>   filter = list(
>     ADSL = list(STUDYID="STUDY1"),
>     ADLB = list()
>   )
> )
> 
> shinyApp(app$ui, app$server)

You can see that lineplot shows all arms despite selecting only one study.

I did cross-post this on Overflow. Just wanted to mention that so this issue won't occupy multiple people's time.

Melkiades commented 1 year ago

Thanks for reporting and for the precise test code/case. I copied the one in Overflow, remember next time to not have the > when you copy them ;)

weijie25 commented 1 year ago

Thanks for reporting and for the precise test code/case. I copied the one in Overflow, remember next time to not have the > when you copy them ;)

Didn't realize I quoted the entire code, sorry about the inconvenience! Thanks for looking into this :- )

Melkiades commented 1 year ago

With that PR it will be fixed. Just if you tried with your test to see abnormality by worst grade, there was a mysterious error. Now, the error message is more informative. From your side just add ADLB <- formatters::var_relabel(ADLB, PARAMCD = "Paramcd") (with the correct label ofc) before the app call so it will not crash.

weijie25 commented 1 year ago

With that PR it will be fixed. Just if you tried with your test to see abnormality by worst grade, there was a mysterious error. Now, the error message is more informative. From your side just add ADLB <- formatters::var_relabel(ADLB, PARAMCD = "Paramcd") (with the correct label ofc) before the app call so it will not crash.

Thank you! I am still seeing all the levels and wondering how I could use the new version with this issue fixed? Do I need to re-install teal.module.clinical? Thank you!

Melkiades commented 1 year ago

Thank you! I am still seeing all the levels and wondering how I could use the new version with this issue fixed? Do I need to re-install teal.module.clinical? Thank you!

yes, you must have it installed on the most recent version (check it in your R console, it should be 0.8.14.9038 or higher). We are working to have a nice versioning system so it should work also for the dependencies

weijie25 commented 1 year ago

Thank you! I am still seeing all the levels and wondering how I could use the new version with this issue fixed? Do I need to re-install teal.module.clinical? Thank you!

yes, you must have it installed on the most recent version (check it in your R console, it should be 0.8.14.9038 or higher). We are working to have a nice versioning system so it should work also for the dependencies

Thanks! Is there a way to install this new version? I tried remotes::install_github("insightsengineering/teal.modules.clinical@*release"), but it seems to still install the 0.8.14 version without the extra digits: teal.modules.clinical_0.8.14 in the session info.

Melkiades commented 1 year ago

I think remotes::install_github("insightsengineering/teal.modules.clinical) should install main