1
votes

I would like to check whether a given directory exists in a remote server, given the URL pointing to that directory. For example:

url <- "http://plasmodb.org/common/downloads/release-24"

How can this be accomplished in R? I have considered using url.show, which downloads and shows the url if it exists but gives an error in the case of a non-existent directory. But I am not sure what would be the best approach, optimally without having to download the whole URL in the case of an existing directory.

1
have you tried ?file.exists ?tospig
@tospig yes. It doesn't work. file.exists(url) returns FALSE but it should be TRUE.ddiez

1 Answers

5
votes

This will be highly dependent on the server/resource in question since it has more to do with HTTP status codes than R capability. Provided a remote server is configured to respond properly to directory index requests you can use HEAD from httr for this:

library(httr)

status <- HEAD("http://plasmodb.org/common/downloads/release-24/")
status$status_code
## [1] 200

status <- HEAD("http://plasmodb.org/common/downloads/release-100/")
status$status_code
## [1] 404

Here's a nicely-formatted list of status codes http://httpstatus.es and here's a salient RFC http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html that you should also peruse other sections of. Finally, here's a Wikipedia link http://en.wikipedia.org/wiki/Webserver_directory_index discussing the "directory index". That shows you may also get a 403 vs 200 or 404 depending on the config (and it's really not limited to that depending on the web server).