How to use% dopar% when only import foreach in package DESCRIPTION

How to avoid "could not find the function"% dopar% "" in the package function when only importing (independent) foreach in the package DESCRIPTION? is there a way like foreach ::% dopar% since i am using foreach :: foreach in a function? Thanks.

The code:

In function

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

IN DESCRIPTION

 Imports: Matrix, parallel, foreach, doParallel 
+5
source share
3 answers

You need to use backlinks: foreach::`%dopar%` (or the quotes foreach::"%dopar%" will also work).

+3
source

@ Kevin Zen,

I had the same problem, but I think I just solved it using the importFrom field in the namespace file. I use Roxygen2 for documentation, so I just added the tag:

 #' @importFrom foreach %dopar% 

with a call to the foreach function. He created a field in the namespace file, for example:

 importFrom(foreach,"%dopar%") 

therefore, if you are not using Roxygen2, you can just put this string in your namespace, and that should do the trick too.

This should prevent the valve from being checked. However, as soon as you try to run the code on a computer that does not yet have the "foreach" package downloaded and connected via:

 library(foreach) 

You will receive a message stating that% dopar% was not found if "foreach" is specified in the "Import" section and not "Depends" in the DESCRIPTION file. Therefore, make sure that foreach is specified in the Dependencies box.

+4
source

The following worked for me. Define a local %do% or %dopar% as follows

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

Then you can run

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

All Articles