Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recordTest does not work with shinyMatrix #311

Closed
simenlonsethagen opened this issue May 7, 2020 · 2 comments
Closed

recordTest does not work with shinyMatrix #311

simenlonsethagen opened this issue May 7, 2020 · 2 comments

Comments

@simenlonsethagen
Copy link

simenlonsethagen commented May 7, 2020

Not sure if this belongs here or at shinyMatrix. Cross-posted as Issue #12.

I've run in to a problem when I try to use shinytest to test an app where I use shinyMatrix's matrixInput. To reproduce, I've taken the following example app from their GitHub repo:

ui.R:

library(shinyMatrix)

m <- diag(5)
# colnames(m) <- 1:3
# rownames(m) <- letters[1:3]

shiny::tagList(
  shiny::fluidPage(
    shiny::titlePanel("Demonstration Matrix Input Field"),
    shiny::fluidRow(
      column(6, matrixInput(
        inputId = "matrix",
        value = m,
        class = "numeric",
        cols = list(
          names = TRUE
        ),
        rows = list(
          names = TRUE
        )
      )),
      column(6, tableOutput("table"))
    )
  )
)

and server.R:

library(shinyMatrix)

function(input, output, session) {
  output$table <- renderTable(input$matrix, rownames = TRUE)
}

I then record a test where I just change a number in the input matrix:

> library(shinytest)
> recordTest()

This gives the following console output:

Loading required package: shiny

Listening on http://127.0.0.1:5017
C> Running application in test mode.
C> Loading required package: shiny
C> 
C> Listening on http://127.0.0.1:7857
Saved test code to /home/simen/repos/shinymatrixshinytestissue/tests/mytest.R
Running mytest.R Error in session_makeRequest(self, private, endpoint, data, params, headers) : 
  undefined is not an object (evaluating 'value.data.length')

The recorded snapshot looks like this:

001

When I try to run the test, I get error message at the end again:

> testApp()
Running mytest.R Error in session_makeRequest(self, private, endpoint, data, params, headers) : 
  undefined is not an object (evaluating 'value.data.length')

I've tried to run it in debug mode, but I don't understand what happens in the JavaScript at line 12 in the traceback from the call above:

> traceback()
17: stop(create_condition(response, "error", call = call))
16: report_error(response)
15: session_makeRequest(self, private, endpoint, data, params, headers)
14: private$makeRequest("EXECUTE ASYNC SCRIPT", list(script = script, 
        args = args))
13: session_executeScriptAsync(self, private, script, ...)
12: private$web$executeScriptAsync("var wait = arguments[0];\n    var timeout = arguments[1];\n    var callback = arguments[2];\n    shinytest.outputValuesWaiter.start(timeout);\n    shinytest.inputQueue.flush();\n    shinytest.outputValuesWaiter.finish(wait, callback);", 
        wait, timeout)
11: sd_flushInputs(self, private, wait, timeout)
10: private$flushInputs(wait_, timeout_)
9: sd_setInputs(self, private, ..., wait_ = wait_, values_ = values_, 
       timeout_ = timeout_, allowInputNoBinding_ = allowInputNoBinding_, 
       priority_ = priority_)
8: app$setInputs(matrix = c(c("1", "0", "0", "0", "0"), c("0", "1", 
       "0", "0", "0"), c("0", "0", "1", "0", "0"), c("0", "0", "0", 
       "1", "0"), c("0", "0", "0", "1", "1"), "", "", "", "", "", 
       "", "", "", "", "")) at mytest.R#5
7: eval(ei, envir)
6: eval(ei, envir)
5: withVisible(eval(ei, envir))
4: source(testname, local = env)
3: FUN(X[[i]], ...)
2: lapply(found_testnames, function(testname) {
       withr::local_dir(testsDir)
       withr::local_envvar(c(RSTUDIO = ""))
       withr::local_options(list(shinytest.app.dir = "appdir"))
       gc()
       env <- new.env(parent = .GlobalEnv)
       if (!quiet) {
           message(testname, " ", appendLF = FALSE)
       }
       source(testname, local = env)
   })
1: testApp()

My guess is that there is something strange going on in the app$setInput call on line 8, so maybe the input to the matrix was not parsed correctly in recordTest? Here is the generated mytest.R where the app$setInput happens:

app <- ShinyDriver$new("../")
app$snapshotInit("mytest")

app$snapshot()
app$setInputs(matrix = c(c("1", "0", "0", "0", "0"), c("0", "1", "0", "0", "0"), c("0", "0", "1", "0", "0"), c("0", "0", "0", "1", "0"), c("0", "0", "0", "1", "1"), "", "", "", "", "", "", "", "", "", ""))
app$snapshot()
@aneudecker
Copy link

It seems like shinytest does capture a different value than it should. Changing the app$setInputs parameters to

app$setInputs(matrix = list(
    data = diag(5),
    rownames = 1:5,
    colnames = 1:5
  )
)

solves this issue.

@hadley
Copy link
Member

hadley commented Aug 7, 2020

It seems like you've resolved this issue, so I'm going to close it.

@hadley hadley closed this as completed Aug 7, 2020
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

No branches or pull requests

3 participants