表达与通话 [英] expression vs call

查看:78
本文介绍了表达与通话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表达式和调用之间有什么区别?

What is the difference between an expression and a call?

例如:

func <- expression(2*x*y + x^2)
funcDx <- D(func, 'x')

然后:

> class(func)
[1] "expression"
> class(funcDx)
[1] "call"

调用带有环境列表的eval 都可以在两者上使用。但是我很好奇这两个类之间有什么区别,在什么情况下应该使用表达式或调用。

Calling eval with envir list works on both of them. But Im curious what is the difference between the two class, and under what circumstances should I use expression or call.

推荐答案

您应该如果您希望 expression 可以容纳多个表达式或调用,请使用 expression 。它实际上返回一个表达式列表。 R的临时用户通常的情况是形成绘图函数的参数,其中任务是为标签形成符号表达式。 R表达式列表是可能包含许多项目的列表,而调用则永远不是这样。有趣的是,@ hadley的Advanced R Programming建议您永远不需要使用[ expression 函数]: http://adv-r.had.co.nz/Expressions.html 。顺便说一句, bquote 函数非常有用,但有一个局限性,一次不能作用于多个表达式。我最近破解了一个关于解析表达式的问题的响应并得到了检查,但我认为@mnel的答案更好: R选择性地设置绘图轴标签的样式

You should use expression when you want its capacity to hold more than one expression or call. It really returns an "expression list". The usual situation for the casual user of R is in forming arguments to ploting functions where the task is forming symbolic expressions for labels. R expression-lists are lists with potentially many items, while calls never are such. It's interesting that @hadley's Advanced R Programming suggests "you'll never need to use [the expression function]": http://adv-r.had.co.nz/Expressions.html. Parenthetically, the bquote function is highly useful, but has the limitation that it does not act on more than one expression at a time. I recently hacked a response to such a problem about parsing expressions and got the check, but I thought @mnel's answer was better: R selectively style plot axis labels

通过 eval(expr,envir =<命名环境或列表>)本质上是函数正在执行的操作的另一条路径。 表达式 call (函数)之间的很大区别是后者期望一个字符对象并通过以下方式对其求值:在符号表中寻找命名函数。

The strategy of passing an expression to the evaluator with eval( expr, envir= < a named environment or list>) is essentially another route to what function is doing. A big difference between expression and call (the functions) is that the latter expects a character object and will evaluate it by looking for a named function in the symbol table.

当您说用 eval 都可以处理,您并不是说它会产生相同的结果,对吗? D 函数(调用)具有其他参数,这些参数可以替换并限制和修改结果。另一方面,对表达式对象的求值会将值替换为符号。

When you say that processing both with the eval "works", you are not saying it produces the same results, right? The D function (call) has additional arguments that get substituted and restrict and modify the result. On the other hand evaluation of the expression-object substitutes the values into the symbols.

似乎有评估级别:

expression(mean(1:10))
# expression(mean(1:10))
 call("mean" , (1:10))
# mean(1:10)
 eval(expression(mean(1:10)))
# [1] 5.5
 eval(call("mean" , (1:10)))
# [1] 5.5

一个人可能期望 eval(expression(mean(1:10 )))仅返回返回调用对象的下一个级别,但是它将继续解析表达式树并评估结果。为了获得对平均值的未经评估的函数调用,我需要插入 quote

One might have expected eval(expression(mean(1:10))) to return just the next level of returning a call object but it continues to parse the expression tree and evaluate the results. In order to get just the unevaluated function call to mean, I needed to insert a quote:

eval(expression(quote(mean(1:10))))
# mean(1:10)

这篇关于表达与通话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