rstudio / shinymeta

Record and expose Shiny app logic using metaprogramming

Home Page:https://rstudio.github.io/shinymeta

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

is.reactive is FALSE for metaReactive variables

zsigmas opened this issue · comments

In some cases it is useful to check if a variable is or is not a reactive. See below an example of an add_1 function that expect either a reactive or a non-reactive.

add_1 <- function(x) {
  reactive ({
    if (is.reactive(x)) {
        x()+1
     } else {
       x+1
     }
    )
  }
}

As expected in a reactive environment:

reactiveConsole(T)
add_1(1)()
[1] 2
add_1(reactive(1))()
[1] 2

But:

add_1(metaReactive({1}))()
Error in x + 1 : non-numeric argument to binary operator

This is quite counterintuitive as my expectation is that add_1 would work in any case.

At the moment I am solving through this two functions:

is.metareactive <- function (x) {
  inherits(x, "shinymeta_reactive")
}

is.anyreactive <- function(x) {
  inherits(x, "reactive") | inherits(x, "shinymeta_reactive")
}