Movatterモバイル変換


[0]ホーム

URL:


rdrr.io

substitute: Substituting and Quoting Expressions

substituteR Documentation

Substituting and Quoting Expressions

Description

substitute returns the parse tree for the (unevaluated)expressionexpr, substituting any variables bound inenv.

quote simply returns its argument. The argument is not evaluatedand can be any R expression.

enquote is a simple one-line utility which transforms a call ofthe formFoo(....) into the callquote(Foo(....)). Thisis typically used to protect acall from early evaluation.

Usage

substitute(expr, env)quote(expr)enquote(cl)

Arguments

expr

any syntactically validR expression

cl

acall, i.e., anR object ofclass (andmode)"call".

env

an environment or a list object. Defaults to thecurrent evaluation environment.

Details

The typical use ofsubstitute is to create informative labelsfor data sets and plots.Themyplot example below shows a simple use of this facility.It uses the functionsdeparse andsubstituteto create labels for a plot which are character string versionsof the actual arguments to the functionmyplot.

Substitution takes place by examining each component of the parse treeas follows: If it is not a bound symbol inenv, it isunchanged. If it is a promise object, i.e., a formal argument to afunction or explicitly created usingdelayedAssign(),the expression slot of the promise replaces the symbol. If it is anordinary variable, its value is substituted, unlessenv is.GlobalEnv in which case the symbol is left unchanged.

Bothquote andsubstitute are ‘special’primitive functions which do not evaluate their arguments.

Value

Themode of the result is generally"call" butmay in principle be any type. In particular, single-variableexpressions have mode"name" and constants have theappropriate base mode.

Note

substitute works on a purely lexical basis. There is noguarantee that the resulting expression makes any sense.

Substituting and quoting often cause confusion when the argument isexpression(...). The result is a call to theexpression constructor function and needs to be evaluatedwitheval to give the actual expression object.

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.

See Also

missing for argument ‘missingness’,bquote for partial substitution,sQuote anddQuote for adding quotationmarks to strings,

all.names to retrieve the symbol names from an expressionor call.

Examples

require(graphics)(s.e <- substitute(expression(a + b), list(a = 1)))  #> expression(1 + b)(s.s <- substitute( a + b,            list(a = 1)))  #> 1 + bc(mode(s.e), typeof(s.e)) #  "call", "language"c(mode(s.s), typeof(s.s)) #   (the same)# but:(e.s.e <- eval(s.e))          #>  expression(1 + b)c(mode(e.s.e), typeof(e.s.e)) #  "expression", "expression"substitute(x <- x + 1, list(x = 1)) # nonsensemyplot <- function(x, y)    plot(x, y, xlab = deparse1(substitute(x)),               ylab = deparse1(substitute(y)))## Simple examples about lazy evaluation, etc:f1 <- function(x, y = x)             { x <- x + 1; y }s1 <- function(x, y = substitute(x)) { x <- x + 1; y }s2 <- function(x, y) { if(missing(y)) y <- substitute(x); x <- x + 1; y }a <- 10f1(a)  # 11s1(a)  # 11s2(a)  # atypeof(s2(a))  # "symbol"

What can we improve?

R Package Documentation

Browse R Packages

We want your feedback!

Note that we can't provide technical support on individual packages. You should contact the package authors for that.

 
Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, readEmbedding Snippets.

Close

[8]ページ先頭

©2009-2026 Movatter.jp