简体   繁体   English

R package 设计:如何将内部函数导出到集群

[英]R package design: how to export internal functions to a cluster

I'm working on an R package, and I need to run a function myfun on a cluster using parallel::parLapply .我正在研究 R package,我需要使用parallel::parLapply在集群上运行 function myfun myfun calls several additional functions from my package, which in turn call more functions, some of which have multiple methods... so passing all of the functions and methods to the cluster explicitly by name is very cumbersome. myfun从我的 package 中调用了几个额外的函数,这些函数又调用了更多的函数,其中一些函数有多个方法……所以通过名称显式地将所有函数和方法传递给集群非常麻烦。

The standard advice , as I understand it, is to run parallel::clusterEvalQ({library("my_package")}) .据我了解, 标准建议是运行parallel::clusterEvalQ({library("my_package")}) But the call to library("my_package") is apparently anathema to R-CMD-check.但是对library("my_package")的调用显然是对 R-CMD-check 的厌恶。 And I have reason to believe that my-package:::function won't fly on CRAN either.而且我有理由相信my-package:::function也不会在 CRAN 上飞行。

What is the standard approach here?这里的标准方法是什么? Do I need to export every single relevant function and method by name?我是否需要按名称导出每个相关的 function 和方法?

Ok, this seems to work, (it passes R-CMD-check on GitHub):好的,这似乎可行,(它通过了 GitHub 上的 R-CMD-check):

parallel::clusterExport(cl = cl, 
                        unclass(lsf.str(envir = asNamespace("my_package"), 
                                        all = T)),
                        envir = as.environment(asNamespace("my_package"))
                        )

Hope it's useful to others.希望对其他人有用。

There's also probably a nifty solution available via the globals package, but I haven't been able to get this to pass checks on GitHub actions.也可能通过globals package 提供了一个漂亮的解决方案,但我无法通过对 GitHub 操作的检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM