如何将许多变量和函数从全局环境导出到foreach循环? [英] How to export many variables and functions from global environment to foreach loop?

查看:139
本文介绍了如何将许多变量和函数从全局环境导出到foreach循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在每个并行仿真的开头输出全球环境?下面的代码是被调用来运行模拟的函数的一部分。

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