问题描述
这问题是一个跟进到这引起了一个难题。
This question is a follow-up to a previous answer which raised a puzzle.
从previous回答重复的例子:
Reproducible example from the previous answer:
Models <- list( lm(runif(10)~rnorm(10)),lm(runif(10)~rnorm(10)),lm(runif(10)~rnorm(10)) )
lm1 <- lm(runif(10)~rnorm(10))
library(functional)
# This works
do.call( Curry(anova, object=lm1), Models )
# But so does this
do.call( anova, Models )
问题是为什么 do.call(ANOVA,型号)
做工精细,如@Roland指出?
The question is why does do.call(anova, Models)
work fine, as @Roland points out?
有关ANOVA签名是方差分析(对象...)
The signature for anova is anova(object, ...)
ANOVA
要求 UseMethod
,这应该*呼叫 anova.lm
应该叫 anova.lmlist
,它的第一行是对象&LT; - 列表(对象...)
,但对象
在配方不存在。
anova
calls UseMethod
, which should* call anova.lm
which should call anova.lmlist
, whose first line is objects <- list(object, ...)
, but object
doesn't exist in that formulation.
我可以推测的唯一的事情就是 do.call
可能无法只需填写椭圆,但在所有的参数没有填写违约并留下任何额外的省略号赶上?如果是这样,其中的记录,因为这绝对是新的我!
The only thing I can surmise is that do.call
might not just fill in ellipses but fills in all arguments without defaults and leaves any extra for the ellipsis to catch? If so, where is that documented, as it's definitely new to me!
*它本身就是一个线索 - 如何 UseMethod
知道叫 anova.lm
如果第一个参数是不确定的?有没有 anova.list
方法或 anova.default
或类似...
* Which is itself a clue--how does UseMethod
know to call anova.lm
if the first argument is unspecified? There's no anova.list
method or anova.default
or similar...
推荐答案
在普通函数调用 ...
按位置,部分匹配和全场比赛捕捉参数:
In a regular function call ...
captures arguments by position, partial match and full match:
f <- function(...) g(...)
g <- function(x, y, zabc) c(x = x, y = y, zabc = zabc)
f(1, 2, 3)
# x y zabc
# 1 2 3
f(z = 3, y = 2, 1)
# x y zabc
# 1 2 3
do.call
行为以完全相同的方式除了没有直接提供的函数的自变量,它们存储在列表和 do.call
带他们到传递函数的护理:
do.call
behaves in exactly the same way except instead of supplying the arguments directly to the function, they're stored in a list and do.call
takes care of passing them into the function:
do.call(f, list(1, 2, 3))
# x y zabc
# 1 2 3
do.call(f, list(z = 3, y = 2, 1))
# x y zabc
# 1 2 3
这篇关于do.call的行为()在参数presence没有违约的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!