Skip to content

Load global vars when saving app objects#307

Merged
schloerke merged 17 commits intomainfrom
use_global_vars
Jan 19, 2023
Merged

Load global vars when saving app objects#307
schloerke merged 17 commits intomainfrom
use_global_vars

Conversation

@schloerke
Copy link
Copy Markdown
Collaborator

@schloerke schloerke commented Jan 11, 2023

Fixes #303
Fixes #295

  • Try using global env when assigning
    • Ans: Shimming an environment between the server and global env
    • Ans: Using global env. Performing multi asign using list2env(envir=globalenv())
  • Try using {future} / callr to pass app object instead of saving to temp arg
    • Ans: While {future} does a great job at capturing what is needed, it does not work well with stdout/stderr. This is a deal breaker.

@jcheng5
Copy link
Copy Markdown
Member

jcheng5 commented Jan 11, 2023

Am I correct that the main three cases we care about are:

  1. Writing tests for a nontrivial app that is in an app.R
  2. Writing tests for a nontrivial app that is using golem/rhino(?)
  3. Writing tests for trivial apps that exercise some feature or component that is not specific to a single app

The kind of cross-process de/serialization that this PR is fixing, totally makes sense to me for case 3. It makes me a lot less comfortable if people are relying on it for case 2. I'd love to see us make a formal recommendation for using shinytest2 with golem/rhino that doesn't involve serializing any code objects.

@daattali
Copy link
Copy Markdown
Contributor

daattali commented Jan 13, 2023

The biggest benefit that I personally wanted from this is to be able to test a shiny module. If there is another good way to test a shiny module, please let me know.

library(shiny)

mod_ui <- function(id) {
  ns <- NS(id)
  textOutput(ns("foo"))
}

mod_server <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
      output$foo <- renderText("bar")
    }
  )
}

app <- shinyApp(
  mod_ui("test"), 
  function(input, output, session) { mod_server("test") }
)

driver <- shinytest2::AppDriver$new(app)
driver$get_logs()  #  Error in mod_server("test") : could not find function "mod_server"

@schloerke schloerke marked this pull request as ready for review January 13, 2023 20:53
@daattali
Copy link
Copy Markdown
Contributor

daattali commented Jan 13, 2023

This PR does indeed fix my usecase in #303

@schloerke schloerke merged commit 96a1d28 into main Jan 19, 2023
@schloerke schloerke deleted the use_global_vars branch January 19, 2023 14:26
schloerke added a commit that referenced this pull request Feb 19, 2023
* Better error message when both app.R and server.R are found

* Lint

* Explicitly load all global vars found

* Test global vars being found by using a script to cuz of parent environments

* Revert "Better error message when both app.R and server.R are found"

This reverts commit 8b38be8.

* `devtools::document()` (GitHub Actions)

* Rather than assigning values, shim an environment. Test two apps

* Fix lint

* Update .lintr

* Update NEWS.md

* Create issue_295.R

* Add test for issue 295

* Recursively look for globals

* Use `list2env(globals, envir=globalenv())` to multi assign globals

* Update issue_295.R

* Only run tests on CI

Co-authored-by: schloerke <schloerke@users.noreply.github.com>
Co-authored-by: Joe Cheng <joe@rstudio.com>
schloerke added a commit that referenced this pull request Feb 22, 2023
* main:
  Update cran-comments.md
  Update cran-comments.md
  Run revdep
  Move crashpad file to setup test file
  Update cran-comments.md
  Remove detritus Crashpad file/directory on CRAN
  Don't use github `{lintr}` or `{decor}` package
  Run revdep on 12 pkgs; ✅
  Remove C++11 from DESCRIPTION (#326)
  News entries for #284 and #288
  Load global vars when saving app objects (#307)
  Better error message when both app.R and server.R are found (#284)
  Don't assume preview overflow is of length 1 (#291)
  Force JS code to be a character for `{glue}` support (#288)
  fix typo: expect_unqiue_names -> expect_unique... (#270)
  Fix typo: use_shinytest -> use_shinytest2 (#269)
  `compare_screenshot_threshold()` should not error from `screenshot_max_difference()` (#276)
  Increment version number to 0.2.1
  Change default screenshot selector to `"scrollable_area"` (from bounding box of `"html"` DOM) (#325)
  actions/test-app; Add multi app `app-dir` support; Default upload-snapshot to `true`  (#322)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to use a variable inside a shiny app? Error when trying to initialize AppDriver from inside a function

3 participants