-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
941 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#' @md | ||
#' @title get_taxa | ||
#' @description a sites object with the requested taxa. | ||
#' @param x string taxa name or names | ||
#' @returns A Neotoma2 sites object with datasets with the requested taxa. | ||
#' | ||
#' @export | ||
get_taxon <- function(x = NA, ...) { | ||
if (!missing(x)) { | ||
UseMethod("get_taxa", x) | ||
} else { | ||
UseMethod("get_taxa", NA) | ||
} | ||
} | ||
|
||
parse_taxon <- function(result) { # nolint | ||
|
||
fix_null <- function(x) { | ||
for (i in seq_len(length(x))) { | ||
if (is.null(x[[i]])) { | ||
x[[i]] <- NA | ||
} else { | ||
if (is(x[[i]], "list")) { | ||
x[[i]] <- fix_null(x[[i]]) | ||
} | ||
} | ||
} | ||
return(x) | ||
} | ||
|
||
data <- result$data %>% | ||
fix_null() | ||
|
||
# With a large dataset this seems to take some time, but it's not too bad. | ||
newTaxon <- map(data, function(x) { | ||
|
||
new_taxon <- new("taxon", | ||
taxonid = x$taxonid, | ||
taxoncode = x$taxoncode, | ||
taxonname = x$taxonname, | ||
author = x$author, | ||
ecolgroup = x$ecolgroup, | ||
highertaxonid = x$highertaxonid, | ||
status = x$status, | ||
taxagroupid = x$taxagroupid, | ||
publicationid = x$publicationid, | ||
publication = x$publication) | ||
|
||
print(new_taxon) | ||
}) | ||
|
||
return(new_taxon) | ||
|
||
} | ||
|
||
|
||
#' @title Get Taxa Default | ||
#' @param x Use a taxon ID to extract site information | ||
#' @param ... accepted arguments, see details for more information. | ||
#' @importFrom utils URLencode | ||
#' @returns `sites` object containing the requested `taxa` | ||
#' @export | ||
get_taxon.default <- function(x, ...) { | ||
oo <- options(scipen = 9999999) | ||
on.exit(options(oo)) | ||
cl <- as.list(match.call()) | ||
|
||
cl[[1]] <- NULL | ||
|
||
cl <- lapply(cl, eval, envir = parent.frame()) | ||
|
||
all_data <- ifelse(is.null(cl$all_data), FALSE, TRUE) | ||
error_check <- check_args(cl) # nolint | ||
|
||
if (error_check[[2]]$flag == 1) { | ||
stop(paste0(unlist(error_check[[2]]$message), collapse = "\n ")) | ||
} else { | ||
cl <- error_check[[1]] | ||
} | ||
|
||
base_url <- paste0("data/taxa") | ||
result <- parseURL(base_url, ...) %>% | ||
cleanNULL() | ||
|
||
if (is.null(result$data[1][[1]]) || is.null(result[1][[1]])) { | ||
return(NULL) | ||
|
||
} else { | ||
output <- parse_taxa(result) | ||
return(output) | ||
} | ||
} | ||
|
||
#' @title Get Taxa Numeric | ||
#' @param x Use a taxon ID to extract sites information | ||
#' @param ... Additional parameters to get_taxa | ||
#' @returns `sites` object with requested `taxa` | ||
#' @examples \donttest{ | ||
#' allds <- get_datasets(1:3) | ||
#' } | ||
#' @export | ||
get_taxon.numeric <- function(x, ...) { | ||
use_na <- function(x, type) { | ||
if (is.na(x)) { | ||
return(switch(type, | ||
"char" = NA_character_, | ||
"int" = NA_integer_)) | ||
} else { | ||
return(x) | ||
} | ||
} | ||
|
||
if (length(x) > 0) { | ||
taxa_id <- paste0(x, collapse = ",") | ||
} | ||
|
||
base_url <- paste0("data/taxa/", taxa_id) | ||
result <- neotoma2::parseURL(base_url, ...) | ||
result_length <- length(result[2]$data) | ||
|
||
if (result_length > 0) { | ||
output <- parse_taxa(result) | ||
return(output) | ||
} else { | ||
return(NULL) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,124 @@ | ||
#' @md | ||
#' @title get_taxon | ||
#' @description a sites object with the requested taxa. | ||
#' @param x string taxa name or names | ||
#' @returns A Neotoma2 sites object with datasets with the requested taxa. | ||
#' | ||
#' @export | ||
get_taxon <- function(x = NA, ...) { | ||
if (!missing(x)) { | ||
UseMethod("get_taxon", x) | ||
} else { | ||
UseMethod("get_taxon", NA) | ||
} | ||
} | ||
|
||
parse_taxon <- function(result) { # nolint | ||
|
||
fix_null <- function(x) { | ||
for (i in seq_len(length(x))) { | ||
if (is.null(x[[i]])) { | ||
x[[i]] <- NA | ||
} else { | ||
if (is(x[[i]], "list")) { | ||
x[[i]] <- fix_null(x[[i]]) | ||
} | ||
} | ||
} | ||
return(x) | ||
} | ||
|
||
data <- result$data %>% | ||
fix_null() | ||
|
||
newTaxa <- map(data, function(x) { | ||
new_taxon <- new("taxon", | ||
taxonid = use_na(testNull(x$taxonid, NA), "int"), | ||
taxoncode = use_na(testNull(x$taxoncode, NA), "char"), | ||
taxonname = use_na(testNull(x$taxonname, NA), "char"), | ||
author = use_na(testNull(x$author, NA), "char"), | ||
ecolgroup = use_na(testNull(x$ecolgroup, NA), "char"), | ||
highertaxonid = use_na(testNull(x$highertaxonid, NA), "int"), | ||
status = use_na(testNull(x$status, NA), "char"), | ||
taxagroupid = use_na(testNull(x$taxagroupid, NA), "int"), | ||
publicationid = use_na(testNull(x$publicationid, NA), "int"), | ||
publication = use_na(testNull(x$publication, NA), "char")) | ||
}) | ||
|
||
taxa <- new("taxa", taxa = newTaxa) | ||
|
||
return(taxa) | ||
} | ||
|
||
|
||
#' @title Get Taxa Default | ||
#' @param x Use a taxon ID to extract site information | ||
#' @param ... accepted arguments, see details for more information. | ||
#' @importFrom utils URLencode | ||
#' @returns `sites` object containing the requested `taxa` | ||
#' @export | ||
get_taxon.default <- function(x, ...) { | ||
oo <- options(scipen = 9999999) | ||
on.exit(options(oo)) | ||
cl <- as.list(match.call()) | ||
|
||
cl[[1]] <- NULL | ||
|
||
cl <- lapply(cl, eval, envir = parent.frame()) | ||
|
||
all_data <- ifelse(is.null(cl$all_data), FALSE, TRUE) | ||
error_check <- check_args(cl) # nolint | ||
|
||
if (error_check[[2]]$flag == 1) { | ||
stop(paste0(unlist(error_check[[2]]$message), collapse = "\n ")) | ||
} else { | ||
cl <- error_check[[1]] | ||
} | ||
|
||
base_url <- paste0("data/taxa") | ||
result <- parseURL(base_url, ...) %>% | ||
cleanNULL() | ||
|
||
if (is.null(result$data[1][[1]]) || is.null(result[1][[1]])) { | ||
return(NULL) | ||
|
||
} else { | ||
output <- parse_taxon(result) | ||
return(output) | ||
} | ||
} | ||
|
||
#' @title Get Taxa Numeric | ||
#' @param x Use a taxon ID to extract sites information | ||
#' @param ... Additional parameters to get_taxa | ||
#' @returns `sites` object with requested `taxa` | ||
#' @examples \donttest{ | ||
#' allds <- get_datasets(1:3) | ||
#' } | ||
#' @export | ||
get_taxon.numeric <- function(x, ...) { | ||
use_na <- function(x, type) { | ||
if (is.na(x)) { | ||
return(switch(type, | ||
"char" = NA_character_, | ||
"int" = NA_integer_)) | ||
} else { | ||
return(x) | ||
} | ||
} | ||
|
||
if (length(x) > 0) { | ||
taxa_id <- paste0(x, collapse = ",") | ||
} | ||
|
||
base_url <- paste0("data/taxa/", taxa_id) | ||
result <- neotoma2::parseURL(base_url, ...) | ||
result_length <- length(result[2]$data) | ||
|
||
if (result_length > 0) { | ||
output <- parse_taxon(result) | ||
return(output) | ||
} else { | ||
return(NULL) | ||
} | ||
} |
Oops, something went wrong.