为什么以下工作有效:
foo <- function(x) {x}
curve(foo)
# plots the identity function between 0 and 1
这并不:
curve(function(x) {x})
曲线错误(函数(x){:
“expr”未求值为长度为“n”的对象
And yet
# the anonymous function can be called
foo(1) #1
(function(x) x)(1) #1
all.equal(foo, function(x) {x})
# TRUE
我尝试了“quote()”和“expression()”的各种组合,但没有结果。
实际上帮助页面curve
does not假设“expr”参数可以是函数对象。可接受的三种论点是“name函数的函数,或者作为 x 的函数编写的调用或表达式,其计算结果将是与 x 长度相同的对象。”(已添加强调。)
以下所有操作都会成功:
curve( (function(x) {x})(x) )
curve( local(x) )
curve( eval(x) )
当你看到那个...
all.equal(foo, function(x) {x})
# TRUE
...这是说附加到名字的语言对象foo
与function(x) {x}
. (The all.equal.language
-function 解析对象或对象名称并比较字符结果。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)