Skip to content

Commit 9ea5339

Browse files
committed
moved to using markdown docs fix #176
replaced all possible uses of httr to use crul instead (some deps still use httr) fix #174 fixed all as.* fxns to pass on curl options to the unerlying http client fix #177 tidied most lined to 80 line width
1 parent 8047a56 commit 9ea5339

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+874
-638
lines changed

DESCRIPTION

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ Description: A programmatic interface to many species occurrence data sources,
88
System ('OBIS'), and Atlas of Living Australia ('ALA'). Includes
99
functionality for retrieving species occurrence data, and combining
1010
those data.
11-
Version: 0.6.2.9140
11+
Version: 0.6.6.9100
1212
License: MIT + file LICENSE
1313
Authors@R: c(
14-
person("Scott", "Chamberlain", role = c("aut", "cre"), email = "myrmecocystus@gmail.com"),
14+
person("Scott", "Chamberlain", role = c("aut", "cre"),
15+
email = "myrmecocystus@gmail.com"),
1516
person("Karthik", "Ram", role = "ctb"),
1617
person("Ted", "Hart", role = "ctb")
1718
)
1819
URL: https://github.com/ropensci/spocc
1920
BugReports: https://github.com/ropensci/spocc/issues
2021
LazyData: true
2122
VignetteBuilder: knitr
23+
Roxygen: list(markdown = TRUE)
2224
Imports:
2325
utils,
2426
rgbif (>= 0.9.5),
@@ -27,7 +29,7 @@ Imports:
2729
rvertnet (>= 0.5.0),
2830
ridigbio (>= 0.3.3),
2931
lubridate (>= 1.5.0),
30-
httr (>= 1.1.0),
32+
crul (>= 0.3.4),
3133
whisker (>= 0.3-2),
3234
jsonlite (>= 1.1),
3335
data.table (>= 1.9.6),

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
YEAR: 2016
1+
YEAR: 2017
22
COPYRIGHT HOLDER: Scott Chamberlain

NAMESPACE

-4
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ export(wkt2bbox)
8989
export(wkt_vis)
9090
importFrom(data.table,rbindlist)
9191
importFrom(data.table,setDF)
92-
importFrom(httr,GET)
93-
importFrom(httr,content)
94-
importFrom(httr,stop_for_status)
95-
importFrom(httr,warn_for_status)
9692
importFrom(jsonlite,toJSON)
9793
importFrom(lubridate,as_date)
9894
importFrom(lubridate,now)

R/ala_helpers.R

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ala_base <- function() "http://biocache.ala.org.au/ws/occurrences"
1+
ala_base <- function() "http://biocache.ala.org.au"
22

33
ala_search <- function(taxon = NULL, limit = 500, offset = 0, fq = NULL,
44
facet = "off", facets = NULL, sort = NULL, dir = NULL, flimit = NULL,
@@ -12,22 +12,29 @@ ala_search <- function(taxon = NULL, limit = 500, offset = 0, fq = NULL,
1212
foffset = foffset, fprefix = fprefix, lat = lat, lon = lon,
1313
radius = radius, wkt = geometry
1414
))
15-
res <- httr::GET(file.path(ala_base(), "search"), query = args, ...)
16-
httr::stop_for_status(res)
17-
jsonlite::fromJSON(httr::content(res, "text", encoding = "UTF-8"), flatten = TRUE)
15+
cli <- crul::HttpClient$new(
16+
url = ala_base(),
17+
opts = list(...)
18+
)
19+
out <- cli$get(path = "ws/occurrences/search", query = args)
20+
out$raise_for_status()
21+
jsonlite::fromJSON(out$parse("UTF-8"), flatten = TRUE)
1822
}
1923

2024
ala_occ_id <- function(id, ...) {
2125
if (length(id) > 1) {
22-
lapply(file.path(ala_base(), id), ala_GET, ...)
26+
lapply(id, ala_GET, ...)
2327
} else {
24-
ala_GET(file.path(ala_base(), id), ...)
28+
ala_GET(id, ...)
2529
}
2630
}
2731

