vadr (version 0.01)

quoting.env: Given a list of names, build an environment such that evaluating any expression using those names just gets you the expression back.

Description

Technically, this defines two nested environments, the outer containing functions and the inner containing names, and returns the inner.

Usage

quoting.env(names, parent = emptyenv(), call.names = names)

Arguments

names
The names the environment should define.
parent
The parent environment (defaults to the empty environment)
call.names
The functions the enclosing environment should define. Decaults to names, but sometimes you want these to be different.

Value

The environment constructed.

Details

This somewhat esoteric function mostly intended to be used by expand_macros

Examples

Run this code
en <- quoting.env(c('+', '(', 'a', 'b', '*'), environment())

evalq(a+b, en) # a+b
evalq(a+(b*a), en) # a+(b*a)
z <- 100
evalq(a+(b*a)*z, en) #a+(b*a)*100

##We can build a function that does something like substitute() like this:
ersatz.substitute <- function(expr, envir=arg_env(expr)) {
  parent <- as.environment(envir)
  en <- quoting.env(setdiff(all.names(expr), ls(parent)), parent)
  eval(expr, en)
}

ersatz.substitute(quote(a+b+c), list(b=quote(q+y))) # returns a+(q+y)+c

Run the code above in your browser using DataLab