2011-03-07 80 views
8

如果我想打印的符號RI表示的對象可以使用引號():計算在R上的語言

> X <- list() 
> print(quote(X)) 
X 
> 

但是,如果我具備的功能

h <- function(Y){ 
    quote(Y) 
} 

然後

> h(X) 
Y 
> 

是否有可能中的R編寫一個函數使得

> h(X) 
X 

回答

12
> f = function(x) print(deparse(substitute(x))) 
> f(asd) 
[1] "asd" 
> 

爲什麼?正如你發現quote()告訴R不要評估一個代碼塊(它與Y一樣)。 substitute()行爲不同;在?substitute有一個很好的例子。

6
h <- function(x) match.call()[['x']] 

h(X) 
X 
0

substitute也工作沒有額外的呼叫:

h <- function(x) substitute(x) 
h(X) 
X