title: R use tryCatch within for loop to add row with error values to output
body: <p>forgive me if I missed the right way to do this here, but I can't seem to make progress. <a href="https://stackoverflow.com/questions/14748557/skipping-error-in-for-loop">Skipping error in for-loop</a> helped a lot, as did some of the other answers related to tryCatch, but I'm still struggling. <a href="https://stackoverflow.com/questions/31689648/using-trycatch-in-r-to-assign-error-values-in-loop">using tryCatch() in R to assign error values in loop</a> didn't work for me, or I'm missing something.</p>
  
  <p>I am running a for loop with tryCatch, but if I get an error, I would like to record it as a row in the resulting matrix. I can't seem to get the error function output up one level to the loop to be recorded. Here is a simple version of what I'm trying:</p>
  
  <pre><code>collectem &lt;- function(eList){ 
    tmpList &lt;- NULL
    for (e in eList){
      tryCatch({
      tmpVar &lt;- c("foo", e)
      if (e==3) stop("BLAH!")
      }, error=function(d){c("No",d) -&gt; tmpVar})
      tmpList &lt;- rbind(tmpList, tmpVar)
    }
    return(tmpList)
  }
  </code></pre>
  
  <p>Call:</p>
  
  <pre><code>x &lt;- collectem(1:10)
  </code></pre>
  
  <p>Which results in:</p>
  
  <pre><code>&gt; x
         [,1]  [,2]
  tmpVar "foo" "1" 
  tmpVar "foo" "2" 
  tmpVar "foo" "3" 
  tmpVar "foo" "4" 
  tmpVar "foo" "5" 
  tmpVar "foo" "6" 
  tmpVar "foo" "7" 
  tmpVar "foo" "8" 
  tmpVar "foo" "9" 
  tmpVar "foo" "10"
  </code></pre>
  
  <p>But I'm looking for this:</p>
  
  <pre><code>x
         [,1]  [,2]   
  tmpVar "foo" "1"    
  tmpVar "foo" "2"    
  tmpVar "No"  "BLAH!"
  tmpVar "foo" "4"    
  tmpVar "foo" "5"    
  tmpVar "foo" "6"    
  tmpVar "foo" "7"    
  tmpVar "foo" "8"    
  tmpVar "foo" "9"    
  tmpVar "foo" "10"   
  </code></pre>
  
  <p>Thanks!!</p>