title: Using parLapply and clusterExport inside a function
body: <p>I asked a related question here and the response worked well:  <a href="https://stackoverflow.com/questions/12019638/using-parlapply-for-parallel-computing">using parallel&#39;s parLapply: unable to access variables within parallel code</a></p>
  
  <p>The problem is when I try to use the answer inside of the function it won't work as I think it has to the default environment of <code>clusterExport</code>.  I've read the vignette and looked at the help file but am approaching this with a very limited knowledge base.  The way I used <code>parLapply</code> I expected it to behave similar to <code>lapply</code> but it doesn't appear to.</p>
  
  <p>Here is my attempt:</p>
  
  <pre><code>par.test &lt;- function(text.var, gc.rate=10){ 
      ntv &lt;- length(text.var)
      require(parallel)
      pos &lt;-  function(i) {
          paste(sapply(strsplit(tolower(i), " "), nchar), collapse=" | ")
      }
      cl &lt;- makeCluster(mc &lt;- getOption("cl.cores", 4))
      clusterExport(cl=cl, varlist=c("text.var", "ntv", "gc.rate", "pos"))
      parLapply(cl, seq_len(ntv), function(i) {
              x &lt;- pos(text.var[i])
              if (i%%gc.rate==0) gc()
              return(x)
          }
      )
  }
  
  par.test(rep("I like cake and ice cream so much!", 20))
  
  #gives this error message
  &gt; par.test(rep("I like cake and ice cream so much!", 20))
  Error in get(name, envir = envir) : object 'text.var' not found
  </code></pre>