0

I'm not sure how to fix this issue. I've tried adding the .packages="foreach" as in here Function not found in R doParallel 'foreach' - Error in { : task 1 failed - "could not find function "raster"" but cannot get it to work. Any help is greatly appreciated!

####################
install.packages("doParallel")
install.packages("doSNOW")
install.packages("doParallel") 
install.packages("doMPI")
install.packages("tidyverse")

# Load packages -----------------------------------------------------------
rm(list = ls())
library(tidyverse)
library(foreach)
library(doSNOW)
library(doMPI)
library(doParallel)
registerDoParallel()

# Single iteration function -------------------------------------------------------------
one_iteration <- function(snps, estuaries) {
  sim_snps <- matrix(data = NA, nrow = snps, ncol = estuaries) %>% 
    as.data.frame(.)
  for (i in 1:snps) {
    sim_snps[i, ] <- sample(x = c(0,0,0, 1,1,1), size = estuaries, replace = F)
  }
  nbig <- rowSums(sim_snps)

  c(sum(nbig == 3), sum(nbig == 2), sum(nbig == 1), sum(nbig == 0))
}

# Permutations function ---------------------------------------------------
doNullPermutations <- function(nit, snps, estuaries) {
  start <- summary(proc.time())[3] # get time at start of function
  x <- foreach(iteration = 1:nit, # do 100 iterations
               .combine = rbind) %dopar% one_iteration(snps, estuaries)
  print(summary(proc.time())[3] - start) # print time elapse (time at end minus the start)
  return(x)
}

# 3 estuaries -------------------------------------------------------------
perms_res_3est_10kperm <- doNullPermutations(1, snps = 20083, estuaries = 3)
**Error in one_iteration(snps, estuaries) : 
  task 1 failed - "could not find function "one_iteration""
Called from: e$fun(obj, substitute(ex), parent.frame(), e$data)**
AGE
  • 169
  • 6

1 Answers1

2

The code below should work. The reason you are getting this error is that the foreach function creates new workers in new sessions, and in those sessions functions and packages in the main session aren't exported. So note the .export argument, and the .packages argument.

# Permutations function ---------------------------------------------------
doNullPermutations <- function(nit, snps, estuaries) {
    x <- foreach(iteration = 1:3, # do 100 iterations
               .combine = rbind,
               .export = ls(globalenv()),
               .packages = 'tidyverse') %dopar% one_iteration(snps, estuaries)
    return(x)
}

perms_res_3est_10kperm <- doNullPermutations(1, snps = 20083, estuaries = 3)