title: Use tryCatch with a while loop in R
body: <p>I am trying to implement tryCatch with a while loop in R, but have been running into issues. I have tried to implement a number of the proposed solutions (which revolve around for loops), but with no success.</p>
  
  <p>Essentially I am querying an API with R and looping through a number of relevant parameters (longitudes and latitudes to be precise). The reason why I need a tryCatch block is because sometimes the URL request will fail, which in turn stops the script running. What I want to be able to do is to ignore the error, increase the looping counter by 1 and continue extraction.</p>
  
  <p>The while loop I have set up is (FYI - length refers to length of dataframe being looped over):</p>
  
  <pre><code>i &lt;- 1
  while(i &lt;= length) {
  x_cord &lt;- geocode_area$X[i]
  y_cord &lt;- geocode_area$Y[i]
  target &lt;- getUrl(x_cord,y_cord)
  dat &lt;- fromJSON(target)
  geocode_area$Block[i] &lt;- dat$result$geographies$`2010 Census Blocks`[[1]]$BLOCK
  print(paste(i/length*100,"% completed",sep=""))
  print(dat$result$geographies$`2010 Census Blocks`[[1]]$BLOCK)
  i &lt;- i + 1 
  }
  </code></pre>
  
  <p>The getUrl() function is defined as:</p>
  
  <pre><code>getUrl &lt;- function(x,y) {
  root &lt;- "http://geocoding.geo.census.gov/geocoder/geographies/coordinates?"
  u &lt;- paste0(root,"x=", x,"&amp;y=", y,"&amp;benchmark=4&amp;vintage=4&amp;format=json")
  return(URLencode(u))
  }
  </code></pre>
  
  <p>The input data.frame to the while loop looks like so (note I have thrown in the character strings to simulate an error to test that tryCatch is working):</p>
  
  <pre><code>          X                 Y          Block
  1 -122.425891675136 37.7745985956747     0
  2  -122.42436302145 37.8004143219856     0
  3 -122.426995326766 37.8008726327692     0
  4 -122.438737622757 37.7715411720578     0
  5               abc            zsads     0
  </code></pre>
  
  <p>I have tried a number of the SO and other solutions, but the results do not seem to be working properly. Can anyone help?</p>
  
  <p>Thanks!</p>
  
  <p>Jack</p>