Behavior of do.call() in the presence of arguments without defaults
Asked Answered
U

3

5

This question is a follow-up to a previous answer which raised a puzzle.

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 )

The question is why does do.call(anova, Models) work fine, as @Roland points out?

The signature for anova is anova(object, ...)

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.

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!

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

Uncle answered 7/8, 2013 at 16:13 Comment(10)
isn't it the same situation as do.call(plot, list(1:2, 1:2, col="red"))? It seems to obey the standard behaviour re positional matching, named arguments and ....Footworn
I'm confused, what's puzzling about do.call(anova, Models) being the same as anova(Models[[1]], Models[[2]], Models[[3]])?Rosio
Because do.call is always talked about as a solution to ..., I guess it's never mentioned that it will also fill in according to the positional matching rules. But this was pretty surprising to me.Uncle
I really don't understand what positional matching rules you're talking about - can you please explain in a couple of sentences?Rosio
@Rosio the positional matching rule is what baptiste referred to: empty arguments (those without defaults or which don't have named arguments specified) are filled in in order.Uncle
@SimonO101 seems like you're missing the point of the "*", rolling back, again...Rosio
do.call just does a basic lexical substitution from do.call(f, list(...)) to f(...) - and you can always pass both named and unnamed arguments along to ...Draughts
@Draughts Can you explain more? "Basic lexical substitution...to f(...)" is similar to what I've read in other places, and is what led me to believe that it substitutes for ... rather than following the usual positional, naming, unnamed rules.Uncle
@AriB.Friedman tried to describe it in the answer below - I think you're missing how ... works, not how do.call worksDraughts
@Draughts I agree that's where the confusion came from. Thanks for the answer.Uncle
D
5

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 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     
Draughts answered 8/8, 2013 at 17:15 Comment(0)
D
2

I think it is worth stressing that the names of the list elements do matter. Hadley mentioned it, but it can be an annoyance. Consider the next example:

x <- rnorm(1000)
y <- rnorm(1000)
z <- rnorm(1000) + 0.2

Models <- list()
Models$xy <- lm(z~x)
Models$yz <- lm(z~y)

# This will fail, because do.call will not assign anything to the argument "object" of anova
do.call(anova, Models)
# This won't
do.call(anova, unname(Models))
Damask answered 8/11, 2013 at 23:25 Comment(0)
M
1

do.call passes the first element of the list to the first argument:

fun <- function(x,...) {
  print(paste0("x=",x))

  list(x, ...)
}

do.call(fun, list(1,2))
# [1] "x=1"
# [[1]]
# [1] 1
# 
# [[2]]
# [1] 2
Mealworm answered 7/8, 2013 at 16:42 Comment(8)
what else could it possibly do?! isn't do.call(f, list(1,2)) by definition supposed to be equivalent to f(1,2)? I'm really not getting the OP.Rosio
Well, we thought that would be the case for f <- function(...), but not for f <- function(x ,...). I expected the latter to give an error with do.call.Mealworm
@Rosio I could only substitute in for the ... and return an error otherwise. It's obviously more useful the way it actually works, but the most common usage is to fill in for the ....Uncle
:) not sure why you'd think that, but okRosio
We were wrong, but thinking that do.call would only fill in the ellipses doesn't seem far-fetched to me, though the examples section of the documentation shows otherwise.Mealworm
This answer is wrong! If the elements of the list is named, the statement is false.Lowbrow
@AntoineLizée The list elements in the question are not named ... You cannot expect an answer to answer something that wasn't asked.Mealworm
I'm not sure to totally agree with that. The spirit of SO is to answer something that is true in general, or make it clear if it is specific to the situation.Lowbrow

© 2022 - 2024 — McMap. All rights reserved.