How to write trycatch in R

I want to write trycatch code to deal with error in downloading from the web.

url <- c(
    "http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html",
    "http://en.wikipedia.org/wiki/Xz")
y <- mapply(readLines, con=url)

These two statements run successfully. Below, I create a non-exist web address:

url <- c("xxxxx", "http://en.wikipedia.org/wiki/Xz")

url[1] does not exist. How does one write a trycatch loop (function) so that:

  1. When the URL is wrong, the output will be: “web URL is wrong, can’t get”.
  2. When the URL is wrong, the code does not stop, but continues to download until the end of the list of URLs?

5 Answers
5

Leave a Comment