title: R errors in loop: file(con, "r") and open.connection(con, "rb")
body: <p>I am downloading a series of urls which are JSON files, into a list of list, to be analyzed later. </p>
  
  <pre><code>    baseurl &lt;- "http://zoeken.kvk.nl/Address.ashx?site=handelsregister&amp;partialfields=&amp;q=010"
      pages &lt;- list()
  
      for(i in 1:99999){
        if(i &lt; 10000){
          message("ignoring page ", i)
        }
        if(i &gt;= 10000){
          message("Retrieving page ", i)
          mydata &lt;- RJSONIO::fromJSON(paste0(baseurl,i), flatten=TRUE) 
  
          pages[[i+1]] &lt;- mydata$resultatenHR
  # adding adjustment 1
          options(timeout = 4000000)
  # adding adjustment 2
          if(i %% 100 == 0){Sys.sleep(2)}
          if(i %% 1000 == 0){Sys.sleep(10)}
        }
  
      }
  </code></pre>
  
  <p>However, at irrelugar moments, I get either the error code:</p>
  
  <pre><code>error in open.connection(con, "rb") : Recv failure: Connection was reset. 
  </code></pre>
  
  <p>or</p>
  
  <pre><code>Error in file(con, "r") : cannot open the connection
  </code></pre>
  
  <p>I first tried the abovementioned adjustment 1, and then adjustment 2, but the problem keeps on coming.
  If i try restarting the loop at the point of error, it works again untill the following irregular error. </p>
  
  <p>How can I build in that R restarts the loop at the point of error automatically?</p>
  
  <p>NB I have seen the other topics on error in open.connection, but I did not understand the answers given, or it was not applicable to my type of code i think...</p>
  
  <p>NB2: I have also tried using the <code>jsonlite</code> package instead of <code>RJSONIO</code>, but it gave the same errors at irregular moments.
  Thanks for your input.</p>