Create a shiny::reactive() that, when invoked with meta-mode activated (i.e. called within withMetaMode() or expandChain()), returns a code expression (instead of evaluating that expression and returning the value).

metaReactive(
  expr,
  env = parent.frame(),
  quoted = FALSE,
  varname = NULL,
  domain = shiny::getDefaultReactiveDomain(),
  inline = FALSE,
  localize = "auto",
  bindToReturn = FALSE
)

metaReactive2(
  expr,
  env = parent.frame(),
  quoted = FALSE,
  varname = NULL,
  domain = shiny::getDefaultReactiveDomain(),
  inline = FALSE
)

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.

varname

An R variable name that this object prefers to be named when its code is extracted into an R script. (See also: expandChain())

domain

See domains.

inline

If TRUE, during code expansion, do not declare a variable for this object; instead, inline the code into every call site. Use this to avoid introducing variables for very simple expressions. (See also: expandChain())

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. When called outside meta mode, it acts the same as a regular shiny::reactive() expression call.

Details

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

If varname is unspecified, srcrefs are used in attempt to infer the name bound to the meta-reactive object. In order for this inference to work, the keep.source option must be TRUE and expr must begin with \{.

See also

Examples


library(shiny)
options(shiny.suppressMissingContextError = TRUE)

input <- list(x = 1)

y <- metaReactive({
  req(input$x)
  a <- ..(input$x) + 1
  b <- a + 1
  c + 1
})
#> Warning: Unable to infer variable name for metaReactive when the option keep.source is FALSE. Either set `options(keep.source = TRUE)` or specify `varname` in metaReactive

withMetaMode(y())
#> req(input$x)
#> a <- 1 + 1
#> b <- a + 1
#> c + 1
expandChain(y())
#> var_1 <- {
#>   req(input$x)
#>   a <- 1 + 1
#>   b <- a + 1
#>   c + 1
#> }
#> var_1

y <- metaReactive2({
  req(input$x)

  metaExpr({
    a <- ..(input$x) + 1
    b <- a + 1
    c + 1
  }, bindToReturn = TRUE)
})
#> Warning: Unable to infer variable name for metaReactive2 when the option keep.source is FALSE. Either set `options(keep.source = TRUE)` or specify `varname` in metaReactive2

expandChain(y())
#> a <- 1 + 1
#> b <- a + 1
#> var_1 <- c + 1
#> var_1