How to determine if you have an internet connection in R

后端 未结 9 962
南旧
南旧 2020-11-30 01:16

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.

相关标签:
9条回答
  • 2020-11-30 01:31

    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).

    0 讨论(0)
  • 2020-11-30 01:33

    Do it with just two lines of code:

    install.packages('pingr')
    pingr::is_online()
    
    0 讨论(0)
  • 2020-11-30 01:34

    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.

    0 讨论(0)
  • 2020-11-30 01:36

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

    if (is.character(getURL("www.google.com"))) {
        out <- TRUE
    } else {
        out <- FALSE
    }
    
    0 讨论(0)
  • 2020-11-30 01:37

    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
    
    0 讨论(0)
  • 2020-11-30 01:38

    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()
    
    0 讨论(0)
提交回复
热议问题