@ben-bolker,我也有类似的愿望,并认为我已经制定了初步解决方案,我也已经测试过可以并行工作。该函数,我有点混淆地称为gmcmapply
(g 表示网格)采用一个任意大的命名列表mvars
(在函数内获取expand.grid
-ed)和一个FUN
使用列表名称的函数,就好像它们是函数本身的参数一样(gmcmapply
将更新的形式FUN
这样当FUN
传递给mcmapply
它的参数时,它的参数就会反映用户想要迭代的变量(这将是嵌套 for 循环中的层)。mcmapply
然后动态更新这些形式的值,因为它在mvars
.
我已经发布了初步代码作为一个要点(用下面的例子转载),很想得到你的反馈。我是一名研究生,自称是中级 R 爱好者,所以这肯定会推动我的 R 技能。您或社区中的其他人可能会提出可以改进我所拥有的建议。我确实认为,即使是这样,我将来也会经常使用这个功能。
gmcmapply <- function(mvars, FUN, SIMPLIFY = FALSE, mc.cores = 1, ...){
require(parallel)
FUN <- match.fun(FUN)
funArgs <- formals(FUN)[which(names(formals(FUN)) != "...")] # allow for default args to carry over from FUN.
expand.dots <- list(...) # allows for expanded dot args to be passed as formal args to the user specified function
# Implement non-default arg substitutions passed through dots.
if(any(names(funArgs) %in% names(expand.dots))){
dot_overwrite <- names(funArgs[which(names(funArgs) %in% names(expand.dots))])
funArgs[dot_overwrite] <- expand.dots[dot_overwrite]
#for arg naming and matching below.
expand.dots[dot_overwrite] <- NULL
}
## build grid of mvars to loop over, this ensures that each combination of various inputs is evaluated (equivalent to creating a structure of nested for loops)
grid <- expand.grid(mvars,KEEP.OUT.ATTRS = FALSE, stringsAsFactors = FALSE)
# specify formals of the function to be evaluated by merging the grid to mapply over with expanded dot args
argdefs <- rep(list(bquote()), ncol(grid) + length(expand.dots) + length(funArgs) + 1)
names(argdefs) <- c(colnames(grid), names(funArgs), names(expand.dots), "...")
argdefs[which(names(argdefs) %in% names(funArgs))] <- funArgs # replace with proper dot arg inputs.
argdefs[which(names(argdefs) %in% names(expand.dots))] <- expand.dots # replace with proper dot arg inputs.
formals(FUN) <- argdefs
if(SIMPLIFY) {
#standard mapply
do.call(mcmapply, c(FUN, c(unname(grid), mc.cores = mc.cores))) # mc.cores = 1 == mapply
} else{
#standard Map
do.call(mcmapply, c(FUN, c(unname(grid), SIMPLIFY = FALSE, mc.cores = mc.cores)))
}
}
下面的示例代码:
# Example 1:
# just make sure variables used in your function appear as the names of mvars
myfunc <- function(...){
return_me <- paste(l3, l1^2 + l2, sep = "_")
return(return_me)
}
mvars <- list(l1 = 1:10,
l2 = 1:5,
l3 = letters[1:3])
### list output (mapply)
lreturns <- gmcmapply(mvars, myfunc)
### concatenated output (Map)
lreturns <- gmcmapply(mvars, myfunc, SIMPLIFY = TRUE)
## N.B. This is equivalent to running:
lreturns <- c()
for(l1 in 1:10){
for(l2 in 1:5){
for(l3 in letters[1:3]){
lreturns <- c(lreturns,myfunc(l1,l2,l3))
}
}
}
### concatenated outout run on 2 cores.
lreturns <- gmcmapply(mvars, myfunc, SIMPLIFY = TRUE, mc.cores = 2)
Example 2. Pass non-default args to FUN.
## Since the apply functions dont accept full calls as inputs (calls are internal), user can pass arguments to FUN through dots, which can overwrite a default option for FUN.
# e.g. apply(x,1,FUN) works and apply(x,1,FUN(arg_to_change= not_default)) does not, the correct way to specify non-default/additional args to FUN is:
# gmcmapply(mvars, FUN, arg_to_change = not_default)
## update myfunc to have a default argument
myfunc <- function(rep_letters = 3, ...){
return_me <- paste(rep(l3, rep_letters), l1^2 + l2, sep = "_")
return(return_me)
}
lreturns <- gmcmapply(mvars, myfunc, rep_letters = 1)
我想添加但仍在尝试解决的一些附加功能是
将输出清理为一个带有 mvar 名称的漂亮嵌套列表(通常,我会在嵌套的 for 循环中创建多个列表,并将低级列表标记到更高级别的列表上,直到巨大嵌套的所有层循环完成)。我认为使用此处提供的解决方案的一些抽象变体会起作用,但我还没有弄清楚如何使解决方案灵活适应expand.grid
-ed data.frame 中的列数。
我想要一个选项来记录在mcmapply
用户指定的目录中调用的子进程的输出。因此,您可以查看生成的每个变量组合的 .txt 输出expand.grid
(即,如果用户打印模型摘要或状态消息FUN
作为我经常做的一部分)。我认为一个可行的解决方案是使用substitute()
andbody()
功能,描述here to edit FUN
to open a sink()
at the beginning FUN
and close it at the end if user specified a directory to write to。现在,我只是将它直接编程到FUN
自身中,但稍后只传递gmcmapply
一个名为log_children = "path_to_log_dir
. 然后将函数的主体编辑为(伪代码)sink(file = file.path(log_children, paste0(paste(names(mvars), sep = "_"), ".txt")
让我知道你的想法!
-内特