11

I'm trying to decrease run time of my code by using doParallel package in R.

I'm calling a function awareRateSIR that some extra packages are used in the body of this function. I get some error like

could not find function "vcount" and..

I know vcount is a function of package igraph that is used in awareRateSIR) but it's not the only one. How can I solve this problem? I've thought I should pass all packages name that is used in my function awareRateSIR but I don't know how I cant export multiple function in foreach or how I can export multiple package name.

This is my code:

tp<-foreach(i=1:iter,  .inorder = FALSE, .export = "awareRateSIR",
          .packages = "igraph", .packages="doParallel")%dopar%{
tp <- awareRateSIR(graphContact, graphCom,state)
return(tp)
}

if I don't pass these packages I will get error states some function are unknown if I pass all the packages I will get error:

Error in foreach(i = 1:iter, .inorder = FALSE, .export = "awareRateSIR", : formal argument ".packages" matched by multiple actual arguments"

thanks in advance

lmo
  • 37,904
  • 9
  • 56
  • 69
ElhamMotamedi
  • 199
  • 1
  • 2
  • 12

2 Answers2

23

You should pass all the packages required, using c function, as below :

tp<-foreach(i=1:iter,  .inorder = FALSE, .export = "awareRateSIR",
          .packages = c("igraph", "doParallel"))%dopar%{
tp <- awareRateSIR(graphContact, graphCom,state)
return(tp)
}
Kumar Manglam
  • 2,780
  • 1
  • 19
  • 28
2

Usually, you don't need to export any variable, function or package when using foreach.

For variables and functions, they just need to be in the same environment as the foreach statement (the better is to make a function where you pass only what you need).

When functions are required from other packages, you just need to use package::function, like inside packages.


E.g. you can do

library(doParallel)
registerDoParallel(cl <- makeCluster(3))

df <- iris

foreach(spec = unique(iris$Species)) %dopar% {
  dplyr::filter(df, Species == !!spec)
}

stopCluster(cl)
F. Privé
  • 11,423
  • 2
  • 27
  • 78
  • Could you elaborate on the last point by giving some examples, since I am currently having an exact problem where I tried to use `foreach` on multiple functions each of them contains many other sub-functions? – user177196 May 02 '22 at 17:48
  • 1
    @user177196 Please see my edit. – F. Privé May 04 '22 at 06:32