If error, next iteration in a for loop in R
<p>I'm looking for a simple way to move on to the next iteration in a for loop in R if the operation inside the for loop errors. </p>

<p>I've recreated a simple case below:</p>

<pre><code>for(i in c(1, 3)) {
  test &lt;- try(i+1, silent=TRUE)
  calc &lt;- if(class(test) %in% 'try-error') {next} else {i+1}
  print(calc)
}
</code></pre>

<p>This correctly gives me the following calc values.</p>

<pre><code>[1] 2
[1] 4
</code></pre>

<p>However once I change the vector in i to include a non-numeric value:</p>

<pre><code>for(i in c(1, "a", 3)) {
  test &lt;- try(i+1, silent=TRUE)
  calc &lt;- if(class(test) %in% 'try-error') {next} else {i+1}
  print(calc)
}
</code></pre>

<p>This for loop doesn't work. I was hoping for the same calc values as above with the vector excluding the non-numeric value in i. </p>

<p>I tried using tryCatch as the following: </p>

<pre><code>for(i in c(1, "a", 3)) {
  calc &lt;- tryCatch({i+1}, error = function(e) {next})
  print(calc)
}
</code></pre>

<p>However, I get the following error: </p>

<pre><code>Error in value[[3L]](cond) : no loop for break/next, jumping to top level 
</code></pre>

<p>Could someone please help me understand how I could achieve this using a for loop in R?</p>