Michael
Michael

Reputation: 1498

R parallel: error "could not find function "%do%""

This is a snippet of R code (function "Kclust") that tries to perform a task in parallel using library(doParallel).

  result = foreach (r = rseq, .combine=c) %dopar% {
    K=apply(D, 1, function(v){sum(v <= r)-1})
    L=sqrt((diff(xlim)+2*max(rseq))*(diff(ylim)+2*max(rseq))* K /(pi * (dim(tor)[1]-1)))
    foreach (th = thseq) %do% {
      C=which(L>=th)
      if (length(C)>0){
        G = graph.adjacency(D[C,C] < 2 *r)
        lab=clusters(G, "weak")
        labels=(N+1):(2*N); labels[C]=lab$membership
      }
      else labels=1:N
      s=0
      if (score){
        s=scorewprec(labels=labels, pts=pts, sds=sds, xlim=xlim, ylim=ylim, psd=psd, minsd=minsd, maxsd=maxsd, useplabel=useplabel, alpha=alpha, pb=pb)
      }
      list("scale" = r, "score" = s, "thresh" = th, "labels" = labels)
    }
  }

The code runs fine with:

R version 3.0.2 (2013-09-25) -- "Frisbee Sailing". Platform: x86_64-pc-linux-gnu (64-bit)

However, if I run the code with:

R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree". Platform: x86_64-w64-mingw32/x64 (64-bit).

I get

Error in { : task 1 failed - "could not find function "%do%"" Calls: sapply -> lapply -> FUN -> Kclust -> %dopar% -> Execution halted

What could be the problem?

Upvotes: 2

Views: 1775

Answers (1)

loki
loki

Reputation: 10350

as @Roland mentioned in his Comment, you have to export the foreach package to the individual workers. This can be done like this:

result = foreach (r = rseq, .combine=c, .packages = c("foreach")) %dopar% {
  #some code...
  # now use the foreach function from the foreach package again
  foreach (th = thseq) %do% {
  #...
  }
}

just like this, you have to export all used libraries to the workers as a vector of characters. Therefore I added the c("packageA", "packageB", "etc.").

Upvotes: 1

Related Questions