Skip to contents

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

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

# 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)
#> quo(y, <environment: 0x5645f05b1b10>)
#> quo(y, <environment: 0x5645f05b1b10>)
#> quo(x, <environment: 0x5645f05b2130>)
#> quo(w, <environment: 0x5645ed7c54e0>)