If I want to print the symbol denoting an object in R I can use quote():
> X <- list()
> print(quote(X))
X
>
However, if I have the function
h <- func开发者_如何学JAVAtion(Y){
quote(Y)
}
then
> h(X)
Y
>
Is it possible in R to write a function such that
> h(X)
X
?
> f = function(x) print(deparse(substitute(x)))
> f(asd)
[1] "asd"
>
Why? As you've found out quote()
tells R to not evaluate a code block (which it does with Y
). substitute()
behaves differently; there's a good example at ?substitute
.
h <- function(x) match.call()[['x']]
h(X)
X
substitute
also works without the extra calls:
h <- function(x) substitute(x)
h(X)
X
精彩评论