GithubHelp home page GithubHelp logo

Cannot ref shiny reactiveValues about lobstr HOT 14 CLOSED

r-lib avatar r-lib commented on August 17, 2024
Cannot ref shiny reactiveValues

from lobstr.

Comments (14)

jcheng5 avatar jcheng5 commented on August 17, 2024 1

I'm missing some context here but if you do decide to set the shiny.suppressMissingContextError option, please make sure to restore its previous value when you're done.

from lobstr.

hadley avatar hadley commented on August 17, 2024

Can you please provide a minimal reprex (reproducible example)? The goal of a reprex is to make it as easy as possible for me to recreate your problem so that I can fix it: please help me help you!

If you've never heard of a reprex before, start by reading "What is a reprex", and follow the advice further down the page. Please make sure your reprex is created with the reprex package as it gives nicely formatted output and avoids a number of common pitfalls.

from lobstr.

dipterix avatar dipterix commented on August 17, 2024

Can't use reprex for the case 1 because it runs forever.

Case 1:

# devtools::install_github('beauchamplab/rave@rave-fir')
  library(rave)
  library(lobstr)
  # rave::download_subject_data('https://s3-us-west-2.amazonaws.com/rave-demo-subject/sfn-demo/data-large.zip')
  m = unlist(load_modules())[[3]]
  init_app(m, test.mode = T)
  ref(m)

Error message:

'as.list.reactivevalues' is deprecated. Use reactiveValuesToList instead.
Please see ?reactiveValuesToList for more information.
Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

Second case doesn't go well with reprex as well since the shiny app didn't launch.

suppressPackageStartupMessages(library(shiny))
env = new.env()

shinyApp(fluidPage(actionButton('asd',' ')), server = function(input,output,session){
  local_data = reactiveValues(a=1)
  
  env$sess = session
  stopApp()
})

Shiny applications not supported in static R Markdown documents

lobstr::ref(env$sess)
#> [1:0x7fada201e2e0] <NULL>

Created on 2018-12-05 by the reprex package (v0.2.1)

If I ran without reprex, then the error message is:

>   lobstr::ref(env$sess)
Error in is.character(lhs) : 
  C stack usage  7970736 is too close to the limit
Error during wrapup: C stack usage  7969856 is too close to the limit

from lobstr.

hadley avatar hadley commented on August 17, 2024

Can you please give me some snippet off code to run that doesn't require installing other packages or installing large data? It should be possible to make a very small shiny app to demonstrate the problem.

from lobstr.

yutannihilation avatar yutannihilation commented on August 17, 2024

I can reproduce the issue with the code above, though the error message is different now.

> lobstr::ref(env$sess)
Error in is.character(lhs) : node stack overflow

This part might be done with shiny:::ShinySession$new(ws), but I'm yet to find what ws I should pass...

  env$sess = session

from lobstr.

hadley avatar hadley commented on August 17, 2024

@jcheng5 should be able to help you figure it out

from lobstr.

yutannihilation avatar yutannihilation commented on August 17, 2024

I can reproduce the issue with the code above

Sorry, I just looked at the second issue... 🙄

For the first case, @jcheng5 suggested (thanks!) we can use options(shiny.suppressMissingContextError=TRUE) to suppress the error.

For the second case, it seems there's a ciruclation of reference:

> unclass(env$sess$.__enclos_env__)
<environment: 0x00000197c79482c0>
> unclass(env$sess$.__enclos_env__$self$.__enclos_env__)
<environment: 0x00000197c79482c0>

from lobstr.

yutannihilation avatar yutannihilation commented on August 17, 2024

For the second case, it seems there's a ciruclation of reference

Sorry, this is not the core of the problem, it seems package_version (env$sess$request$httpuv.version) is the problem:

> lobstr::ref(package_version("0.0.1"))
Error in is.character(lhs) : node stack overflow

from lobstr.

hadley avatar hadley commented on August 17, 2024

@yutannihilation do you want to have a look at why this is a problem? I suspect it's because:

x <- package_version("0.0.1")
identical(x, x[[1]])
#> [1] TRUE

Created on 2019-01-19 by the reprex package (v0.2.1.9000)

So we need to use .subset2() instead of [[

from lobstr.

yutannihilation avatar yutannihilation commented on August 17, 2024

Yes, I'd like to (but maybe not today...).

from lobstr.

hadley avatar hadley commented on August 17, 2024

No problems, the most important thing is the reprex 😄

from lobstr.

yutannihilation avatar yutannihilation commented on August 17, 2024

It seems you are right; ref_tree() is applied infinitely here because lapply(<package_version>, f) passes the newly generated package_version, not its elements, to f().

lobstr/R/ref.R

Line 57 in 9fcf642

subtrees <- lapply(x, ref_tree, layout = layout, seen = seen, character = character)

The problem is [[.numeric_version() creates a new object, so it never matches the addresses in seen.

library(lobstr)

x <- package_version("1.0.0")

obj_addr(x)
#> [1] "0x156f7138"

obj_addr(x[[1]])
#> [1] "0x17f49378"

obj_addr(x[[1]])
#> [1] "0x17f6d648"

Created on 2019-01-25 by the reprex package (v0.2.1)

Using .subset2() might be an idea, but I think it's not always possible to know the "correct" length of the object to generate indices, because length() might tell us some wierd value.

Instead, how about unclass() in all these recursive cases? Since we are looking into the underlying structure, we don't need custom classes and methods. This means we can avoid calling such methods as as.list.reactiveValues(), so I feel this fix is more general.

lobstr/R/ref.R

Lines 55 to 62 in 9fcf642

# recursive case
if (is.list(x)) {
subtrees <- lapply(x, ref_tree, layout = layout, seen = seen, character = character)
} else if (is.environment(x)) {
subtrees <- lapply(as.list(x), ref_tree, layout = layout, seen = seen, character = character)
} else if (is.character(x)) {
subtrees <- ref_tree_chr(x, layout = layout, seen = seen)
}

If this looks OK, I'll create a PR.

from lobstr.

hadley avatar hadley commented on August 17, 2024

Yeah, that sounds right to me.

from lobstr.

yutannihilation avatar yutannihilation commented on August 17, 2024

Thanks, I created #36.

BTW, I didn't know environments cannot be unclassed...

e <- structure(new.env(), class = "foo")
class(e)
#> [1] "foo"
unclass(e)
#> Error in unclass(e): cannot unclass an environment

Created on 2019-01-26 by the reprex package (v0.2.1)

from lobstr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.