title: How to avoid 'sink stack is full' error when sink() is used to capture messages in foreach loop
body: <p>In order to see the console messages output by a function running in a <code>foreach()</code> loop I followed the advice of <a href="http://vikparuchuri.com/blog/monitoring-progress-inside-foreach-loop/" rel="nofollow noreferrer">this guy</a> and added a <code>sink()</code> call like so:</p>
  
  <pre><code>   library(foreach)    
     library(doMC)
     cores &lt;- detectCores()
     registerDoMC(cores)
  
     X &lt;- foreach(i=1:100) %dopar%{
     sink("./out/log.branchpies.txt", append=TRUE)
     cat(paste("\n","Starting iteration",i,"\n"), append=TRUE)
     myFunction(data, argument1="foo", argument2="bar")
     }
  </code></pre>
  
  <p>However, at iteration 77 I got the error 'sink stack is full'. There are <a href="https://stackoverflow.com/questions/8339306/sink-inside-a-for-loop-gives-the-error-sink-stack-is-full-in-r">well-answered questions</a> about avoiding this error when using for-loops, but not foreach. What's the best way to write the otherwise-hidden foreach output to a file?</p>