R/FinBIF.R

Defines functions tc_FinBIF

#' @param d string vector of species names
#' @param write TRUE/FALSE if to write the function output
tc_FinBIF <- function(
  d = NULL,
  write = FALSE
) {
  if (is.null(d) | length(d) == 0) {
    stop("No input species")
  }
  #FinBIF need token
  token <- read.csv("~/Documents/finbif.txt", header = FALSE)
  Sys.setenv(FINBIF_ACCESS_TOKEN = token$V1)
  ans <- lapply(d, function(x) {
    if (is.na(x)) {
      data.frame(search_name = x, scientific_name = NA)
    } else {
      ans <- finbif::finbif_taxa(x, type = "exact")
      if (length(ans$content) > 0) {
        data.frame(search_name = x,
                   scientific_name = ans$content[[1]]$scientificName)
      } else {
        data.frame(search_name = x,
                   scientific_name = NA)
      }
    }
  })
  ans <- do.call("rbind", ans)
  if (write) {
    write.csv(ans, "results/FinBIF.csv")
  }
  return(ans)
}
emilio-berti/taxclean documentation built on May 30, 2021, 5:19 p.m.