R/as.obis.R

Defines functions make_obis make_obis_df as.obis.list as.obis.character as.obis.data.frame as.obis.occdat as.obis.occkey as.obis.obiskey as.obis

Documented in as.obis

#' Coerce occurrence keys to obis id objects
#' 
#' @export
#' 
#' @family coercion
#' 
#' @param x Various inputs, including the output from a call to 
#' [occ()] (class occdat), [occ2df()] (class data.frame), 
#' or a list, numeric, obiskey, or occkey.
#' @param ... curl options; named parameters passed on to [crul::HttpClient()]
#' @return One or more in a list of both class obiskey and occkey
#' @examples \dontrun{
#' spnames <- c('Mola mola', 'Loligo vulgaris', 'Stomias boa')
#' out <- occ(query=spnames, from='obis', limit=2)
#' (res <- occ2df(out))
#' (tt <- as.obis(out))
#' (uu <- as.obis(res))
#' as.obis(x = res$key[1])
#' as.obis(as.list(res$key[1:2]))
#' as.obis(tt[[1]])
#' as.obis(uu[[1]])
#' as.obis(tt[1:2])
#'
#' library("data.table")
#' rbindlist(lapply(tt, "[[", "results"),
#'   use.names = TRUE, fill = TRUE)
#' }
as.obis <- function(x, ...) UseMethod("as.obis")

#' @export
as.obis.obiskey <- function(x, ...) x

#' @export
as.obis.occkey <- function(x, ...) x

#' @export
as.obis.occdat <- function(x, ...) {
  x <- occ2df(x)
  make_obis_df(x, ...)
}

#' @export
as.obis.data.frame <- function(x, ...) make_obis_df(x, ...)

#' @export
as.obis.character <- function(x, ...) make_obis(x, ...)

#' @export
as.obis.list <- function(x, ...) {
  lapply(x, function(z) {
    if (inherits(z, "obiskey")) {
      as.obis(z, ...)
    } else {
      make_obis(z, ...)
    }
  })
}

make_obis_df <- function(x, ...) {
  tmp <- x[x$prov %in% "obis", ]
  if (NROW(tmp) == 0) {
    stop("no data from OBIS found", call. = FALSE)
  } else {
    stats::setNames(lapply(tmp$key, make_obis, ...), tmp$key)
  }
}

make_obis <- function(y, ...) {
  structure(obis_occ_id(id = y, ...), class = c("obiskey", "occkey"))
}

Try the spocc package in your browser

Any scripts or data that you put into this service are public.

spocc documentation built on March 31, 2023, 9:59 p.m.