R/Checklists.R

Defines functions Checklists TaxaLink Coordinates ChecklistProjects

Documented in ChecklistProjects Checklists Coordinates TaxaLink

#' Retrieves Checklists resources from the Symbiota2 server
#'
#' Functions that retrieve Checklist resources
#' from the server previously connected to.
#' Each function either retrieves an individual resource or a page of resources,
#' depending on the arguments provided.
#'
#' @template SymbiotaR2
#' @examples
#' \dontrun{
#' # Pulling a Coordinates resource (id = 1), from a (nonexistent) dummy portal
#' object <- Coordinates(id = 1, url = "http://dummy-portal.com/api/")
#' }
#' @rdname Checklists
#' @name Checklists
#' @export
ChecklistProjects <- function(id, page, url = NULL) {
  # Argument handling
  url <- .get.url(url)
  robject <- .api.scaffold(.check.api.entry("checklist/checklistprojects"), url, id, page)

  # id Download
  if (!missing(id)) {
    robject[sapply(robject, is.null)] <- NA
    return(robject)
  }

  # Page (specified or default) download
  if (!missing(page)) {
    robject <- robject$`hydra:member`
    for (i in seq_along(robject)) {
      robject[[i]][sapply(robject[[i]], is.null)] <- NA
    }
    output <- as.data.frame(do.call(rbind, robject))
    return(output)
  }
}

#' @export
#' @rdname Checklists
Coordinates <- function(id, page, url = NULL) {
  # Argument handling
  url <- .get.url(url)
  robject <- .api.scaffold(.check.api.entry("checklist/coordinates"), url, id, page)

  # id download
  if (!missing(id)) {
    robject <- c(robject$decimalLatitude, robject$decimalLongitude)
    return(robject)
  }

  # Page (specified or default) download
  if (!missing(page)) {
    robject <- robject$`hydra:member`
    robject <- lapply(robject, function(x) c(x$decimalLatitude, x$decimalLongitude))
    output <- as.data.frame(do.call(rbind, robject))
    names(output) <- c("latitude", "longitude")
    return(output)
  }
}

#' @export
#' @rdname Checklists
TaxaLink <- function(id, page, url = NULL) {
  # Argument handling
  url <- .get.url(url)
  robject <- .api.scaffold(.check.api.entry("checklist/taxalink"), url, id, page)

  # id Download
  if (!missing(id)) {
    robject[sapply(robject, is.null)] <- NA
    return(robject)
  }

  # Page (specified or default) download
  if (!missing(page)) {
    robject <- .page.to.dataframe(robject)
  }
  return(robject)
}

#' @export
#' @rdname Checklists
Checklists <- function(id, page, url = NULL) {
  # Argument handling
  url <- .get.url(url)
  robject <- .api.scaffold(.check.api.entry("checklists"), url, id, page)

  # id Download
  if (!missing(id)) {
    robject[sapply(robject, is.null)] <- NA
    return(robject)
  }

  # Page (specified or default) download
  if (!missing(page)) {
    robject <- robject$`hydra:member`
    for (i in seq_along(robject)) {
      robject[[i]][sapply(robject[[i]], is.null)] <- NA
    }
    output <- as.data.frame(do.call(rbind, robject))
    return(output)
  }
}
pearselab/SymbiotaR2 documentation built on Jan. 30, 2022, 5:11 a.m.