2

I use the getURL function from the Rcurl package in R to read content from a list of links.

When trying to fetch a broken link of the list I get the error "Error in function (type, msg, asError = TRUE) : Could not resolve host:" and the program stops running.

I use the Try command to try to avoid the program stopping but it doesn´t work.

try(getURL(URL, ssl.verifypeer = FALSE, useragent = "R")

Any hint on how can I avoid the program to stop running when trying to get a broken link?

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
Francisco Ghelfi
  • 872
  • 1
  • 11
  • 34
  • you really haven't provided enough info to help. `try` or `tryCatch` will catch the errors in the URL retrieval but you may not be handling the output of `try` very well (and causing other errors which do stop the program). you should paste more of the code/loop that you have that `try` running in. – hrbrmstr Oct 01 '15 at 21:15

1 Answers1

4

You need to be doing some type of error handling. I would argue tryCatch is actually better for your situation.

I'm assuming you are inside a loop over the links, then you can check the response from your try/tryCatch to see if an error was thrown, and if so just move to the following iteration in your loop using next.

status <- tryCatch(
    getURL(URL, ssl.verifypeer=FALSE, useragent="R"),
    error = function(e) e
)

if(inherits(status,  "error")) next
devmacrile
  • 470
  • 2
  • 7