如何将许多变量和函数从全局环境导出到foreach循环? [英] How to export many variables and functions from global environment to foreach loop?
问题描述
num.cores< - detectCores() - 1
cluztrr< - makeCluster(num.cores)
registerDoParallel(cl = cluztrr)
sim.result.list< - foreach(r = 1:simulations,
.combine = list,
.multicombine = TRUE,
)%dopar%{
#...使用多个变量的吨计算...
list(vals1,
vals2,
vals3)
}
stopCluster(cluztrr)
是否有必要使用.export每个变量和函数的字符向量来使用?如果foreach循环在全局环境中,变量应该被自动导出。如果foreach循环在全局环境中,变量应该被自动导出。如果没有,可以使用 .export = ls(globalenv())
(或 .GlobalEnv
)。
对于其他包中的函数,只需使用语法 package :: function
。
How can I export the global environment for the beginning of each parallel simulation in foreach? The following code is part of a function that is called to run the simulations.
num.cores <- detectCores()-1
cluztrr <- makeCluster(num.cores)
registerDoParallel(cl = cluztrr)
sim.result.list <- foreach(r = 1:simulations,
.combine = list,
.multicombine = TRUE,
) %dopar% {
#...tons of calculations using many variables...
list(vals1,
vals2,
vals3)
}
stopCluster(cluztrr)
Is it necessary to use .export with a character vector of every variable and function that I use? Would that be slow in execution time?
If the foreach loop is in the global environment, variables should be exported automatically. If not, you can use .export = ls(globalenv())
(or .GlobalEnv
).
For functions from other packages, you just need to use the syntax package::function
.
这篇关于如何将许多变量和函数从全局环境导出到foreach循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!