Skip to content

[Bug]: Reporter fails with teal as a Shiny module #1615

@mschaffer-incyte

Description

@mschaffer-incyte

What happened?

Using the latest development version of teal, I'm running into issues trying to use teal as a Shiny module (ref: https://github.com/insightsengineering/teal/blob/main/vignettes/teal-as-a-shiny-module.Rmd). In the code below, a wrapper is created to dynamically use predefined teal data sets and teal modules based on a selected study name.

Code

library(shiny)
library(bslib)
library(teal)
library(teal.data)
library(teal.widgets)
library(teal.reporter)

options(shiny.legacy.datatable = TRUE)


# Define selectable studies with datasets and study-specific modules
studies <- c("Study1", "Study2")

datasets <- list(
  "Study1" = within(teal.data::teal_data(), { iris <- iris; mtcars <- mtcars}),
  "Study2" = within(teal.data::teal_data(), { df <- data.frame(a = 1:10, b = letters[1:10]) } )
)

mods <- list(
  "Study1" = teal::modules(teal::example_module(), label = "Study1 modules"),
  "Study2" = teal::modules(teal::example_module(), teal.modules.general::tm_variable_browser(), label="Study2 modules")
)


ui_app <- bslib::page_fluid(
  titlePanel("Dynamic teal modules"),
  selectInput("study_id", "Select study", choices = studies, multiple = FALSE),
  uiOutput("teal_ui"),
  ui_session_info("teal-footer-session_info")
)

srv_app <- function(input, output, session) {
  
  reporter <- teal.reporter::Reporter$new()
  
  current_modules <- reactive({
    req(input$study_id)
    mods[[input$study_id]]
  })
  
  current_datasets <- reactive({
    req(input$study_id)
    datasets[[input$study_id]]
  })
  
  output$teal_ui <- renderUI({
    mods <- current_modules()
    req(!is.null(mods))
    ui_teal(id = "teal", modules = mods)
  })
  
  observe({
    curr_mods <- current_modules()
    curr_ds <- current_datasets()
    req(!is.null(curr_mods), length(curr_ds) > 0)
    srv_teal("teal", data = curr_ds, modules = curr_mods, reporter = reporter)
  })
  
  srv_session_info("teal-footer-session_info")
}

shinyApp(ui_app, srv_app)

Issue

While this code works fine to dynamically switch between studies and load different data sets with study-specifc teal modules, using the report when toggling between studies leads to reporter issues and ultimately application crashes. I understand this is the development branch and reporter may be undergoing refactoring, but wanted to highlight the issue and/or get advice for correcting the code above.

Steps to reproduce

  1. Run app.
  2. With 'Study1' selected, click 'Add to Report' and name card.
  3. Click 'Report' > 'Preview Report' to confirm card creation.
  4. Switch to 'Study2'.

App crash message

Warning: Error in observe: Assertion on 'card' failed: Must inherit from class 'ReportCard'/'teal_card', but has class 'NULL'.
  88: <Anonymous>
  87: stop
  86: mstop
  85: makeAssertion
  84: checkmate::assert_multi_class
  83: observe
  82: <observer:observeEvent(input$add_card_ok)>
   3: runApp
   2: print.shiny.appobj
   1: <Anonymous>

sessionInfo()

R version 4.4.1 (2024-06-14)
Platform: aarch64-apple-darwin20
Running under: macOS Sonoma 14.7

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/New_York
tzcode source: internal

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

other attached packages:
[1] teal.reporter_0.5.0.9009 teal.widgets_0.5.0.9000  teal_1.0.0.9009          teal.slice_0.7.0         teal.data_0.8.0.9000     teal.code_0.7.0          bslib_0.9.0             
[8] shiny_1.10.0            

loaded via a namespace (and not attached):
 [1] tidyr_1.3.1                     plotly_4.10.4                   sass_0.4.9                      generics_0.1.3                  digest_0.6.37                   magrittr_2.0.3                 
 [7] evaluate_1.0.3                  grid_4.4.1                      RColorBrewer_1.1-3              fastmap_1.2.0                   jsonlite_2.0.0                  ggrepel_0.9.6                  
[13] backports_1.5.0                 formatR_1.14                    promises_1.3.2                  httr_1.4.7                      purrr_1.0.4                     viridisLite_0.4.2              
[19] scales_1.4.0                    lazyeval_0.2.2                  jquerylib_0.1.4                 cli_3.6.5                       teal.transform_0.7.0            rlang_1.1.6                    
[25] withr_3.0.2                     cachem_1.1.0                    tools_4.4.1                     memoise_2.0.1                   checkmate_2.3.2                 dplyr_1.1.4                    
[31] ggplot2_3.5.2                   httpuv_1.6.15                   vctrs_0.6.5                     logger_0.4.1                    R6_2.6.1                        mime_0.12                      
[37] lifecycle_1.0.4                 fs_1.6.5                        htmlwidgets_1.6.4               shinycssloaders_1.1.0           fontawesome_0.5.3               shinyjs_2.1.0.9006             
[43] ggmosaic_0.3.3                  pkgconfig_2.0.3                 teal.logger_0.4.0               pillar_1.10.2                   later_1.4.1                     gtable_0.3.6                   
[49] rsconnect_1.3.4                 glue_1.8.0                      data.table_1.17.0               Rcpp_1.0.14                     teal.modules.general_0.5.0.9005 xfun_0.50                      
[55] tibble_3.2.1                    tidyselect_1.2.1                knitr_1.49                      rstudioapi_0.16.0               farver_2.1.2                    xtable_1.8-4                   
[61] bsicons_0.1.2                   htmltools_0.5.8.1               shinyWidgets_0.8.7              compiler_4.4.1

Relevant log output

Code of Conduct

  • I agree to follow this project's Code of Conduct.

Contribution Guidelines

  • I agree to follow this project's Contribution Guidelines.

Security Policy

  • I agree to follow this project's Security Policy.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingcore

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions