[R] Loop for readLines(URL[i]) fails when URL returns Error 404
Noia Raindrops
noia.raindrops at gmail.com
Wed Aug 22 21:39:53 CEST 2012
Hello,
For example:
url <- c("http://www.example.com", "http://www.example5.com")
res <- vector("list", length(url))
for (i in 1:length(url)) res[[i]] <- try(readLines(url[i]), silent = TRUE)
res[[2]]
## [1] "Error in file(con, \"r\") : cannot open the connection\n"
## attr(,"class")
## [1] "try-error"
## attr(,"condition")
## <simpleError in file(con, "r"): cannot open the connection>
# or
for (i in 1:length(url)) res[[i]] <- tryCatch(readLines(url[i]), error = function (e) conditionMessage(e))
res[[2]]
## [1] "cannot open the connection"
--
Noia Raindrops
noia.raindrops at gmail.com
More information about the R-help
mailing list