I am currently working with some large datasets, so parallelizing the workflows is the only way to go.
I need to load some packages to each thread once at the beginning
(i.e: for(this.thread in threads) { #load some packages }
.
Unfortunately , I'm not sure how to do that.
The following code further illustrates my problem, where I am trying to use the pipe operator from magrittr
in a %dopar%
:
.
library(parallel)
library(doParallel)
library(foreach)
library(magrittr)
# Generate some random data and function :
# -----------------------------------------
randomData = runif(10^3)
randomFunction = function(x) {x * (2^x) }
randomData[1] %>% randomFunction #Works
# And now ... The parallel part :
# --------------------------------
myCluster = makeCluster(6)
registerDoParallel(myCluster)
# Test that the do par is up and running:
foreach(i = randomData) %dopar% { i }
# Use magrittr pipe operator:
# Error in { : task 1 failed - "could not find function "%>%""
foreach(i = randomData) %dopar% { i %>% randomFunction }
# Load the library at each loop: (ie: length(data) times !)
# Other than unnecessarily loading the library (length(data) - numberOfThreads) times,
# it works nicely
foreach(i = randomData) %dopar% { library(magrittr); i %>% randomFunction }
# Now try without re-loading:
# Tararaa - (ie: Works nicely)
foreach(i = randomData) %dopar% { i %>% randomFunction }
.
Any ideas?