Skip to content

Error when server portion of app uses a non-namespaced internal function #296

Closed

Description

@daattali

I'm not sure if this is strictly a bug, perhaps it's more of a question of how to achieve this.

When developing a package, I have non-exported functions that go inside the server portion of a shiny app. When using shinytest2, I can successfully create an AppDriver with such an app, but when I try calling expect_values() on it, I get an error:

Error in `app_expect_values(self, private, ..., input = input, output = output, 
    export = export, screenshot_args = screenshot_args, name = name, 
    cran = cran)`: Shiny server returned 404 for values URL: http://127.0.0.1:7774/session/4efa4384338a8862356b36154eb4512c/dataobj/shinytest?w=&nonce=ee808e486&input=1&output=1&export=1&format=json&sortC=1
i Is `shiny::runApp(test.mode = TRUE)` enabled?

To reproduce:

  1. In an R package, create a file R/foo.R with a function foo <- function() 5

  2. Run usethis::use_testthat() to set up the standard {testthat} structure

  3. In tests/testthat/test-foo.R write the following code:

    test_that("test", {
      expect_equal(foo(), 5)
    
      app <- shiny::shinyApp(
        shiny::fluidPage(foo()),
        function(input, output, session) { bar <- foo() }
      )
    
      expect_true(inherits(app, "shiny.appobj"))
    
      driver <- shinytest2::AppDriver$new(app)
      driver$expect_values()
    }) 
  4. Click the "Run Tests" button in RStudio

  5. You'll see that the failure is only at the last line, when trying to expect_values()

  6. If you replace bar <- foo() with bar <- 5 then the tests do run

Activity

  1. schloerke commented on Jan 5, 2023

    @schloerke
    Collaborator

    Would your code work if you used bar <- dean:::foo()?

    Related: #298

    Hopefully, if we can get testthat to load the local package (not yet implemented in {shinytest2}), you will not need to namespace the methods / objects to the local package.

  2. daattali commented on Jan 5, 2023

    @daattali
    ContributorAuthor

    Using ::: does get it to work, it's what I've been using everywhere so far. Although notice that only the call inside the server causes a failure -- calling foo() in the UI is ok.

    Hopefully it would be fixed by loading the local package like you say.

  3. added
    theme: testthat file loading
    on Jan 12, 2023
  4. added this to the v0.4.0 milestone on Jan 18, 2023
  5. schloerke commented on Nov 18, 2025

    @schloerke
    Collaborator

    Given an R package, #402 added support for:

    driver <- AppDriver$new(create_app)

    where create_app() is in your package and returns Shiny app object

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

    theme: testthat file loading

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions