Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

Commit

Permalink
fix #19 add nsr datasets, throw warnings when country, state/province…
Browse files Browse the repository at this point in the history
… name not in accepted set
  • Loading branch information
sckott committed Apr 30, 2018
1 parent b39d1ce commit eb852bf
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 10 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Expand Up @@ -8,7 +8,7 @@ Description: Get species origin data (whether species is native/invasive) from t
(<http://bien.nceas.ucsb.edu/bien/tools/nsr/nsr-ws/>), Integrated Taxonomic
Information Service (<http://www.itis.gov/>), and Global Register of
Introduced and Invasive Species (<http://www.griis.org/>).
Version: 0.2.5.9110
Version: 0.2.6.9110
Authors@R: c(
person("Scott", "Chamberlain", role = c("aut", "cre"), email = "myrmecocystus@gmail.com"),
person("Ignasi", "Bartomeus", role = "aut", email = "nacho.bartomeus@gmail.com")
Expand All @@ -17,6 +17,8 @@ License: MIT + file LICENSE
URL: https://github.com/ropensci/originr
BugReports: https://github.com/ropensci/originr/issues
Roxygen: list(markdown = TRUE)
LazyLoad: yes
LazyData: yes
Imports:
crul (>= 0.5.2),
jsonlite (>= 1.5),
Expand Down
3 changes: 3 additions & 0 deletions R/globals.R
@@ -0,0 +1,3 @@
if (getRversion() >= "2.15.1") {
utils::globalVariables(c('nsr_countries','nsr_pol_divisions'))
}
27 changes: 23 additions & 4 deletions R/nsr.R
Expand Up @@ -5,23 +5,42 @@
#' @param country (character) A country name. required.
#' @param stateprovince (character) A state or province name
#' @param countyparish (character) A county or parish name
#' @param ... curl options passed on to \code{\link[crul]{HttpClient}}
#' @references \url{http://bien.nceas.ucsb.edu/bien/tools/nsr/nsr-ws/}
#' @param ... curl options passed on to [crul::HttpClient]
#' @references http://bien.nceas.ucsb.edu/bien/tools/nsr/nsr-ws/
#' @details Currently, only one name is allowed per request. We loop internally
#' over a list of length > 1, but this will still be slow due to only 1
#' name per request.
#'
#' Note that this service can be quite slow.
#'
#' @section political names:
#'
#' - `nsr_countries`: is a vector of country names that we use to check
#' your country names
#' - `nsr_pol_divisions`: is a data.frame of country names and state/province
#' names that we used to check your parameter inputs - these are for checklists
#' that NSR has complete coverage for
#'
#' @examples \dontrun{
#' nsr("Pinus ponderosa", "United States")
#' nsr(c("Pinus ponderosa", "Poa annua"), "United States")
#' nsr("Pinus ponderosa", country = "United States")
#' nsr(c("Pinus ponderosa", "Poa annua"), country = "United States")
#' splist <- c("Pinus ponderosa", "Poa annua", "bromus tectorum", "Ailanthus altissima")
#' nsr(splist, country = "United States")
#' nsr(splist, country = "United States", stateprovince = "California")
#'
#' # curl options
#' nsr("Pinus ponderosa", "United States", verbose = TRUE)
#' }
nsr <- function(species, country, stateprovince = NULL, countyparish = NULL, ...) {
if (!country %in% nsr_countries) {
warning("country ", country, " not in the NSR list of countries")
}
if (!is.null(stateprovince)) {
if (!stateprovince %in% nsr_pol_divisions$state_province) {
warning("state/province ", stateprovince,
" not in the NSR list of state/provinces")
}
}
tmp <- lapply(species, function(z) {
args <- orc(list(format = 'json', species = z, country = country,
stateprovince = stateprovince, countyparish = countyparish))
Expand Down
21 changes: 21 additions & 0 deletions R/originr-package.R
Expand Up @@ -21,3 +21,24 @@
#' \item Integrated Taxonomic Information Service (http://www.itis.gov/)
#' }
NULL

#' Vector of country names for use with NSR
#'
#' @format A vector of countries of length 251
#' @name nsr_countries
#' @docType data
#' @keywords data
NULL

#' NSR political divisions
#'
#' @format A data frame with 73 rows and 2 variables:
#' \describe{
#' \item{country}{Country name}
#' \item{state_province}{State or province name}
#' }
#'
#' @name nsr_pol_divisions
#' @docType data
#' @keywords data
NULL
Binary file added data/nsr_countries.RData
Binary file not shown.
Binary file added data/nsr_pol_divisions.RData
Binary file not shown.
20 changes: 16 additions & 4 deletions man/nsr.Rd

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

11 changes: 11 additions & 0 deletions man/nsr_countries.Rd

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

15 changes: 15 additions & 0 deletions man/nsr_pol_divisions.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-nsr.R
Expand Up @@ -13,6 +13,6 @@ test_that("nsr works", {
test_that("fails well", {
skip_on_cran()

expect_error(nsr(), "argument \"species\" is missing")
expect_error(nsr(), "argument \"country\" is missing")
expect_equal(NROW(nsr(species = "adadfd", country = "United States")), 1)
})

0 comments on commit eb852bf

Please sign in to comment.