Reputation: 871
I would like to know how can I check if a html is available. If it is not, I would like to control the return to avoid stop the script by error. Ex:
arq <- readLines("www.pageerror.com.br")
print(arq)
Upvotes: 2
Views: 117
Reputation: 21532
?tryCatch
'Nuff said. <-- Except, apparently not, because the pageweaver demands more characters in an answer. So, "If a chicken and a half lays an egg and a half in a day and a half, how many eggs do nine chickens lay in nine days?"
OK, long enough.
Upvotes: 0
Reputation: 174853
An alternative is try()
- it is simpler to work with than trycatch()
but isn't as featureful. You might also need to suppress warnings as R will report that it can't resolve the address.
You want something like this in your script:
URL <- "http://www.pageerror.com.br"
arq <- try(suppressWarnings(readLines(con <- url(URL))), silent = TRUE)
close(con) ## close the connection
if(inherits(arq, "try-error")) {
writeLines(strwrap(paste("Page", URL, "is not available")))
} else {
print(arq)
}
The silent = TRUE
bit suppresses the reporting of errors (if you leave this at the default FALSE
, then R will report the error but not abort the script). We wrap the potentially error-raising function call in try(...., silent = TRUE)
, with suppressWarnings()
being used to suppress the warnings. Then we test the class of the returned object arq
and if it inherits from class "try-error"
we know the page could not be retrieved and issue a message indicating so. Otherwise we can print arq
.
Upvotes: 5