title: Environment when running function using mclapply
body: <p>I have a function like the following:</p>
  
  <pre><code>fxn &lt;- function(X) {
      data &lt;- replicate(10, rnorm(10000)) 
      clusters &lt;- kmeans(data, X)
      write.csv(clusters$cluster, paste0("kmeans", X, ".csv"))}
  </code></pre>
  
  <p>I want to use mclapply to iterate it in parallel.</p>
  
  <pre><code>list &lt;- list(10, 50, 100, 150, 200, 250, 300)
  mclapply(list, fxn, mc.cores = 8)
  </code></pre>
  
  <p>This is a very simplified version of my function and use-case, but I want to use it to clarify how environments are handled when using a user-defined function and mclapply. </p>
  
  <p>Because this is being processed in parallel on the same RAM, I was wondering whether the mclapply function could get "confused" at some point and mix up either <code>data</code> or <code>clusters</code> for a different parameter (as defined in <code>list</code>)(by overwriting <code>data</code> and <code>clusters</code> and using the variable which was made using the wrong <code>X</code>). I am aware that each function maintains its own environment, but as the same function is being used several times at once, I want to confirm how this works.</p>
  
  <p>I would really appreciate it if you could clarify this for me or point me in the right direction.</p>
  
  <p>Thanks!</p>