nseval (version 0.4)

unwrap: Unwrap variable references.

Description

Given an unforced quotation whose expression is a bare variable name, unwrap follows the variable reference, and returns a quotation. When the argument is forced or has a nontrivial expression unwrap has no effect.

Usage

unwrap(x, recursive = FALSE)

# S3 method for dots unwrap(x, recursive = FALSE)

Arguments

x

a quotation to unwrap.

recursive

Default FALSE unwraps exactly once. If TRUE, unwrap as far as possible (until a forced promise or nontrivial expression is found.)

Value

The quotation method returns a quotation.

The dots method returns a dots object with each quotation unwrapped.

Details

The syntax locate( (...) ) is available for locating ....

There are two good use cases for unwrap(x, recursive=TRUE). One is to derive plot labels (the most inoccuous use of metaprogramming). Another is to check for missingness (this is what R's missing and does as well).

Using unwrap(x, recursive=TRUE) in other situations can get you into confusing situations -- effectively you are changing the behavior of a parent function that may be an unknown number of levels up the stack, possibly turning a standard-evaluating function into nonstandard-evaluating function. So recursive unerapping is not the default behavior.

Examples

Run this code
# NOT RUN {
# different levels of unwrapping:
f <- function(x) { g(x) }
g <- function(y) { h(y) }
h <- function(z) {
  print(arg(z))
  print(unwrap(quo(z)))
  print(unwrap(unwrap(quo(z))))
  print(unwrap(quo(z), recursive=TRUE))
}

w <- 5
f(w)
# }

Run the code above in your browser using DataCamp Workspace