Skip to content

Instantly share code, notes, and snippets.

@hadley
Created August 3, 2012 17:04
Show Gist options
  • Save hadley/3249592 to your computer and use it in GitHub Desktop.
Save hadley/3249592 to your computer and use it in GitHub Desktop.
# OR with httr
library(httr)
vertoccurrence <- function(key="r_B68F3", grp="fish", t = NULL, l = NULL,
c = NULL, d = NULL, q = NULL, p = NULL, m = NULL, cols = NULL, num = NULL,
set = NULL)
{
grp <- match.arg(grp, c("bird", "herp", "fish"))
url <- c(
bird = "http://ornis2.ornisnet.org/api/v1/occurrence/",
herp = "http://herpnet2.org/api/v1/occurrence/",
fish = "http://www.fishnet2.net/api/v1/occurrence/"
)[grp]
query <- as.list(c(api = key, t = t, l = l, c = c, d = d, q = q, p = p,
m = m, cols = cols, num = num, set = set))
# Server is misconfigured, so you must only use HTTP 1, not 1.1
resp <- GET(url = url, query = query, config(http.version = 1L))
# This will give informative error messages for http errors
stop_for_status(resp)
out <- read.csv(text = text_content(resp))
if (nrow(out) == 0){
out <- NULL
message("No records found")
}
out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment