R/children_s.R

Defines functions children_s.wormsid children_s.default children_s

Documented in children_s children_s.default children_s.wormsid

#' Retrieve immediate children taxa for a given taxon name or ID.
#'
#' @export
#' @param x character; taxons to query.
#' @param db character; database to query. Only \code{worms} for now.
#' @param ... Further args passed on to \code{worms_children}.
#' @return A named list of data.frames with the children names of every supplied taxa.
#' You get an NA if there was no match in the database.
#' @details In \pkg{taxize} the equivalent function \code{taxize} accepts names
#' and taxonomic identifiers. This function only accepts names for now
#' @examples \dontrun{
#' children_s("Aatolana", db = 'worms')
#' }

children_s <- function(...) {
  UseMethod("children_s")
}

#' @export
#' @rdname children_s
children_s.default <- function(x, db = NULL, ...) {
  if (is.null(db))
    stop("Must specify db value!")

  if (db == 'worms') {
    id <- get_wormsid(x, ...)
    out <- children_s(id, ...)
  }
  names(out) <- x
  return(out)
}

#' @export
#' @rdname children_s
children_s.wormsid <- function(x, db = NULL, ...) {
  fun <- function(y, ...) {
    # return NA if NA is supplied
    if (is.na(y)) {
      NA
    } else {
      out <- worms_children(ids = y, ...)
      if (NROW(out[[1]]) == 0) {
        out[[1]]
      } else {
        out[[1]][, c("AphiaID","scientificname","rank","status","valid_AphiaID",
                     "valid_name","kingdom","phylum","class","order","family","genus")]
      }
    }
  }
  out <- lapply(x, fun, ...)
  class(out) <- 'children_s'
  attr(out, 'db') <- 'worms'
  return(out)
}
ropensci/taxizesoap documentation built on May 18, 2022, 7:33 p.m.