stack_trim

0th

Percentile

Trim top call layers from the evaluation stack.

eval_stack() can be tricky to use in real code because all intervening frames are returned with the stack, including those at eval_stack() own call site. stack_trim() makes it easy to remove layers of intervening calls.

Usage
stack_trim(stack, n = 1)
Arguments
stack
An evaluation stack.
n
The number of call frames (not eval frames) to trim off the top of the stack. In other words, the number of layers of intervening frames to trim.
Aliases
  • stack_trim
Examples
# Intervening frames appear on the evaluation stack:
identity(identity(eval_stack()))

# stack_trim() will trim the first n layers of calls:
stack_trim(identity(identity(eval_stack())))

# Note that it also takes care of calls intervening at its own call
# site:
identity(identity(
  stack_trim(identity(identity(eval_stack())))
))

# It is especially useful when used within a function that needs to
# inspect the evaluation stack but should nonetheless be callable
# within nested calls without side effects:
stack_util <- function() {
  # n = 2 means that two layers of intervening calls should be
  # removed: The layer at eval_stack()'s call site (including the
  # stack_trim() call), and the layer at stack_util()'s call.
  stack <- stack_trim(eval_stack(), n = 2)
  stack
}
user_fn <- function() {
  # A user calls your stack utility with intervening frames:
  identity(identity(stack_util()))
}
# These intervening frames won't appear in the evaluation stack
identity(user_fn())
Documentation reproduced from package rlang, version 0.0.0.9000, License: GPL-3

Community examples

Looks like there are no examples yet.