如果我想在R中打印表示对象的符号,我可以使用quote()函数:
> X <- list()
> print(quote(X))
X
>
然而,如果我有这个函数
h <- function(Y){
quote(Y)
}
那么
> h(X)
Y
>
在R中是否有可能编写这样一个函数:
> h(X)
X
?
如果我想在R中打印表示对象的符号,我可以使用quote()函数:
> X <- list()
> print(quote(X))
X
>
h <- function(Y){
quote(Y)
}
那么
> h(X)
Y
>
> h(X)
X
?
> f = function(x) print(deparse(substitute(x)))
> f(asd)
[1] "asd"
>
为什么?就像你已经发现的那样,quote()
告诉R不要评估代码块(而它会使用Y
进行评估)。substitute()
的行为不同;在?substitute
中有一个很好的例子。
h <- function(x) match.call()[['x']]
h(X)
X
substitute
也可以在不需要额外调用的情况下工作:
h <- function(x) substitute(x)
h(X)
X