28-
ala_GET <- function(url, args = list(), ...) {
29-
res <- httr::GET(url, args, ...)
30-
httr::stop_for_status(res)
31-
jsonlite::fromJSON(httr::content(res, "text", encoding = "UTF-8"),
32-
flatten = TRUE)
32+
ala_GET <- function(id, args = list(), ...) {
33+
cli <- crul::HttpClient$new(
34+
url = ala_base(),
35+
opts = list(...)
36+
)
37+
out <- cli$get(path = file.path("ws/occurrence", id), query = args)
38+
out$raise_for_status()
39+
jsonlite::fromJSON(out$parse("UTF-8"), flatten = TRUE)
3340
}

R/antweb_helpers.R

+38-18
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,57 @@ aw_data2 <- function(genus = NULL, species = NULL, scientific_name = NULL,
44
limit = NULL, offset = NULL, quiet = FALSE, callopts = list()) {
55

66
# Check for minimum arguments to run a query
7-
main_args <- sc(as.list(c(scientific_name, genus, country, type, habitat, bbox)))
7+
main_args <- sc(as.list(c(scientific_name, genus, country, type,
8+
habitat, bbox)))
89
date_args <- sc(as.list(c(min_date, max_date)))
910
elev_args <- sc(as.list(c(min_elevation, max_elevation)))
1011
arg_lengths <- c(length(main_args), length(date_args), length(elev_args))
1112

1213
stopifnot(any(arg_lengths) > 0)
1314
decimal_latitude <- decimal_longitude <- NA
14-
if(!is.null(scientific_name)) {
15+
if (!is.null(scientific_name)) {
1516
genus <- strsplit(scientific_name, " ")[[1]][1]
1617
species <- strsplit(scientific_name, " ")[[1]][2]
1718
}
1819

19-
base_url <- "http://www.antweb.org/api/v2/"
20+
base_url <- "http://www.antweb.org"
2021
original_limit <- limit
2122
args <- sc(as.list(c(genus = genus, species = species, bbox = bbox,
22-
min_elevation = min_elevation, max_elevation = max_elevation,
23+
min_elevation = min_elevation,
24+
max_elevation = max_elevation,
2325
habitat = habitat, country = country, type = type,
2426
min_date = min_date, max_date = max_date, limit = 1,
2527
offset = offset, georeferenced = georeferenced)))
26-
results <- GET(base_url, query = args, callopts)
27-
warn_for_status(results)
28-
data <- jsonlite::fromJSON(content(results, "text", encoding = "UTF-8"), FALSE)
28+
cli <- crul::HttpClient$new(url = base_url, opts = callopts)
29+
out <- cli$get(path = "api/v2", query = args)
30+
out$raise_for_status()
31+
data <- jsonlite::fromJSON(out$parse("UTF-8"), FALSE)
2932
data <- sc(data) # Remove NULL
3033

31-
if(data$count > 1000 & is.null(limit)) {
34+
if (data$count > 1000 && is.null(limit)) {
3235
args$limit <- 1000
33-
results <- GET(base_url, query = args)
34-
if(!quiet) message(sprintf("Query contains %s results. First 1000 retrieved. Use the offset argument to retrieve more \n", data$count))
36+
cli <- crul::HttpClient$new(url = base_url, opts = callopts)
37+
out <- cli$get(path = "api/v2", query = args)
38+
out$raise_for_status()
39+
results <- out$parse("UTF-8")
40+
41+
if (!quiet) {
42+
message(
43+
gsub('\n', '', sprintf(
44+
"Query contains %s results.
45+
First 1000 retrieved. Use the offset argument to retrieve more \n",
46+
data$count))
47+
)
48+
}
3549
} else {
3650
args$limit <- original_limit
37-
results <- GET(base_url, query = args)
51+
cli <- crul::HttpClient$new(url = base_url, opts = callopts)
52+
out <- cli$get(path = "api/v2", query = args)
53+
out$raise_for_status()
54+
results <- out$parse("UTF-8")
3855
}
3956

40-
data <- jsonlite::fromJSON(content(results, "text", encoding = "UTF-8"), FALSE)
57+
data <- jsonlite::fromJSON(results, FALSE)
4158
data <- sc(data)
4259

4360
if (identical(data$specimens$empty_set, "No records found.")) {
@@ -46,18 +63,21 @@ aw_data2 <- function(genus = NULL, species = NULL, scientific_name = NULL,
4663
if (!quiet) message(sprintf("%s results available for query.", data$count))
4764
data_df <- lapply(data$specimens, function(x){
4865
x$images <- NULL
49-
# In a future fix, I should coerce the image data back to a df and add it here.
50-
df <- data.frame(t(unlist(x)), stringsAsFactors=FALSE)
51-
df
66+
# In a future fix, I should coerce the image data back to a df and
67+
# add it here.
68+
data.frame(t(unlist(x)), stringsAsFactors = FALSE)
5269
})
5370
final_df <- rbindlist(data_df, use.names = TRUE, fill = TRUE)
5471
setDF(final_df)
55-
names(final_df)[grep("geojson.coord1", names(final_df))] <- "decimal_latitude"
56-
names(final_df)[grep("geojson.coord2", names(final_df))] <- "decimal_longitude"
72+
names(final_df)[grep("geojson.coord1", names(final_df))] <-
73+
"decimal_latitude"
74+
names(final_df)[grep("geojson.coord2", names(final_df))] <-
75+
"decimal_longitude"
5776
# There seem to be extra field when searching for just a genus
5877
final_df$decimalLatitude <- NULL
5978
final_df$decimalLongitude <- NULL
60-
final_df$minimumElevationInMeters <- as.numeric(final_df$minimumElevationInMeters)
79+
final_df$minimumElevationInMeters <-
80+
as.numeric(final_df$minimumElevationInMeters)
6181
list(count = data$count, call = args, data = final_df)
6282
}
6383
}

R/as.ala.R

+20-19
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,59 @@
33
#' @export
44
#'
55
#' @param x Various inputs, including the output from a call to
6-
#' \code{\link{occ}} (class occdat), \code{\link{occ2df}} (class data.frame),
6+
#' [occ()] (class occdat), [occ2df()] (class data.frame),
77
#' or a list, numeric, alakey, or occkey.
8+
#' @param ... curl options; named parameters passed on to [crul::HttpClient()]
89
#' @return One or more in a list of both class alakey and occkey
910
#' @examples \dontrun{
10-
#' # spnames <- c('Barnardius zonarius', 'Grus rubicunda', 'Cracticus tibicen')
11-
#' # out <- occ(query=spnames, from='ala', limit=2)
12-
#' # (res <- occ2df(out))
13-
#' # (tt <- as.ala(out))
14-
#' # as.ala(x = res$key[1])
11+
#' spnames <- c('Barnardius zonarius', 'Grus rubicunda', 'Cracticus tibicen')
12+
#' out <- occ(query=spnames, from='ala', limit=2)
13+
#' (res <- occ2df(out))
14+
#' (tt <- as.ala(out))
15+
#' as.ala(x = res$key[1])
1516
#' }
16-
as.ala <- function(x) {
17+
as.ala <- function(x, ...) {
1718
UseMethod("as.ala")
1819
}
1920

2021
#' @export
21-
as.ala.alakey <- function(x) x
22+
as.ala.alakey <- function(x, ...) x
2223

2324
#' @export
24-
as.ala.occkey <- function(x) x
25+
as.ala.occkey <- function(x, ...) x
2526

2627
#' @export
27-
as.ala.occdat <- function(x) {
28+
as.ala.occdat <- function(x, ...) {
2829
x <- occ2df(x)
29-
make_ala_df(x)
30+
make_ala_df(x, ...)
3031
}
3132

3233
#' @export
33-
as.ala.data.frame <- function(x) make_ala_df(x)
34+
as.ala.data.frame <- function(x, ...) make_ala_df(x, ...)
3435

3536
#' @export
36-
as.ala.character <- function(x) make_ala(x)
37+
as.ala.character <- function(x, ...) make_ala(x, ...)
3738

3839
#' @export
39-
as.ala.list <- function(x){
40+
as.ala.list <- function(x, ...) {
4041
lapply(x, function(z) {
4142
if (inherits(z, "alakey")) {
42-
as.ala(z)
43+
as.ala(z, ...)
4344
} else {
44-
make_ala(z)
45+
make_ala(z, ...)
4546
}
4647
})
4748
}
4849

49-
make_ala_df <- function(x){
50+
make_ala_df <- function(x, ...) {
5051
tmp <- x[x$prov %in% "ala", ]
5152
if (NROW(tmp) == 0) {
5253
stop("no data from ALA found", call. = FALSE)
5354
} else {
54-
stats::setNames(lapply(tmp$key, make_ala), tmp$key)
55+
stats::setNames(lapply(tmp$key, make_ala, ...), tmp$key)
5556
}
5657
}
5758

58-
make_ala <- function(y, ...){
59+
make_ala <- function(y, ...) {
5960
structure(ala_occ_id(id = y, ...), class = c("alakey", "occkey"))
6061
}

R/as.antweb.R

+20-18
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
#'
33
#' @export
44
#'
5-
#' @param x Various inputs, including the output from a call to \code{\link{occ}}
6-
#' (class occdat), \code{\link{occ2df}} (class data.frame), or a list, numeric,
5+
#' @param x Various inputs, including the output from a call to [occ()]
6+
#' (class occdat), [occ2df()] (class data.frame), or a list, numeric,
77
#' character, or antwebkey, or occkey.
8+
#' @param ... curl options; named parameters passed on to [crul::HttpClient()]
89
#' @return One or more in a list of both class antwebkey and occkey
910
#' @examples \dontrun{
1011
#' spp <- c("linepithema humile", "acanthognathus")
@@ -18,52 +19,53 @@
1819
#' as.antweb(uu[[1]])
1920
#' as.antweb(tt[1:2])
2021
#' }
21-
as.antweb <- function(x) UseMethod("as.antweb")
22+
as.antweb <- function(x, ...) UseMethod("as.antweb")
2223

2324
#' @export
24-
as.antweb.antwebkey <- function(x) x
25+
as.antweb.antwebkey <- function(x, ...) x
2526

2627
#' @export
27-
as.antweb.occkey <- function(x) x
28+
as.antweb.occkey <- function(x, ...) x
2829

2930
#' @export
30-
as.antweb.occdat <- function(x) {
31+
as.antweb.occdat <- function(x, ...) {
3132
x <- occ2df(x)
32-
make_antweb_df(x)
33+
make_antweb_df(x, ...)
3334
}
3435

3536
#' @export
36-
as.antweb.data.frame <- function(x) make_antweb_df(x)
37+
as.antweb.data.frame <- function(x, ...) make_antweb_df(x, ...)
3738

3839
#' @export
39-
as.antweb.character <- function(x) make_antweb(x)
40+
as.antweb.character <- function(x, ...) make_antweb(x, ...)
4041

4142
#' @export
42-
as.antweb.list <- function(x){
43+
as.antweb.list <- function(x, ...) {
4344
lapply(x, function(z) {
4445
if (inherits(z, "antwebkey")) {
45-
as.antweb(z)
46+
as.antweb(z, ...)
4647
} else {
47-
make_antweb(z)
48+
make_antweb(z, ...)
4849
}
4950
})
5051
}
5152

52-
make_antweb_df <- function(x){
53+
make_antweb_df <- function(x, ...) {
5354
tmp <- x[ x$prov %in% "antweb" , ]
5455
if (NROW(tmp) == 0) {
5556
stop("no data from antweb found", call. = FALSE)
5657
} else {
57-
stats::setNames(lapply(tmp$key, make_antweb), tmp$key)
58+
stats::setNames(lapply(tmp$key, make_antweb, ...), tmp$key)
5859
}
5960
}
6061

6162
make_antweb <- function(y, ...){
6263
structure(get_antweb(y, ...), class = c("antwebkey", "occkey"))
6364
}
6465

65-
get_antweb <- function(z) {
66-
res <- GET(sprintf('http://antweb.org/api/v2/?occurrenceId=CAS:ANTWEB:%s', z))
67-
stop_for_status(res)
68-
jsonlite::fromJSON(content(res, "text", encoding = "UTF-8"))
66+
get_antweb <- function(z, ...) {
67+
cli <- crul::HttpClient$new(url = "http://antweb.org", opts = list(...))
68+
out <- cli$get(path = paste0('api/v2/?occurrenceId=CAS:ANTWEB:', z))
69+
out$raise_for_status()
70+
jsonlite::fromJSON(out$parse("UTF-8"))
6971
}

0 commit comments

Comments
 (0)