Skip to content

Add picks variable selection to ae_oview module#331

Open
osenan wants to merge 34 commits into
mainfrom
oriol_ae_oview_picks@main
Open

Add picks variable selection to ae_oview module#331
osenan wants to merge 34 commits into
mainfrom
oriol_ae_oview_picks@main

Conversation

@osenan
Copy link
Copy Markdown

@osenan osenan commented Mar 12, 2026

Pull Request

This partially closes #330

I based my changes in this PR.

Please compare both modules with this example app (requires to have teal.picks installed, osprey)

library("teal")
library("teal.picks")
devtools::load_all()

data <- teal_data() %>%
  within({
    library(dplyr)
    ADSL <- teal.data::rADSL
    ADAE <- teal.data::rADAE
    .add_event_flags <- function(dat) {
      dat <- dat %>%
        mutate(
          TMPFL_SER = AESER == "Y",
          TMPFL_REL = AEREL == "Y",
          TMPFL_GR5 = AETOXGR == "5",
          AEREL1 = (AEREL == "Y" & ACTARM == "A: Drug X"),
          AEREL2 = (AEREL == "Y" & ACTARM == "B: Placebo")
        )
      labels <- c(
        "Serious AE", "Related AE", "Grade 5 AE",
        "AE related to A: Drug X", "AE related to B: Placebo"
      )
      cols <- c("TMPFL_SER", "TMPFL_REL", "TMPFL_GR5", "AEREL1", "AEREL2")
      for (i in seq_along(labels)) {
        attr(dat[[cols[i]]], "label") <- labels[i]
      }
      dat
    }
    ADAE <- .add_event_flags(ADAE)
  })

join_keys(data) <- default_cdisc_join_keys[names(data)]

app <- init(
  data = data,
  modules = modules(
    tm_g_ae_oview(
      label = "Common AE (picks)",
      flag_var_anl = teal.picks::picks(
        teal.picks::datasets("ADAE"),
        teal.picks::variables(
           choices = c("TMPFL_SER", "TMPFL_REL", "TMPFL_GR5", "AEREL1", "AEREL2"),
          selected = "TMPFL_SER"
        )
      ),
      arm_var = teal.picks::picks(
        teal.picks::datasets("ADSL"),
        teal.picks::variables(
          choices = teal.picks::is_categorical(min.len = 2),
          selected = "ACTARMCD"
        )
      ),
      plot_height = c(600, 200, 2000)
    ),
    # Default method (old) for comparison
    tm_g_ae_oview(
      label = "Common AE (default)",
      dataname = "ADAE",
      flag_var_anl = teal.transform::choices_selected(
        selected = "AEREL1",
        choices = teal.transform::variable_choices(
          data[["ADAE"]],
          c("TMPFL_SER", "TMPFL_REL", "TMPFL_GR5", "AEREL1", "AEREL2")
        )
      ),
      arm_var = teal.transform::choices_selected(
        selected = "ACTARMCD",
        choices = c("ACTARM", "ACTARMCD")
      ),
      plot_height = c(600, 200, 2000)
    )
  )
)

shinyApp(app$ui, app$server)

I refactored the tm_g_ae_oview module to create a generic, keep the default for teal.transform and add a method for picks variable selection. The module works as expected and setting the variables is better with picks. The issue reported in the similar PR on the plot dimensions is fixed if we set the plot dimensions in the module. The ui of the picks ui components is different and it breaks, in my opinion, the color pattern of the teal app.

image image

Here the teal.transform module

image image

I added simple tests for the module as well

@osenan osenan added the core label Mar 12, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 12, 2026


🎉 Thank you for your contribution! Before this PR can be accepted, we require that you read and agree to our Contributor License Agreement.
You can digitally sign the CLA by posting a comment on this Pull Request in the format shown below. This agreement will apply to this PR as well as all future contributions on this repository.


I have read the CLA Document and I hereby sign the CLA


osenan seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

@osenan osenan requested a review from a team March 12, 2026 11:38
@osenan
Copy link
Copy Markdown
Author

osenan commented Mar 12, 2026

I have read the CLA Document and I hereby sign the CLA

@osenan osenan marked this pull request as draft March 12, 2026 14:59
@osenan
Copy link
Copy Markdown
Author

osenan commented Mar 12, 2026

Changed to draft as I need to fix:

  • Checks
  • Plot
  • Decorators

Copy link
Copy Markdown
Contributor

