title: Debugging foreach in R using dorng
body: <p>I am parallelizing a task in R using <code>foreach</code> loop with reproducible results using the <code>dorng</code> operator. It is a complex code, and I have an error that I have not been able to identify even though I have run the same code with a regular <code>for</code> loop.</p>
  <p>My fundamental question is: how do I debug a function within of a <code>foreach</code> loop assuming that I have reproducible results? Below is my current tentative.</p>
  <p>In the <a href="https://cran.r-project.org/web/packages/doRNG/vignettes/doRNG.pdf" rel="nofollow noreferrer">vignette</a> of the doRNG package, it says that a sequence of random seeds will be generated and set at the beginning of each iteration using the R number generator &quot;L’Ecuyer-CMRG&quot;. The sequence of random seeds can be defined using <code>set.seed</code> before the <code>foreach</code> loop:</p>
  <pre><code>library(doRNG)
  library(doParallel)
  library(foreach)
  
  registerDoParallel(2)
  set.seed(1234)
  f01 &lt;- foreach(i = 1:100, .combine = 'c')  %dorng% {
    out &lt;- 1 + i
  }
  r01 &lt;- attr(f01, &quot;rng&quot;)
  
  set.seed(1234)
  f02 &lt;- foreach(i = 1:100, .combine = 'c')  %dorng% {
    out &lt;- 1 + i
  }
  r02 &lt;- attr(f02, &quot;rng&quot;)
  </code></pre>
  <p>The objects <code>r01</code> and <code>r02</code> contain the sequence of seeds used in <code>f01</code> and <code>f02</code>,</p>
  <pre><code>identical(f01, f02)
  identical(r01, r02)
  </code></pre>
  <p>such that the results from the <code>foreach</code> loop and their seeds are identical as expected!</p>
  <p>Then, let's consider the case when the <code>foreach</code> loop will give me a random error:</p>
  <pre><code>set.seed(1234)
  f03 &lt;- foreach(i = 1:100, .combine = 'c')  %dorng% {
    u &lt;- floor(runif(1, 1, 101))
    
    if (i == as.integer(u)){
      out &lt;- &quot;a&quot; + &quot;b&quot;
    } else {
      out &lt;- 1 + i
    }
  
  }
  Error in { : task 67 failed - &quot;non-numeric argument to binary operator&quot;
  </code></pre>
  <p>The error occurs at iteration 67 and it is very easy to understand the error. Unfortunately, it is not the same in my case.</p>
  <p>I would like to be able to use <code>debug</code> and walk through my function to understand the error. From the best of my knowlegde, I cannot use <code>debug</code> inside a <code>foreach</code> loop in R.</p>
  <p>Then, I thought about capturing the error in a regular <code>for</code> loop, but running my code is very slow and, apparently, I cannot observe the error with a low number of iterations. I need to understand the error using <code>foreach</code>.</p>
  <p>Although I can't recover the sequence of seeds from <code>f03</code>, I know that they will be identical to <code>r01</code> or <code>r02</code>. For iteration 67, I have</p>
  <pre><code>r01[[67]]
  [1]       10407  1484283582  -741709185   513087691   132931819
  [6]  1318506528 -1383054295
  </code></pre>
  <p>Therefore, I guess that fixing my seed at <code>r01[[67]]</code> should give me the same error:</p>
  <pre><code>i &lt;- 67
  set.seed(r01[[67]], kind = &quot;L'Ecuyer-CMRG&quot;)
  u &lt;- floor(runif(1, 1, 101))
    if (i == as.integer(u)){
      out &lt;- &quot;a&quot; + &quot;b&quot;
    } else {
      out &lt;- 1 + i
  }
  u
  [1] 74
  </code></pre>
  <p>which is not true.</p>
  <p>In the doRNG <a href="https://cran.r-project.org/web/packages/doRNG/vignettes/doRNG.pdf" rel="nofollow noreferrer">vignette</a>, page 6, they have an example of using a seed in a loop from a previous loop:</p>
  <pre><code>set.seed(1234)
  ex01 &lt;- foreach(i=1:5) %dorng% { runif(3) }
  ex02 &lt;- foreach(i=1:5, .options.RNG=attr(ex01, 'rng')[[2]]) %dorng% { runif(3) }
  identical(ex02[1:4], ex01[2:5])
  </code></pre>
  <p>What am I missing?</p>