title: nested foreach loops in R to update common array
body: <p>I am trying to use a couple of foreach loops in R to fill out a common array in parallel. A very simplified version of what I am trying to do is:</p>
  
  <pre><code>library(foreach)
  set.seed(123)
  x &lt;- matrix(NA, nrow = 8, ncol = 2)
  
  foreach(i=1:8) %dopar% {
      foreach(j=1:2) %do% {
  
        l &lt;- runif(1, i, 100)
        x[i,j] &lt;- i + j + l     #This is much more complicated in my real code.   
  
      }
  }
  </code></pre>
  
  <p>I would like to code to update the matrix <code>x</code> in parallel and have the output look like:</p>
  
  <pre><code>&gt; x
         [,1]      [,2]
   [1,]  31.47017  82.04221
   [2,]  45.07974  92.53571
   [3,]  98.22533  12.41898
   [4,]  59.69813  95.67223
   [5,]  63.38633  55.37840
   [6,] 102.94233  56.61341
   [7,]  78.01407  69.25491
   [8,]  26.46907 100.78390 
  </code></pre>
  
  <p>However, I cannot seem to figure out how to get the array to be updated. I have tried putting the <code>x &lt;-</code> elsewhere, but it doesn't seem to like it. I think this will be a very easy thing to fix, but all my searching has not lead me there yet. Thanks.</p>