@averissimo averissimo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some preliminary comments.

  • I believe this is a good case for only keeping 1 version of the module logic. (We can convert directly from choices_selected -> variables())
  • I see that you're using picks(), consider using variables() instead for arm_var and flag_var_anl
    • And then create picks() inside the tm_g_ae_oview() call

Comment thread R/tm_g_ae_oview.R Outdated
Comment thread DESCRIPTION Outdated
Comment thread R/tm_g_ae_oview_picks.R Outdated
Comment thread R/tm_g_ae_oview.R Outdated
Comment thread R/tm_g_ae_oview_picks.R Outdated
Comment thread R/tm_g_ae_oview_picks.R Outdated
Comment thread R/tm_g_ae_oview_picks.R Outdated
Comment thread R/tm_g_ae_oview_picks.R Outdated
Comment thread R/tm_g_ae_oview_picks.R Outdated
Comment thread R/tm_g_ae_oview_picks.R Outdated
osenan and others added 4 commits March 16, 2026 12:31
Co-authored-by: André Veríssimo <211358+averissimo@users.noreply.github.com>
Signed-off-by: Oriol Senan  <35930244+osenan@users.noreply.github.com>
Co-authored-by: André Veríssimo <211358+averissimo@users.noreply.github.com>
Signed-off-by: Oriol Senan  <35930244+osenan@users.noreply.github.com>
Co-authored-by: André Veríssimo <211358+averissimo@users.noreply.github.com>
Signed-off-by: Oriol Senan  <35930244+osenan@users.noreply.github.com>
Co-authored-by: André Veríssimo <211358+averissimo@users.noreply.github.com>
Signed-off-by: Oriol Senan  <35930244+osenan@users.noreply.github.com>
@osenan
Copy link
Copy Markdown
Author

osenan commented Mar 16, 2026

I have read the CLA Document and I hereby sign the CLA

@osenan osenan marked this pull request as ready for review March 17, 2026 06:45
@osenan
Copy link
Copy Markdown
Author

osenan commented Mar 17, 2026

The plot and decorator I will handle in a separate issue finally

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 17, 2026

badge

Code Coverage Summary

Filename                    Stmts    Miss  Cover    Missing
------------------------  -------  ------  -------  -----------------------
R/tm_g_ae_oview.R             274     210  23.36%   93-97, 101-105, 172-405
R/tm_g_ae_sub.R               288     288  0.00%    60-379
R/tm_g_butterfly.R            371     371  0.00%    125-534
R/tm_g_decorate.R              42      42  0.00%    17-91
R/tm_g_events_term_id.R       271     271  0.00%    63-364
R/tm_g_heat_bygrade.R         293     293  0.00%    136-459
R/tm_g_patient_profile.R      691     691  0.00%    159-913
R/tm_g_spiderplot.R           322     322  0.00%    100-464
R/tm_g_swimlane.R             345     345  0.00%    127-525
R/tm_g_waterfall.R            429     429  0.00%    109-590
R/utils.R                      91      80  12.09%   31-73, 107-240
R/zzz.R                         3       3  0.00%    4-7
TOTAL                        3420    3345  2.19%

Diff against main

Filename             Stmts    Miss  Cover
-----------------  -------  ------  -------
R/tm_g_ae_oview.R      +49     -15  +23.36%
R/utils.R               +6       0  +6.21%
TOTAL                  +55     -15  +2.04%

Results for commit: bce4924

Minimum allowed coverage is 80%

♻️ This comment has been updated with latest results

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 17, 2026

Unit Tests Summary

 1 files   2 suites   0s ⏱️
 8 tests  8 ✅ 0 💤 0 ❌
20 runs  20 ✅ 0 💤 0 ❌

Results for commit bce4924.

♻️ This comment has been updated with latest results.

@osenan osenan requested a review from averissimo March 17, 2026 11:56
Copy link
Copy Markdown
Contributor

@averissimo averissimo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! It works great and it even supports picks conversion for choices_selected

Comment thread R/tm_g_ae_oview.R Outdated
Comment thread R/tm_g_ae_oview.R Outdated
Comment thread R/tm_g_ae_oview.R
@averissimo averissimo self-assigned this Mar 17, 2026
osenan and others added 6 commits March 17, 2026 15:31
Co-authored-by: André Veríssimo <211358+averissimo@users.noreply.github.com>
Signed-off-by: Oriol Senan  <35930244+osenan@users.noreply.github.com>
Co-authored-by: André Veríssimo <211358+averissimo@users.noreply.github.com>
Signed-off-by: Oriol Senan  <35930244+osenan@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@averissimo averissimo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ready to be merged as it works as expected.

