计算R语言

时间:2022-10-09 00:17:04

If I want to print the symbol denoting an object in R I can use quote():

如果我想在R中打印表示对象的符号,我可以使用quote():

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

However, if I have the function

但是,如果我有这个功能

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

then

然后

> h(X)
Y
>

Is it possible in R to write a function such that

在R中是否可以编写这样的函数

> h(X)
X

?

3 个解决方案

#1


12  

> 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.

为什么?正如您所发现的,quote()告诉R不要评估代码块(它与Y一起)。 substitute()表现不同;有一个很好的例子吗?替代品。

#2


6  

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

h(X)
X

#3


0  

substitute also works without the extra calls:

替代也可以没有额外的电话工作:

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

#1


12  

> 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.

为什么?正如您所发现的,quote()告诉R不要评估代码块(它与Y一起)。 substitute()表现不同;有一个很好的例子吗?替代品。

#2


6  

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

h(X)
X

#3


0  

substitute also works without the extra calls:

替代也可以没有额外的电话工作:

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