Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delays transform modules reactivity until tab is active #1373

Merged
merged 27 commits into from
Oct 14, 2024

Conversation

averissimo
Copy link
Contributor

@averissimo averissimo commented Oct 9, 2024

Pull Request

Fixes #1303

Changes description

  • Unifying function for delayed trigger of module and transformations
  • Filter manager crash when clicked with an app that has module specific filters
  • Fix bug detected when app is called with teal_data_module
    • One of my testing apps is failing (see below)
  • Add tests

Topics to discuss

  • Functionality change: this PR will delay the first module reactivity execution until data is pulled from teal_data_module
Sample app for bug
options(
  teal.log_level = "INFO",
  teal.show_js_log = TRUE,
  # teal.bs_theme = bslib::bs_theme(version = 5),
  shiny.bookmarkStore = "server"
)

# pkgload::load_all("../teal.data")
# pkgload::load_all("../teal.slice")
pkgload::load_all("../teal")

my_transformers <- list(
  teal_transform_module(
    label = "Keep first 6 from IRIS",
    ui = function(id) {
      ns <- NS(id)
      div(
        checkboxInput(ns("check"), label = "Toggle `head(iris)`"),
      )
    },
    server = function(id, data) {
      moduleServer(id, function(input, output, session) {
        eventReactive(input$check, {
          print("Check triggered")
          req(data())
          if (input$check) {
            within(data(), iris <- head(iris, 6))
          } else {
            data()
          }
        })
      })
    }
  )
)