I have one last ask that both me and @m7pr have been doing in tmc and it also makes sense here.

Instead of S3 class we can just convert choices_selected in the body of the function, in essence support both without the overhead of S3.

Check this R/tm_t_pp_basic_info.R: https://github.com/insightsengineering/teal.modules.clinical/pull/1476/changes#diff-d50ab07375ab0f048cc866c3f6c0a0e7ed77c779483742bd3eebf05156f2a677

Comment thread R/tm_g_ae_oview.R Outdated
Comment thread tests/testthat/test-tm_g_ae_oview.R
@osenan osenan requested a review from averissimo April 8, 2026 09:25
Copy link
Copy Markdown
Contributor

@averissimo averissimo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's almost ready! 👍

My main request is about the default arguments, in this package it might be better to leave them <missing>.

The second main comment here is about the implementation of the conversion. I would prefer to keep this only *if really needed* as it breaks with best practices of R and this project.

I understand the requirements for the util, but maybe we can find a middle ground, see my suggestion and we can then apply "upstrean" on tmc

Comment thread R/tm_g_ae_oview.R Outdated
Comment thread R/utils.R Outdated
Comment thread R/tm_g_ae_oview.R Outdated
Comment thread R/tm_g_ae_oview.R
Comment thread R/utils.R Outdated
Comment thread R/tm_g_ae_oview.R Outdated
osenan and others added 2 commits April 9, 2026 16:44
Co-authored-by: André Veríssimo <211358+averissimo@users.noreply.github.com>
Signed-off-by: Oriol Senan  <35930244+osenan@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@averissimo averissimo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Seems fine by me

Comment thread R/utils.R Outdated
osenan and others added 2 commits April 10, 2026 15:17
Co-authored-by: André Veríssimo <211358+averissimo@users.noreply.github.com>
Signed-off-by: Oriol Senan  <35930244+osenan@users.noreply.github.com>
Comment thread R/tm_g_ae_oview.R
Comment on lines +55 to +56
#' arm_var = teal.picks::variables(
#' choices = dplyr::starts_with("ACTARM"),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not prefix functions in examples nor vignettes.
Only prefix in tests and in the code/implemetnation

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you move teal.picks in DESCRIPTION from Imports to Depends, you will not need to prefix those functions for the examples to work. As you didn't need to prefix teal.transform functions

Comment thread R/tm_g_ae_oview.R
Comment on lines +74 to +75
arm_var,
flag_var_anl,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know @averissimo suggested to drop default values, but we do have default values and the checks in tmc :P

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is not critical

Comment thread R/tm_g_ae_oview.R
Comment on lines +92 to +106
if (isTRUE(attr(arm_var$variables, "multiple"))) {
warning(
"`arm_var` accepts only a single variable selection. ",
"Forcing `teal.picks::variables(multiple)` to FALSE."
)
attr(arm_var$variables, "multiple") <- FALSE
}

if (isTRUE(attr(flag_var_anl$variables, "multiple"))) {
warning(
"`flag_var_anl` accepts only a single variable selection. ",
"Forcing `teal.picks::variables(multiple)` to FALSE."
)
attr(flag_var_anl$variables, "multiple") <- FALSE
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this possible to make it a wrapper, and reuse in all modules in this package? You could keep this wrapper in the main feature branch and reuse in single branches for specific modules.

Comment thread R/utils.R
Comment on lines +250 to +252
arg <- rlang::ensym(arg)
arg_name <- as.character(arg)
arg <- rlang::eval_tidy(arg, env = envir)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible use base R instead of rlang

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also rlang was not in imports/depends so you get this warning in R CMD CHECK
https://github.com/insightsengineering/teal.osprey/actions/runs/24245023719/job/70788946164?pr=331#step:44:98

)
})

testthat::it("fails when flag_var_anl is neiter picks or choices_selected", {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we agreed to add testthat:: prefix for all tests for all functions besides it function, as it collideas with some namespace. check other packages in teal family to see that it is barely prefixed

Comment thread DESCRIPTION
teal.code (>= 0.7.0),
teal.data (>= 0.8.0),
teal.logger (>= 0.4.0),
teal.picks (>= 0.1.0),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

teal.transform is in the Depends, and teal.picks is a substitute, so should also be in the Depends. the same we did in tmc

@m7pr m7pr self-assigned this Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request]: Allow modules to use teal.picks

4 participants