Create a observe()r that, when invoked with meta-mode activated (i.e. called within withMetaMode() or expandChain()), returns a partially evaluated code expression. Outside of meta-mode, metaObserve() is equivalent to observe() (it fully evaluates the given expression).

metaObserve(
  expr,
  env = parent.frame(),
  quoted = FALSE,
  label = NULL,
  domain = getDefaultReactiveDomain(),
  localize = "auto",
  bindToReturn = FALSE
)

metaObserve2(
  expr,
  env = parent.frame(),
  quoted = FALSE,
  label = NULL,
  domain = getDefaultReactiveDomain()
)

Arguments

expr

An expression (quoted or unquoted).

env

The parent environment for the reactive expression. By default, this is the calling environment, the same as when defining an ordinary non-reactive expression. If x is a quosure and quoted is TRUE, then env is ignored.

quoted

If it is TRUE, then the quote()ed value of x will be used when x is evaluated. If x is a quosure and you would like to use its expression as a value for x, then you must set quoted to TRUE.

label

A label for the observer, useful for debugging.

domain

See domains.

localize

Whether or not to wrap the returned expression in local(). The default, "auto", only wraps expressions with a top-level return() statement (i.e., return statements in anonymized functions are ignored).

bindToReturn

For non-localized expressions, should an assignment of a meta expression be applied to the last child of the top-level \{ call?

Value

A function that, when called in meta mode (i.e. inside expandChain()), will return the code in quoted form. If this function is ever called outside of meta mode, it throws an error, as it is definitely being called incorrectly.

Details

If you wish to capture specific code inside of expr (e.g. ignore code that has no meaning outside shiny, like req()), use metaObserve2() in combination with metaExpr(). When using metaObserve2(), expr must return a metaExpr().

See also

Examples


# observers execute 'immediately'
x <- 1
mo <- metaObserve({
  x <<- x + 1
})
getFromNamespace("flushReact", "shiny")()
#> Top:
#> Bottom:
print(x)
#> [1] 2

# It only makes sense to invoke an meta-observer
# if we're in meta-mode (i.e., generating code)
expandChain(mo())
#> x <<- x + 1

# Intentionally produces an error
if (FALSE) mo()