-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix various urls - make coral fxns defunct fix #124
- Loading branch information
Showing
22 changed files
with
90 additions
and
327 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,45 @@ | ||
#' Search for coral data on coraltraits.org | ||
#' | ||
#' @name coral | ||
#' @param taxon A taxon id | ||
#' @param trait A trait id | ||
#' @param location A location id | ||
#' @param methodology A methodology id | ||
#' @param resource A resource id | ||
#' @param taxonomy logical; Include contextual data. Default: `FALSE` | ||
#' @param contextual logical; Include contextual data. Default: `TRUE` | ||
#' @param global logical; Include contextual data. Default: `FALSE` | ||
#' @param ... Curl options passed on to [crul::verb-GET] | ||
#' | ||
#' DEFUNCT: As far as we can tell the coraltraits.org API is down | ||
#' for good | ||
#' | ||
#' @name coral-defunct | ||
#' @keywords internal | ||
#' @param ... ignored | ||
#' @references https://coraltraits.org/ | ||
#' @author Scott Chamberlain \email{myrmecocystus@@gmail.com} | ||
#' @examples \dontrun{ | ||
#' # Get the species and their Ids | ||
#' head( coral_species() ) | ||
#' | ||
#' # Get data by taxon | ||
#' coral_taxa(8) | ||
#' | ||
#' # Get data by trait | ||
#' coral_traits(3) | ||
#' | ||
#' # Get data by methodology | ||
#' coral_methodologies(2) | ||
#' | ||
#' # Get data by location | ||
#' coral_locations(132) | ||
#' | ||
#' # Get data by resource | ||
#' coral_resources(9) | ||
#' | ||
#' # curl options | ||
#' coral_taxa(8, verbose=TRUE) | ||
#' } | ||
|
||
#' @export | ||
#' @rdname coral | ||
coral_taxa <- function(taxon, taxonomy = FALSE, contextual = TRUE, | ||
global = FALSE, ...) { | ||
args <- list(taxonomy = lsw(taxonomy), contextual = lsw(contextual), | ||
global = lsw(global)) | ||
coral_GET(coral_url("species", taxon), args, ...) | ||
#' @rdname coral-defunct | ||
coral_taxa <- function(...) { | ||
.Defunct(msg = "the coraltraits.org API is down for good") | ||
} | ||
|
||
#' @export | ||
#' @rdname coral | ||
coral_traits <- function(trait, taxonomy = FALSE, contextual = TRUE, | ||
global = FALSE, ...) { | ||
args <- list(taxonomy = lsw(taxonomy), contextual = lsw(contextual), | ||
global = lsw(global)) | ||
coral_GET(coral_url("traits", trait), args, ...) | ||
#' @rdname coral-defunct | ||
coral_traits <- function(...) { | ||
.Defunct(msg = "the coraltraits.org API is down for good") | ||
} | ||
|
||
#' @export | ||
#' @rdname coral | ||
coral_locations <- function(location, taxonomy = FALSE, contextual = TRUE, | ||
global = FALSE, ...) { | ||
args <- list(taxonomy = lsw(taxonomy), contextual = lsw(contextual), | ||
global = lsw(global)) | ||
coral_GET(coral_url("locations", location), args, ...) | ||
#' @rdname coral-defunct | ||
coral_locations <- function(...) { | ||
.Defunct(msg = "the coraltraits.org API is down for good") | ||
} | ||
|
||
#' @export | ||
#' @rdname coral | ||
coral_methodologies <- function(methodology, taxonomy = FALSE, | ||
contextual = TRUE, global = FALSE, ...) { | ||
args <- list(taxonomy = lsw(taxonomy), contextual = lsw(contextual), | ||
global = lsw(global)) | ||
coral_GET(coral_url("methodologies", methodology), args, ...) | ||
#' @rdname coral-defunct | ||
coral_methodologies <- function(...) { | ||
.Defunct(msg = "the coraltraits.org API is down for good") | ||
} | ||
|
||
#' @export | ||
#' @rdname coral | ||
coral_resources <- function(resource, taxonomy = FALSE, contextual = TRUE, | ||
global = FALSE, ...) { | ||
args <- list(taxonomy = lsw(taxonomy), contextual = lsw(contextual), | ||
global = lsw(global)) | ||
coral_GET(coral_url("resources", resource), args, ...) | ||
#' @rdname coral-defunct | ||
coral_resources <- function(...) { | ||
.Defunct(msg = "the coraltraits.org API is down for good") | ||
} | ||
|
||
#' @export | ||
#' @rdname coral | ||
#' @rdname coral-defunct | ||
coral_species <- function(...) { | ||
con <- crul::HttpClient$new("https://coraltraits.org/species", | ||
opts = list(...)) | ||
res <- con$get(query = list(all="true")) | ||
res$raise_for_status() | ||
html <- xml2::read_html(res$parse("UTF-8")) | ||
ids <- gsub("\n+|\t+|\\s+", "", | ||
xml2::xml_text(xml2::xml_find_all(html, | ||
'//li[@class="list-group-item"]//div[@style="color: lightgrey;"]'))) | ||
nms <- xml2::xml_text( | ||
xml2::xml_find_all(html, | ||
'//li[@class="list-group-item"]//div[@class="col-sm-5"]//a')) | ||
tibble::as_tibble(data.frame(name = nms, id = ids, stringsAsFactors = FALSE)) | ||
.Defunct(msg = "the coraltraits.org API is down for good") | ||
} | ||
|
||
coral_GET <- function(url, args, ...) { | ||
con <- crul::HttpClient$new(url, opts = list(...)) | ||
res <- con$get(query = args) | ||
res$raise_for_status() | ||
txt <- res$parse("UTF-8") | ||
tibble::as_tibble( | ||
utils::read.csv(text = txt, header = TRUE, stringsAsFactors = FALSE)) | ||
} | ||
|
||
coralbase <- function() 'https://coraltraits.org' | ||
coral_url <- function(var, id) paste0(file.path(coralbase(), var, id), ".csv") | ||
lsw <- function(x) if (x) "on" else "off" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.