开发者

Computing on the Language in R

开发者 https://www.devze.com 2023-02-15 04:28 出处:网络
If I want to print the symbol denoting an object in R I can use quote(): > X <- list() > print(quote(X))

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
0

精彩评论

暂无评论...
验证码 换一张
取 消