How to determine if the url object in the base package R returns '404 Not Found'?

Simply put:

if a

x <- read.csv(url) 

exists, then R will return the contents of this URL. A good example if you want to try might be " http://ichart.finance.yahoo.com/table.csv?s=IBM&a=00&b=1&c=2008&d=03&e=4&f=2014&g=d&ignore=.csv ". This specific URL, if assigned to the URL and works as described above, will download the data.frame file in x from the Yahoo website containing the last 5 years of IBM inventory data.

But how to say, in advance, if any given url gets you 404'd?

something like:

 is.404.or.not(url) 

or maybe

 status(connect.to(url)) 

Thanks!

+6
source share
1 answer

You can use the RCurl package:

 R> library(RCurl) Loading required package: bitops R> url.exists("http://google.com") [1] TRUE R> url.exists("http://csgillespie.org") [1] FALSE 

Alternatively, you can use the httr package

 R> library(httr) R> http_status(GET("http://google.com")) $category [1] "success" $message [1] "success: (200) OK" R> http_status(GET("http://csgillespie.org")) $category [1] "server error" $message [1] "server error: (503) Service Unavailable" 
+4
source share

All Articles