r - 如何将 "..."参数传递给从 foreach() 调用的函数?

标签 r foreach parallel-processing

以下代码会产生警告:

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)

我猜这是因为 worker /奴隶没有看到 ...了。形式参数通常通过 .export=c("<arg>") 作为字符向量传递, 但这似乎不适用于 ...参数。

处理...的正确方法是什么?这个例子中的参数?

最佳答案

好吧,显然 ... 参数必须通过 do.i 传递。这是一个更明显(且运行正确)的示例:

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)

关于r - 如何将 "..."参数传递给从 foreach() 调用的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14167068/

相关文章:

html - 格式化迭代记录以平铺格式排列

java - 需要 java.util.ArrayList<String>,找到 java.lang.Object : I do not understand the reason for this error

c# - Parallel.For 有异常行为

c++ - C++ 标准中错误使用 std::memory_order::relaxed 的示例 [n4713 中的 algorithms.parallel.exec/5]

c++ - bind2nd 在 for_each 循环中

scala - 并行化Scala的迭代器

sql - 使用 dplyr 进行 SQL in-db 操作时的 ifelse 和 grepl 命令

r - 在 geom_bar 中按类别定义不同的标签大小

r - read.csv与read.table

r - 创建一个函数,提供与 R 中的 lapply 相同的结果