Repository navigation
Description
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
daattali commentedon Jan 4, 2023
I just discovered that it's a little different from what I described above.
Starting from a clean state, run the following:
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.schloerke commentedon Jan 5, 2023
I can reprex the app driver failing to start the first time and working after running the app once. Thank you.
Tracing the error:
{shinytest2}callsglobals::globalsOf()here:shinytest2/R/save-app.R
Line 42 in b47a0db
{shiny}here: https://github.com/cran/shiny/blob/9a77db3684924b61cd324581c0159a708a02488e/R/modules.R#L187All of the
sessionobjects are defaulting toshiny::getDefaultReactiveDomain(). On a fresh R session, this returnsNULL. ANULLvalue would cause the error to be thrown.Even after running an app and stopping it,
shiny::getDefaultReactiveDomain()still returnsNULL.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:
IDK... this seems like a
{globals}issue and I currently do not know enough to pursue it.daattali commentedon Jan 5, 2023
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.