title: using parallel's parLapply: unable to access variables within parallel code
body: <p>I recently got a computer with several cores and am learning to use parallel computing.  I'm fairly proficient with <code>lapply</code> and was told <code>parLapply</code> works very similarly.  I'm not operating it correctly though.  It seems I have to explicitly put everything inside the <code>parLapply</code> to make it work (that is functions to be use, variables etc.).  With <code>lapply</code> it reads from the parent environment and <code>parLapply</code> does not seem to do this.  So in my example below I could make everything work by placing all info inside <code>parLapply</code> but if I use this inside a user defined function I can't explicitly put <code>text.var</code> inside of <code>parLapply</code>.  </p>
  
  <pre><code>library(parallel)
  text.var &lt;- rep("I like cake and ice cream so much!", 20)
  ntv &lt;- length(text.var)
  gc.rate &lt;- 10
  
  pos &lt;-  function(i) {
      paste(sapply(strsplit(tolower(i), " "), nchar), collapse=" | ")
  }
  
  lapply(seq_len(ntv), function(i) {
          x &lt;- pos(text.var[i])
          if (i%%gc.rate==0) gc()
          return(x)
      }
  
  )
  
  #doesn't work
  cl &lt;- makeCluster(mc &lt;- getOption("cl.cores", 4))
  parLapply(cl, seq_len(ntv), function(i) {
          x &lt;- pos(text.var[i])
          if (i%%gc.rate==0) gc()
          return(x)
      }
  
  )
  
  #does work but have to specify all the stuff inside parLapply
  cl &lt;- makeCluster(mc &lt;- getOption("cl.cores", 4))
  parLapply(cl, seq_len(ntv), function(i) {
          ######stuff I have to put inside parLapply##########
          text.var &lt;- rep("I like cake and ice cream so much!", 20)
          ntv &lt;- length(text.var)
          gc.rate &lt;- 10
          pos &lt;-  function(i) {
              paste(sapply(strsplit(tolower(i), " "), nchar), collapse=" | ")
          }
          ######stuff I have to put inside parLapply##########
          x &lt;- pos(text.var[i])
          if (i%%gc.rate==0) gc()
          return(x)
      }
  )
  </code></pre>
  
  <p>How can I pass <code>text.var</code>, <code>ntv</code>, <code>gc.rate</code>, and <code>pos</code> to <code>parLapply</code> without explicitly putting them inside? (I'm guessing you pass them as a list somehow)</p>
  
  <p>PS windows 7 machine so I need to use <code>parLapply</code> I think</p>