Skip to content

Error when trying to initialize AppDriver from inside a function #295

Closed

Description

@daattali

I want to have a way to easily create an AppDriver for different shiny modules. I provide the UI and the server of a module, and a shiny app is created with that module, then an AppDriver is created for this shiny app. I get an error when doing that. Full details below.

Here's an example little module to test:

library(shiny)

my_mod_ui <- function(id) {
  ns <- NS(id)
  tagList(
    textInput(ns("name"), "name", "test"),
    textOutput(ns("greet"))
  )
}

my_mod_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    output$greet <- renderText(input$name)
  })
}

And here is a function for easily wrapping a given module in a shiny app, a way to "graduate" a module to a real app:

make_app <- function(mod_ui, mod_server) {
  shiny::shinyApp(
    shiny::fluidPage(mod_ui),
    function(input, output, session) mod_server
  )
}

Now I can create a shiny app easily from a module, for example:

app <- make_app(my_mod_ui("test"), my_mod_server("test"))

You can run this app and it works.

To create an AppDriver for this, I can do:

make_driver <- function(app) {
  shinytest2::AppDriver$new(app)
}
make_driver(app)

And this works. However, my actual goal is to have just one function where I provide the module, and it creates the AppDriver. So if I take the same code and put it into one function, it looks like this:

make_driver <- function(mod_ui, mod_server) {
  app <- make_app(mod_ui, mod_server)
  shinytest2::AppDriver$new(app)
}

But this doesn't work. If I call make_driver(my_mod_ui("test"), my_mod_server("test")), I get an error:

Error in `app_initialize()`:
! Identified global objects via static code inspection (function (input, output, session); mod_server). session must be a ShinySession or session_proxy object.


ℹ You can inspect the failed AppDriver object via `rlang::last_error()$app`
ℹ AppDriver logs:
{shinytest2} R info 17:20:51.65 Start AppDriver initialization
{shinytest2} R info 17:20:51.67 Error while initializing AppDriver:
                                Identified global objects via static code inspection (function (input, output, session); mod_server). session must be a ShinySession or session_proxy object.

Caused by error in `callModule()`:
! Identified global objects via static code inspection (function (input, output, session); mod_server). session must be a ShinySession or session_proxy object.
Run `rlang::last_error()` to see where the error occurred.

Activity

  1. daattali commented on Jan 4, 2023

    @daattali
    ContributorAuthor

    I just discovered that it's a little different from what I described above.

    Starting from a clean state, run the following:

    library(shiny)
    
    my_mod_ui <- function(id) {
      ns <- NS(id)
      tagList(
        textInput(ns("name"), "name", "test"),
        textOutput(ns("greet"))
      )
    }
    
    my_mod_server <- function(id) {
      moduleServer(id, function(input, output, session) {
        output$greet <- renderText(input$name)
      })
    }
    
    make_app <- function(mod_ui, mod_server) {
      shiny::shinyApp(
        shiny::fluidPage(mod_ui),
        function(input, output, session) mod_server
      )
    }
    
    app <- make_app(my_mod_ui("test"), my_mod_server("test"))

    Now, trying to run shinytest2::AppDriver$new(app) will fail. It will keep failing until you actually run the app once. If you run the app, you will then be able to successfully call AppDriver on it.

  2. schloerke commented on Jan 5, 2023

    @schloerke
    Collaborator

    I can reprex the app driver failing to start the first time and working after running the app once. Thank you.

    Tracing the error:

    All of the session objects are defaulting to shiny::getDefaultReactiveDomain(). On a fresh R session, this returns NULL. A NULL value would cause the error to be thrown.

    Even after running an app and stopping it, shiny::getDefaultReactiveDomain() still returns NULL.


    I believe the module's server function is being created too early. A session object is required for creating the module: https://github.com/cran/shiny/blob/9a77db3684924b61cd324581c0159a708a02488e/R/modules.R#L157

    Adjusting the code to create the module within the server function did not help. It could not find my_module_server() function.

    Adjusting the code to have the module server be created within make_app() allowed for it to work.

    This code worked for me:

    library(shiny)
    my_mod_ui <- function(id) {
      ns <- NS(id)
      tagList(
        textInput(ns("name"), "name", "test"),
        textOutput(ns("greet"))
      )
    }
    my_mod_server <- function(id) {
      moduleServer(id, function(input, output, sessionServer) {
        output$greet <- renderText(input$name)
      })
    }
    
    # Can not be saved
    make_app <- function(mod_ui, mod_server) {
      shiny::shinyApp(
        shiny::fluidPage(mod_ui),
        function(input, output, session) mod_server
      )
    }
    
    # Can not find `my_mod_server` at run time
    make_app2 <- function(mod_id) {
      shiny::shinyApp(
        shiny::fluidPage(my_mod_ui(mod_id)),
        function(input, output, session) my_mod_server(mod_id)
      )
    }
    
    # Works
    make_app3 <- function(mod_id) {
      my_inner_server <- function(id) {
        moduleServer(id, function(input, output, session) {
          output$greet <- renderText(input$name)
        })
      }
      shiny::shinyApp(
        shiny::fluidPage(my_mod_ui(mod_id)),
        function(input, output, session) my_inner_server(mod_id)
      )
    }
    
    
    ## Can not be saved
    # shiny_app <- make_app(my_mod_ui("test"), my_mod_server("test"))
    ## Runtime error
    # shiny_app <- make_app2("test")
    ## Works
    shiny_app <- make_app3("test")
    
    app <- AppDriver$new(shiny_app, view = TRUE)

    IDK... this seems like a {globals} issue and I currently do not know enough to pursue it.

  3. daattali commented on Jan 5, 2023

    @daattali
    ContributorAuthor

    Thanks for looking into this! The version that works is not a helpful solution to the issue I described though, because the goal was to have a function with a specific signature: take in any module UI and module server, and return a shinytest2 driver. It was meant to make testing a module much easier, less code duplication.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions