Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if you have an internet connection in R

Tags:

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?

like image 230
Clair Crossupton Avatar asked Feb 22 '11 09:02

Clair Crossupton


People also ask

How do you connect to the internet on R?

Enter the necessary information on the Connect to R‑Studio dialog box and click the Connect button. Specify the DNS name or IP address of the host where R‑Studio is running. Specify the port set on the R‑Studio Connect to Remote Computer dialog box.

Does R require internet?

R clients using RStudio Package Manager do not need internet access, just access to RStudio Package Manager. RStudio Package Manager is designed to help organizations with restricted internet access in their R environment.

Why is my internet r?

Senior Member. That R, and the description on your homescreen, mean that you are in Roaming mode; you are not in your own carrier's network.


2 Answers

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

like image 102
Jeroen Ooms Avatar answered Sep 18 '22 05:09

Jeroen Ooms


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

if (is.character(getURL("www.google.com"))) {     out <- TRUE } else {     out <- FALSE } 
like image 25
Roman Luštrik Avatar answered Sep 20 '22 05:09

Roman Luštrik