Since connecting to the internet is failure prone, it would be nice to be able to quickly check if R can access the internet and write code like
if(r_can_connect_to_internet())
{
download_some_data(my_url)
} else
{
warning("R cannot connect to the internet")
get_local_data()
}
You could check that R can connect to the internet by trying to connect to a website. Something like:
r_can_connect_to_internet <- function(test_url = "http://cran.r-project.org")
{
res <- try(readLines(test_url, n = 1), silent = TRUE)
!inherits(res, "try-error")
}
However, at this point you might as well just try to connect to the real URL that you wanted.
capabilities("http/ftp")
gives a quick, possibly necessary condition to connect, though it certainly isn't sufficient, and there are possibly alternate ways of connecting.
Is there a quicker way to check if R can connect to the internet?