Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atualização de API #102

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ BugReports: https://github.com/LimaRAF/plantR/issues
LazyData: true
LazyDataCompression: xz
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.2
RoxygenNote: 7.2.3
VignetteBuilder: knitr
Imports:
countrycode,
Expand Down
53 changes: 30 additions & 23 deletions R/rspeciesLink.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#' Gets occurrence data from speciesLink
#'
#' This function access version beta 0.1 of the
#' [speciesLink API]{https://api.splink.org.br/} and returns occurrence data of
#' This function access version 1.0 of the
#' [speciesLink API](https://specieslink.net/ws/1.0/) and returns occurrence data of
#' species.
#'
#' @param key Character. Your personal specieslink API key.
#' Generate one at \url{specieslink.net/aut/profile/apikeys}
#' @param dir Path to directory where the file will be saved. Default is to
#' create a "results/" directory
#' @param filename Name of the output file
Expand Down Expand Up @@ -32,7 +34,7 @@
#' @param CoordinatesQuality Any character in "Good" or "Bad" to select specific
#' type of coordinates
#' @param Scope Group to be required. If NULL searches all groups. Any in
#' "plants", "animals", "microrganisms" or "fossils"
#' "p", "a", "m", "f" or "b" (plants, animals, microorganisms, fossil and broad(?))
#' @param Synonyms If species names should be checked for synonyms in a specific
#' dictionary. Set to "species2000" for search in Catálogo da Vida species2000,
#' "flora2020" for Flora do Brasil 2020, "MycoBank" for MycoBank, "AlgaeBase"
Expand All @@ -42,7 +44,7 @@
#' @param Images If select only records with images. Default is NULL.
#' It accepts: "Yes", "Live", "Polen", "Wood"
#' @param RedList Logic. If TRUE only species in the IUCN Red List are returned
#' @param MaxRecords Numeric. Maximum number of records to be required
#' @param limit Numeric. Maximum number of records to be required. Default is 200, maximum is 5000.
#' @param file.format Character. The file extension to be used for saving ('csv'
#' or 'rds'). Default to 'csv'.
#' @param compress Logical. Should the file be compressed? Default to FALSE.
Expand All @@ -57,19 +59,19 @@
#' time. So to download records from larger lists of species, you will probably
#' need to make the queries in a loop (see Examples).
#'
#' @author Sara Mortara
#' @author Sara Mortara and João Vieira
#'
#' @examples
#'\dontrun{
#' # Example for a single species, saving into a file called "ex01"
#' ex01 <-
#' rspeciesLink(filename = "ex01",
#' species = c("Eugenia platyphylla"),
#' Scope = "plants")
#' Scope = "p")
#' # Use lapply or similar for more than 50 species
#' # Making a request for multiple species
#'sp_list <- lapply(sp, function(x) rspeciesLink(species = x,
#' Scope = "plants",
#' Scope = "p",
#' basisOfRecord = "PreservedSpecimen",
#' Synonyms = "flora2020"))
#'# Adding species names to each element of the list
Expand All @@ -81,7 +83,8 @@
#' @import data.table
#' @importFrom jsonlite fromJSON
#' @export
rspeciesLink <- function(dir = "results/",
rspeciesLink <- function(key = NULL,
dir = "results/",
filename = "output",
save = FALSE,
basisOfRecord = NULL,
Expand All @@ -93,24 +96,28 @@ rspeciesLink <- function(dir = "results/",
county = NULL,
Coordinates = NULL, # Yes | No | Original | Automatic | Blocked
CoordinatesQuality = NULL, #Good | Bad
Scope = NULL, # plants, animals, microrganisms,fossils
Scope = NULL, # a (animais) p (plantas) b (abrangente) m (microorganismos) f (fósseis)
Synonyms = "no synomyms", #species2000 | flora2020 | MycoBank | AlgaeBase | DSMZ | Moure no synonyms
Typus = FALSE,
Images = NULL,
RedList = FALSE,
MaxRecords = NULL, # n > 0 all records
limit = NULL,
file.format = "csv",
compress = FALSE
) { # Yes | No | Live | Polen | Wood
# speciesLink url
my_url <- "https://api.splink.org.br/records/"
my_url <- "https://specieslink.net/ws/1.0/search?"

# helper function
url_query <- function(vector, name) {
char <- paste(paste0(vector, "/"), collapse = "")
url <- paste0(name, "/", char)
char <- paste(paste0(vector, "&"), collapse = "")
url <- paste0(name, "=", char)
return(url)
}
# api key
if (is.null(key)) {
stop("Please provide an API key! Get one at specieslink.net/aut/profile/apikeys")
}
# basis of record
if (is.null(basisOfRecord)) {
my_url
Expand All @@ -134,7 +141,7 @@ rspeciesLink <- function(dir = "results/",
if (is.character(species)) {
if (length(species) > 50)
stop("Please make request of no more than 50 species at a time!")
species <- gsub(" ", "%20", species)
species <- gsub(" ", "+", species)
sp <- url_query(species, "scientificName")
my_url <- paste0(my_url, sp)
} else {
Expand Down Expand Up @@ -167,7 +174,7 @@ rspeciesLink <- function(dir = "results/",
my_url
} else {
if (is.character(country)) {
country <- gsub(" ", "%20", country)
country <- gsub(" ", "+", country)
ct <- url_query(country, "country")
my_url <- paste0(my_url, ct)
}
Expand All @@ -177,7 +184,7 @@ rspeciesLink <- function(dir = "results/",
my_url
} else {
if (is.character(stateProvince)) {
stateProvince <- gsub(" ", "%20", stateProvince)
stateProvince <- gsub(" ", "+", stateProvince)
st <- url_query(stateProvince, "stateProvince")
my_url <- paste0(my_url, st)
}
Expand All @@ -187,7 +194,7 @@ rspeciesLink <- function(dir = "results/",
my_url
} else {
if (is.character(county)) {
county <- gsub(" ", "%20", county)
county <- gsub(" ", "+", county)
co <- url_query(county, "county")
my_url <- paste0(my_url, co)
}
Expand All @@ -214,7 +221,7 @@ rspeciesLink <- function(dir = "results/",
if (is.null(Scope)) {
my_url
} else {
if (Scope %in% c("plants", "animals", "microrganisms", "fossils")) {
if (Scope %in% c("p", "a", "m", "f", "b")) {
sc <- url_query(Scope, "Scope")
my_url <- paste0(my_url, sc)
}
Expand Down Expand Up @@ -255,22 +262,22 @@ rspeciesLink <- function(dir = "results/",
my_url <- paste0(my_url, "RedList/Yes/")
}
# MaxRecords
if (is.null(MaxRecords)) {
if (is.null(limit)) {
my_url
} else {
if (is.numeric(MaxRecords)) {
mr <- url_query(MaxRecords, "MaxRecords")
if (is.numeric(limit)) {
mr <- url_query(limit, "limit")
my_url <- paste0(my_url, mr)
}
}
# making request
my_url <- paste0(my_url, "Format/JSON/") #Model/DwC is already default
my_url <- paste0(my_url,"apikey=", key) #Model/DwC is already default
message("Making request to speciesLink...")
#r <- httr::GET(my_url)
#message("Extracting content ...")
#rr <- httr::content(r, as="parse") # text content
# requesting JSON format
df <- jsonlite::fromJSON(my_url)$result
df <- jsonlite::fromJSON(my_url)$features$properties
#rrr <- readr::read_tsv(rr, locale = readr::locale(encoding = "UTF-8"))

if (save) {
Expand Down
20 changes: 12 additions & 8 deletions man/rspeciesLink.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.