如何快速检查R是否可以访问互联网?

时间:2015-07-07 13:10:30

标签: r

由于连接到互联网是容易出错的,因此能够快速检查R是否可以访问互联网并编写类似

的代码会很高兴。
if(r_can_connect_to_internet())
{
  download_some_data(my_url)
} else
{
  warning("R cannot connect to the internet")
  get_local_data()
}

您可以通过尝试连接到网站来检查R是否可以连接到互联网。类似的东西:

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")
}

但是,此时您还可以尝试连接到您想要的真实网址。

capabilities("http/ftp")给出了一个快速的,可能必要的连接条件,虽然它肯定是不够的,并且可能有其他的连接方式。

有没有更快捷的方法来检查R是否可以连接到互联网?

0 个答案:

没有答案