mathlete
mathlete

Reputation: 6682

How to pass "..." arguments to a function called from foreach()?

The following code produces the warning:

Warning message:
<anonymous> : <anonymous>: ... may be used in an incorrect context: ‘mean(x[l], ...)’

doForeach <- function(x, ...)
{
    require(doSNOW)
    require(foreach)
    cl <- snow::makeCluster(2, type="MPI")
    on.exit(snow::stopCluster(cl))
    registerDoSNOW(cl)
    do.i <- function(i) lapply(seq_len(length(x)), function(l) mean(x[l], ...))
    foreach(i=seq_len(10)) %dopar% { do.i(i) }
}
x <- rnorm(20)
r <- doForeach(x, trim=1)

I'd guess that it comes from the fact that the workers/slaves do not see the ... anymore. Formal arguments are typically passed as character vectors via .export=c("<arg>"), but that does not seem to work for ... arguments.

What's the correct way of dealing with ... arguments in this example?

Upvotes: 3

Views: 1773

Answers (1)

mathlete
mathlete

Reputation: 6682

Okay, apparently the ... arguments have to be passed via do.i. Here is a more obvious (and correctly running) example:

doForeach <- function(x, ...)
{
    require(doSNOW)
    require(foreach)
    cl <- snow::makeCluster(2, type="MPI")
    on.exit(snow::stopCluster(cl))
    registerDoSNOW(cl)
    do.i <- function(i, ...) lapply(seq_len(length(x)), function(l) max(x[l], ...))
    foreach(i=seq_len(5)) %dopar% { do.i(i, ...) }
}
x <- 1:3
doForeach(x, 1.5)

Upvotes: 2

Related Questions