I think I have a solution to my own question, but maybe someone can do better (and I did not implement FLATTEN=FALSE ...)
xapply <- function(FUN,...,FLATTEN=TRUE,MoreArgs=NULL) { L <- list(...) inds <- do.call(expand.grid,lapply(L,seq_along)) ## Marek suggestion retlist <- list() for (i in 1:nrow(inds)) { arglist <- mapply(function(x,j) x[[j]],L,as.list(inds[i,]),SIMPLIFY=FALSE) if (FLATTEN) { retlist[[i]] <- do.call(FUN,c(arglist,MoreArgs)) } } retlist }
edit . I tried the @baptiste suggestion, but it is not easy (or not for me). The closest I got
xapply2 <- function(FUN,...,FLATTEN=TRUE,MoreArgs=NULL) { L <- list(...) xx <- do.call(expand.grid,L) f <- function(...) { do.call(FUN,lapply(list(...),"[[",1)) } mlply(xx,f) }
which still does not work. expand.grid really more flexible than I thought (although it creates a strange data frame that cannot be printed), but enough magic happens inside mlply that I cannot get it to work.
Here is a test case:
L1 <- list(data.frame(x=1:10,y=1:10), data.frame(x=runif(10),y=runif(10)), data.frame(x=rnorm(10),y=rnorm(10))) L2 <- list(y~1,y~x,y~poly(x,2)) z <- xapply(lm,L2,L1) xapply(lm,L2,L1)