简体   繁体   English

如果仅在包的描述中导入foreach,如何使用%dopar%

[英]how to use %dopar% when only import foreach in DESCRIPTION of a package

How to avoid "could not find function "%dopar%"" in a function of a package when only imports (not depends) foreach in DESCRIPTION of a package? 如果在包的描述中仅导入(不依赖)foreach,如何在包的功能中避免“找不到函数”%dopar%“”? is there a way like foreach::%dopar% as I use foreach::foreach in function? 有没有像foreach ::%dopar%的方式,因为我在函数中使用foreach :: foreach? Thank you. 谢谢。

Code like: 代码如:

In function 在功能上

foreach::foreach(1:9) %dopar% {

...}

In DESCRIPTION 在描述中

Imports: 
    Matrix,
    parallel,
    foreach,
   doParallel

The following worked for me. 以下对我有用。 Define a local %do% or %dopar% as follows 如下定义局部%do%%dopar%

`%dopar%` <- foreach::`%dopar%`
`%do%` <- foreach::`%do%`

Then you should be able to run 然后你应该能够跑

foreach::foreach(i = 1:9, .combine = "+") %dopar% {i}
foreach::foreach(i = 1:9, .combine = "+") %do% {i}

你需要使用反引号: foreach::`%dopar%` (或引号foreach::"%dopar%"也可以使用)。

@Kevin Zen, @Kevin Zen,

I was having the same issue, but I think I just resolved it by using the "importFrom" field in the namespace file. 我遇到了同样的问题,但我想我只是通过使用命名空间文件中的“importFrom”字段来解决它。 I use Roxygen2 to document, so I simply included the tag: 我使用Roxygen2来记录,所以我只是包含了标签:

#' @importFrom foreach %dopar%

with the function foreach is called in. It created a field in the namespace file like such: 调用foreach函数。它在命名空间文件中创建了一个字段,如下所示:

importFrom(foreach,"%dopar%")

so if you aren't using Roxygen2, you could just put that line in your namespace and that should do the trick as well. 因此,如果您不使用Roxygen2,您可以将该行放在命名空间中,这也应该可以解决问题。

That should prevent the cran check from complaining. 这应该可以防止颅骨检查抱怨。 However, once you try to run the code on a computer that doesn't already have the "foreach" package loaded and attached via: 但是,一旦您尝试在尚未通过以下方式加载和附加“foreach”包的计算机上运行代码:

library(foreach)

you will get a message that %dopar% is not found if "foreach" is listed under "Imports" rather than "Depends" in your DESCRIPTION file. 如果“foreach”列在“导入”下而不是“取决于”描述文件中,则会收到%dopar%未找到的消息。 So make sure foreach is listed in the "Depends" field. 因此,请确保foreach列在“Depends”字段中。

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

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