data <- teal::teal_data_module(
  ui = function(id) {
    ns <- shiny::NS(id)
    shiny::tagList(
      shiny::tags$head(
        shiny::tags$style(shiny::HTML("
          .teal-data-module {
            border: 1px solid rgba(0, 0, 0, .5);
            border-radius: 4px;
            padding: 1em;
            margin: .2em;
          }
          .teal-data-module .shiny-options-group {
            display: flex;
            flex-wrap: wrap;
            column-gap: 1em;
          }
          .teal-data-module .shiny-options-group  .checkbox {
            margin-top: 1em;
            margin-bottom: 0;
          }
        "))
      ),
      shiny::tags$h2("Data Module"),
      shiny::div(
        class = "teal-data-module",
        shiny::checkboxGroupInput(
          ns("datasets"),
          "Datasets",
          choices = c("ADSL", "ADTTE", "iris", "CO2", "miniACC"),
          selected = c("ADSL", "ADTTE", "iris", "CO2")
        ),
        shiny::actionButton(ns("submit"), label = "Submit")
      )
    )
  },
  server = function(id, ...) {
    shiny::moduleServer(id, function(input, output, session) {
      code <- list(
        ADSL = expression(ADSL <- teal.data::rADSL),
        ADTTE = expression({
          ADTTE <- teal.data::rADTTE
          ADTTE$CNSRL <- as.logical(ADTTE$CNSR)
        }),
        iris = expression(iris <- iris),
        CO2 = expression({
          CO2 <- CO2
          factors <- names(Filter(isTRUE, vapply(CO2, is.factor, logical(1L))))
          CO2[factors] <- lapply(CO2[factors], as.character)
        }),
        miniACC = expression({
          data(
            "miniACC",
            package = "MultiAssayExperiment",
            envir = environment(),
            overwrite = TRUE
          )
          miniACC <- miniACC
        })
      )

      datasets <- reactive(input$datasets)

      shiny::eventReactive(input$submit, {
        code_to_eval <- do.call(c, code[datasets()])
        data <- teal.code::eval_code(teal.data::teal_data(), code_to_eval)

        join_keys(data) <- default_cdisc_join_keys[datasets()]
        teal.data::datanames(data) <- datasets()
        data
      })
    })
  }, once = FALSE
)

teal::init(
  data = data,
  modules = teal::modules(
    teal::example_module(label = "A", datanames = NULL, transformers = my_transformers),
    teal::example_module(label = "B", transformers = my_transformers)
  ),
  filter = teal::teal_slices(
    # # FilterRange
    teal.slice::teal_slice("ADSL", "AGE", selected = c(18L, 65L)),
    # # FilterExpr
    teal_slice(
      dataname = "ADSL",
      id = "Female adults",
      expr = "SEX == 'F' & AGE >= 18",
      title = "Female adults"
    ),
    # # FilterDatetime
    teal_slice(
      dataname = "ADTTE",
      varname = "ADTM",
      id = "Analysis DTM",
      selected = c("2019-03-25 07:06:18", "2020-01-22 15:03:58"),
      title = "Female adults"
    ),
    # # FilterDate with LSTALVDT
    teal_slice(
      dataname = "ADSL",
      varname = "LSTALVDT",
      id = "Last Alive Date",
      selected = c("2022-02-14", "2022-11-24"),
      title = "Last Alive Date"
    ),
    # FilterEmpty
    # FilterLogical with CNSRL
    teal_slice(
      dataname = "ADTTE",
      varname = "CNSRL",
      id = "Censored",
      selected = TRUE,
      title = "Censored"
    ),
    module_specific = FALSE,
    teal.slice::teal_slice("ADSL", "SEX")
  ),
  title = "yada"
) |>
  shiny::runApp()

@averissimo averissimo added the core label Oct 9, 2024
@averissimo averissimo changed the title Transform modules are triggered Delays transform modules reactivity until tab is active Oct 9, 2024
R/module_nested_tabs.R Outdated Show resolved Hide resolved
R/module_nested_tabs.R Outdated Show resolved Hide resolved
@gogonzo gogonzo self-assigned this Oct 10, 2024
…03_postpone_transformer@main

* origin/1303_documentation@main:
  move the note between files
  add a note to teal_data_module constructor in its vignette about how the reactivity should be handled
R/module_teal_data.R Outdated Show resolved Hide resolved
@gogonzo gogonzo marked this pull request as ready for review October 11, 2024 03:40
Copy link
Contributor

github-actions bot commented Oct 11, 2024

Unit Tests Summary

  1 files   25 suites   8m 15s ⏱️
255 tests 251 ✅ 4 💤 0 ❌
512 runs  508 ✅ 4 💤 0 ❌

Results for commit d885b10.

♻️ This comment has been updated with latest results.

Copy link
Contributor

github-actions bot commented Oct 11, 2024

Unit Test Performance Difference

Test Suite $Status$ Time on main $±Time$ $±Tests$ $±Skipped$ $±Failures$ $±Errors$
module_teal 💚 $53.03$ $-3.95$ $+4$ $0$ $0$ $0$
shinytest2-module_bookmark_manager 💚 $35.98$ $-1.13$ $0$ $0$ $0$ $0$
shinytest2-reporter 💚 $68.43$ $-1.38$ $0$ $0$ $0$ $0$
Additional test case details
Test Suite $Status$ Time on main $±Time$ Test Case
module_teal 👶 $+0.45$ are_called_only_after_teal_data_module_is_resolved
module_teal 👶 $+0.38$ throws_warning_when_transformer_return_reactive.event

Results for commit ed38de6

♻️ This comment has been updated with latest results.

R/module_filter_manager.R Outdated Show resolved Hide resolved
R/module_nested_tabs.R Outdated Show resolved Hide resolved
averissimo and others added 2 commits October 11, 2024 08:43
Co-authored-by: Dawid Kałędkowski <[email protected]>
Signed-off-by: André Veríssimo <[email protected]>
@averissimo averissimo requested a review from gogonzo October 11, 2024 11:42
Copy link
Contributor

github-actions bot commented Oct 11, 2024

badge

Code Coverage Summary

Filename                          Stmts    Miss  Cover    Missing
------------------------------  -------  ------  -------  ----------------------------------------------------------------------------------------------------------------------------------------
R/checkmate.R                        24       0  100.00%
R/dummy_functions.R                  47      11  76.60%   27, 29, 41, 52-59
R/get_rcode_utils.R                  12       0  100.00%
R/include_css_js.R                   22      17  22.73%   12-38, 76-82
R/init.R                            108      50  53.70%   107-114, 160-169, 171, 183-204, 229-232, 239-245, 248-249, 251
R/landing_popup_module.R             25      25  0.00%    61-87
R/module_bookmark_manager.R         158     127  19.62%   47-68, 88-138, 143-144, 156, 203, 238-315
R/module_data_summary.R             189      68  64.02%   24-52, 93, 187, 227-267
R/module_filter_data.R               64       2  96.88%   22-23
R/module_filter_manager.R           230      57  75.22%   56-62, 73-82, 90-95, 108-112, 117-118, 291-314, 340, 367, 379, 386-387
R/module_init_data.R                 68       0  100.00%
R/module_nested_tabs.R              238      93  60.92%   40-144, 176, 201-203, 322, 362
R/module_snapshot_manager.R         216     146  32.41%   89-95, 104-113, 121-133, 152-153, 170-180, 184-199, 201-208, 215-230, 234-238, 240-246, 249-262, 265-273, 304-318, 321-332, 335-341, 355
R/module_teal_data.R                152      10  93.42%   41-48, 84, 135-136
R/module_teal_lockfile.R            131      44  66.41%   32-36, 44-56, 59-61, 75, 85-87, 99-101, 109-118, 121, 123, 125-126, 160-161
R/module_teal_with_splash.R          12      12  0.00%    22-38
R/module_teal.R                     190      87  54.21%   48-143, 158, 184-185, 216
R/module_transform_data.R            56      32  42.86%   17-51
R/modules.R                         181      32  82.32%   166-170, 225-228, 326-327, 359-373, 411, 423-431
R/reporter_previewer_module.R        19       2  89.47%   30, 34
R/show_rcode_modal.R                 24      24  0.00%    17-42
R/tdata.R                            14      14  0.00%    19-61
R/teal_data_module-eval_code.R       24       0  100.00%
R/teal_data_module-within.R           7       0  100.00%
R/teal_data_module.R                 52       0  100.00%
R/teal_data_utils.R                  32       0  100.00%
R/teal_reporter.R                    68       6  91.18%   69, 77, 125-126, 129, 146
R/teal_slices-store.R                29       0  100.00%
R/teal_slices.R                      63       0  100.00%
R/TealAppDriver.R                   353     353  0.00%    52-735
R/utils.R                           203       0  100.00%
R/validate_inputs.R                  32       0  100.00%
R/validations.R                      58      37  36.21%   110-377
R/zzz.R                              15      11  26.67%   4-18
TOTAL                              3116    1260  59.56%

Diff against main

Filename                  Stmts    Miss  Cover
----------------------  -------  ------  --------
R/module_nested_tabs.R      +18       0  +3.20%
R/module_teal_data.R          0      -1  +0.66%
R/teal_data_module.R         +9       0  +100.00%
TOTAL                       +27      -1  +0.39%

Results for commit: d885b10

Minimum allowed coverage is 80%

♻️ This comment has been updated with latest results

R/teal_data_module.R Outdated Show resolved Hide resolved
Copy link
Contributor

@gogonzo gogonzo left a comment

Choose a reason for hiding this comment

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

👍

@averissimo averissimo merged commit 68be755 into main Oct 14, 2024
28 checks passed
@averissimo averissimo deleted the 1303_postpone_transformer@main branch October 14, 2024 08:30
@github-actions github-actions bot locked and limited conversation to collaborators Oct 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Document the different behaviour of active tab and hidden tab when using Data module
3 participants