title: Parallel computation: Loading packages in each thread only once
body: <p>I am currently working with some large datasets, so parallelizing the workflows is the only way to go.</p>
  
  <p>I need to load some packages to each thread once at the beginning 
  (i.e: <code>for(this.thread in threads) { #load some packages }</code>.</p>
  
  <p>Unfortunately , I'm not sure how to do that. </p>
  
  <p>The following code further illustrates my problem, where I am trying to use the pipe operator from <code>magrittr</code> in a <code>%dopar%</code> :</p>
  
  <p>. </p>
  
  <pre><code>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] %&gt;% 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 "%&gt;%""
  foreach(i = randomData) %dopar% { i %&gt;% 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 %&gt;% randomFunction }
  
  
  # Now try without re-loading: 
  # Tararaa - (ie: Works nicely)
  foreach(i = randomData) %dopar% { i %&gt;% randomFunction }
  </code></pre>
  
  <p>. </p>
  
  <p>Any ideas?</p>