title: Dynamically changing the sequence of a loop
body: <p>I am trying to scrape data from a website which is unfortunately located on a very unreliable sever which has very volatile reaction times. The first idea is of course to loop over the list of (thousands of) URLs and saving the downloaded results by populating a list.</p>
  
  <p>The problem however is that the server randomly responds very slowly which results into a timeout error. This alone would not be a problem as I can use the <code>tryCatch()</code> function and jump to the next iteration. Doing so I am however missing some files in each run. I know that each of the URLs in the list exists and I need all of the data. </p>
  
  <p>My idea thus would have been to use <code>tryCatch()</code> to evaluate if the <code>getURL()</code> request yields and error. If so the loop jumps to the next iteration and the erroneous URL is appended at the end of the URL list over which the loop runs. My intuitive solution would look something like this:   </p>
  
  <pre><code>dwl = list()
  
  for (i in seq_along(urs)) {
  
  temp = tryCatch(getURL(url=urs[[i]]),error=function(e){e})
  
  if(inherits(temp,"OPERATION_TIMEDOUT")){ #check for timeout error
  urs[[length(urs)+1]]  = urs[[i]] #if there is one the erroneous url is appended at the end of the sequence 
  next} else {
  dwl[[i]] = temp #if there is no error the data is saved in the list
  }
  }
  </code></pre>
  
  <p>If it "would" work I would eventually be able to download all the URLs in the list. It however doesn't work, because as the help page for the <code>next</code> function states: "<em>seq in a for loop is evaluated at the start of the loop; changing it subsequently does not affect the loop</em>". Is there a workaround for this or a trick with which I could achieve my envisaged goal? I am grateful for every comment! </p>