r - Behavior of do.call() in the presence of arguments without defaults -
this question follow-up previous answer raised puzzle.
reproducible example 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) # works do.call( curry(anova, object=lm1), models ) # do.call( anova, models )
the question why do.call(anova, models)
work fine, @roland points out?
the signature anova anova(object, ...)
anova
calls usemethod
, should* call anova.lm
should call anova.lmlist
, first line objects <- list(object, ...)
, object
doesn't exist in formulation.
the thing can surmise do.call
might not fill in ellipses fills in arguments without defaults , leaves ellipsis catch? if so, documented, it's new me!
* clue--how usemethod
know call anova.lm
if first argument unspecified? there's no anova.list
method or anova.default
or similar...
in regular function call ...
captures arguments position, partial match , 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
behaves in same way except instead of supplying arguments directly function, they're stored in list , do.call
takes care of passing them 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
Comments
Post a Comment