Clair Crossupton
Clair Crossupton

Reputation: 1382

How to determine if you have an internet connection in R

Sometimes I need to download data from the internet. On occasions this has failed either because the website is down or because my computer has lost its internet connection.

Question: Is there some function in R which will return TRUE/FALSE as to whether I am connected to the internet?

Upvotes: 49

Views: 21255

Answers (9)

Zeke
Zeke

Reputation: 669

All these answers use packages or code outside of base R. Here's how to do it with just base R:

# IANA's test website
is_online <- function(site="http://example.com/") {
  tryCatch({
    readLines(site,n=1)
    TRUE
  },
  warning = function(w) invokeRestart("muffleWarning"),
  error = function(e) FALSE)
}

Upvotes: 8

Jeroen Ooms
Jeroen Ooms

Reputation: 32988

The curl package has a function has_internet which tests by performing a nslookup:

curl::has_internet
## function(){
##    !is.null(nslookup("google.com", error = FALSE))
## }

Testing DNS is faster and may be more reliable than retrieving a URL because the latter might fail for unrelated reasons (e.g. firewall, server down, etc).

Upvotes: 40

Agriculturist
Agriculturist

Reputation: 561

This is a version of eyjo's answer which sacrifices accuracy for speed.

IPavailable <- function() {
  cmd <- switch(.Platform$OS.type, "windows" = "ipconfig", "ifconfig")
  any(grep("(\\d+(\\.|$)){4}", system(cmd, intern = TRUE)))
}

Upvotes: 0

TPArrow
TPArrow

Reputation: 1584

Do it with just two lines of code:

install.packages('pingr')
pingr::is_online()

Upvotes: 7

Eric
Eric

Reputation: 3473

Bioconductor's Biobase package has a function for testing internet connection.

Biobase::testBioCConnection()

Below is a heavily modified version of this function for testing ability to read lines from a URL.

can_internet <- function(url = "http://www.google.com") {

    # test the http capabilities of the current R build
    if (!capabilities(what = "http/ftp")) return(FALSE)

    # test connection by trying to read first line of url
    test <- try(suppressWarnings(readLines(url, n = 1)), silent = TRUE)

    # return FALSE if test inherits 'try-error' class
    !inherits(test, "try-error")
}

can_internet()

Upvotes: 5

user1554736
user1554736

Reputation:

As the function above from eyjo

havingIP <- function() {
 if (.Platform$OS.type == "windows") {
   ipmessage <- system("ipconfig", intern = TRUE)
 } else {
   ipmessage <- system("ifconfig", intern = TRUE)
 }
 validIP <- "((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.]){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
 any(grep(validIP, ipmessage))
}

also returns true for localhost ip "127.0.0.1" it needs to be removed to prevent false positives. For example as shown below:

 havingIP <- function() {
 if (.Platform$OS.type == "windows") {
   ipmessage <- system("ipconfig", intern = TRUE)
 } else {
   ipmessage <- system("ifconfig", intern = TRUE)
 }
 validIP <- "((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.]){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
 any(grep(validIP, ipmessage[-grep("127.0.0.1", ipmessage)]))
}

But even better would be a solution that prevents localhosts by modifying the regex of validIP.

Upvotes: 1

Tony Breyal
Tony Breyal

Reputation: 5378

Just another one to add to the pot, inspired by @romans answer, this works only on Windows I'd assume, not sure about other platforms:

canPingSite <- function(test.site) {
    !as.logical(system(paste("ping", test.site)))
}

Which we test as follows:

> t1 <- canPingSite("www.yahoo.com")
[...]

> t2 <- canPingSite(";lkjsdflakjdlfhasdfhsad;fs;adjfsdlk")
[...]

> t1; t2
[1] TRUE
[1] FALSE

Upvotes: 12

Roman Luštrik
Roman Luštrik

Reputation: 70653

A dirty work around would be using RCurl::getURL function.

if (is.character(getURL("www.google.com"))) {
    out <- TRUE
} else {
    out <- FALSE
}

Upvotes: 29

eyjo
eyjo

Reputation: 1210

Here is an attempt at parsing the output from ipconfig/ifconfig, as suggested by Spacedman.

havingIP <- function() {
  if (.Platform$OS.type == "windows") {
    ipmessage <- system("ipconfig", intern = TRUE)
  } else {
    ipmessage <- system("ifconfig", intern = TRUE)
  }
  validIP <- "((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.]){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
  any(grep(validIP, ipmessage))
}

With a simple TRUE/FALSE output

> havingIP()
[1] TRUE

Upvotes: 20

Related Questions