substitute
Substitute in an Expression
Description
Returns an expression with subsitutions made relative to an
environment.
Usage
substitute(expr, env, evaluate=FALSE)
Arguments
expr |
an expression.
|
env |
an environment or a list that, if supplied, is used to make the
substitutions. More commonly, it is omitted and the local environment
of the function calling substitute is used.
|
evaluate |
a logical value. If TRUE, expr is evaluated before the
substitution is done. The default is FALSE.
|
Details
Each occurence of a name in the expression is subsituted as follows:
- If env is not specified, it defaults to the local
environment of the function calling substitute.
- If env is specified, and the name
is a formal argument to the function calling substitute,
then the expression for that argument is substituted (either
the actual or the default expression). This substitution is
unaffected by whether the argument has been evaluated.
- Names that are not found in env are ignored.
Value
returns an object containing the unevaluated expr, with any
names occurring in expr replaced by the corresponding
component of env.
returns expr as a parsed but unevaluated object if no
name matches occur. In this case, the result is equivalent to
expression(expr)[[1]].
See Also
Examples
# argument x as a character string label
f <- function(x) {
label <- deparse(substitute(x))
label
}
f(1:3+4)
# [1] "1:3 + 4"
# use evaluate argument to evaluate expr argument.
f <- Response ~ .^2 - .
expr <- quote(A+B+C)
substitute(f, list(. = expr))
# returns name: f
substitute(f, list(. = expr), evaluate=TRUE)
# returns expression: Response ~ (A + B + C)^2 - (A + B + C)