How to determine if you have an internet connection in R

后端 未结 9 1122
南旧
南旧 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).

提交回复
热议问题