pengganti mengembalikan pohon parse untuk ekspresi expr (tidak dievaluasi), menggantikan variabel apa pun yang terikat dalam env. quote hanya mengembalikan argumennya. Argumennya tidak dievaluasi dan dapat berupa ekspresi R apa pun. enquote adalah utilitas satu baris sederhana yang mengubah panggilan bentuk Foo(....) menjadi panggilan kutipan(Foo(....)). Ini biasanya digunakan untuk melindungi panggilan dari evaluasi awal.
substitute(expr, env)
quote(expr)
enquote(cl)
| Parameter | Deskripsi |
|---|---|
expr |
any syntactically valid R expression |
cl |
a call, i.e., an R object of class (and mode) "call". |
env |
an environment or a list object. Defaults to the current evaluation environment. |
# NOT RUN {
require(graphics)
(s.e <- substitute(expression(a + b), list(a = 1))) #> expression(1 + b)
(s.s <- substitute( a + b, list(a = 1))) #> 1 + b
c(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)) # nonsense
myplot <- function(x, y)
plot(x, y, xlab = deparse(substitute(x)),
ylab = deparse(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 <- 10
f1(a) # 11
s1(a) # 11
s2(a) # a
typeof(s2(a)) # "symbol"
# }