Skip to contents

get_call(env), given an environment associated with a currently executing call, returns the function call and its arguments, as a dots object. To replicate a call, the dots object returned can be passed to do.

get_function(env) finds the function object associated with a currently executing call.

Usage

get_call(
  env = caller(environment()),
  ifnotfound = stop("get_call: environment not found on stack")
)

get_function(
  env = caller(environment()),
  ifnotfound = stop("get_function: environment not found on stack")
)

Arguments

env

An environment belonging to a currently executing function call. By default, the caller of get_call itself (so get_call() is equivalent to get_call(environment()).)

ifnotfound

What to return if the call is not found. By default an error is thrown.

Value

get_call returns a dots object, the first element of which represents the function name and caller environment.

get_function returns a closure.

Details

get_call is meant to replace match.call and sys.call; its advantage is that it captures the environments bound to arguments in addition to their written form.

get_function is similar to sys.function, but is keyed by environment rather than number.

See also

do dots caller

Examples

# We might think of re-writing the start of [lm] like so:
LM <- function(formula, data, subset, weights, na.action, method = "qr",
               model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, 
               contrasts = NULL, offset, ...) {
  cl <- get_call()
  mf <- do(model.frame,
           arg_list(formula, data, subset, weights, na.action, offset))

  z <- get.call()

  class(z) <- c("LM", class(z))
  z$call <- cl
  z
}

# and `update` like so:
update.LM <- function(object, formula., ...) {
  call <- object$call
  extras <- dots(...)
  call$formula <- forced_quo(update.formula(formula(object), formula.))
  do(call)
}