library("knitr")
library("rredlist")
hook_output <- knitr::knit_hooks$get("output")
knitr::knit_hooks$set(output = function(x, options) {
   lines <- options$output.lines
   if (is.null(lines)) {
     return(hook_output(x, options))  # pass to default hook
   }
   x <- unlist(strsplit(x, "\n"))
   more <- "..."
   if (length(lines)==1) {        # first n lines
     if (length(x) > lines) {
       # truncate the output, but add ....
       x <- c(head(x, lines), more)
     }
   } else {
     x <- c(if (abs(lines[1])>1) more else NULL,
            x[lines],
            if (length(x)>lines[abs(length(lines))]) more else NULL
           )
   }
   # paste these lines together
   x <- paste(c(x, ""), collapse = "\n")
   hook_output(x, options)
 })

knitr::opts_chunk$set(
  warning = FALSE,
  message = FALSE,
  collapse = TRUE,
  comment = "#>"
)

rredlist is an R client for the IUCN Red List API. The IUCN Red List is the world’s most comprehensive information source on the global extinction risk status of animal, fungus and plant species. This package provides access via R to the various data contained within this database which span range details, population size, habitat and ecology, use and/or trade, threats, and conservation actions.

Documentation for the IUCN Red List API is available at https://api.iucnredlist.org/api-docs. Note that use of the web API, and therefore this package, requires authentication.

What rredlist is not:

  1. redlistr is a different package for contributors to the IUCN Red List - not working with the IUCN Red List API.
  2. rredlist does not include support for the spatial API, described at https://apiv3.iucnredlist.org/spatial.

Installation

CRAN

install.packages("rredlist")

Development version

# From GitHub
remotes::install_github("ropensci/rredlist")

# From r-universe
install.packages("rredlist", repos = "https://ropensci.r-universe.dev/")

Authentication

IUCN requires you to get your own API key, an alphanumeric string that you need to send in every request. The rl_use_iucn() function in the package can walk you through getting your own key and storing it locally. Once you store this key, the functions in rredlist will always default to using this key.

rredlist::rl_use_iucn()

Keep this key private. You can pass the key in to each function via the key parameter, but it's better to store the key either as a environment variable (IUCN_REDLIST_KEY) or an R option (iucn_redlist_key) - we recommend using the former option.

Overview of available features

Example usage

Loading the package

library("rredlist")

Search for assessments for a particular species

rl_species("Gorilla", "gorilla")$assessments

Search for assessments that recommend particular conservation actions

Get a list of all conservation actions

rl_actions()

Return assessments with a particular conservation action

rl_actions("2_2", all = FALSE)$assessments

Advanced usage

For a more in-depth walkthrough of how to integrate rredlist into research workflows, check out the vignette.

High level vs. low level interfaces

High level interface

library("rredlist")

High level functions do the HTTP request and parse data to a list for ease of downstream use. The high level functions have no underscore on the end of the function name, e.g., rl_species(). Most of them return long lists containing lots of different information:

rl_species("Fratercula", "arctica")

By default, these high level functions will also parse the data to a data.frame, when possible:

rl_species("Fratercula", "arctica")$assessments

Likely a bit faster is to parse to a list only, and not take the extra data.frame parsing time:

rl_species("Fratercula", "arctica", parse = FALSE)$assessments

For even more speed, you can use the low level package interface (although see the benchmarking vignette for specifics).

Low level interface

The parsing to data.frame in the high level functions does take extra time. The low level functions only do the HTTP request, and give back JSON without doing any more parsing. The low level functions DO have an underscore on the end of the function name, e.g., rl_species_():

rl_species_("Fratercula", "arctica")

To consume this JSON, you can use jsonlite:

library("jsonlite")
fromJSON(rl_species_("Fratercula", "arctica"))

Usage best practice

Citing the IUCN Red List API

Use the function rl_citation():

rl_citation()

We'd also really appreciate it if you could cite your use of this package:

citation("rredlist")

Rate Limiting

From the IUCN folks: "Too many frequent calls, or too many calls per day might get your access blocked temporarily. If you're a heavy API user, the Red List Unit asked that you contact them, as there might be better options. They suggest a 2-second delay between your calls if you plan to make a lot of calls."

API Versioning

rredlist versions ≤ 0.7.1 tracked version 3 of the IUCN Red List API. rredlist versions ≥ 1.0.0 track version 4 of the IUCN Red List API. If you need to use version 3 of the API--and it is still functioning--you can install an old version of rredlist using the remotes package:

# From CRAN archive
remotes::install_version("rredlist", version = "0.7.1")

# From r-universe
remotes::install_version("rredlist", version = "0.7.1",
                         repos = "https://ropensci.r-universe.dev/")

Note, however, that you can no longer generate a new API key for version 3 of the API. See the discussion here for possible solutions if you need to use version 3 of the API.



ropenscilabs/rredlist documentation built on Feb. 7, 2025, 2:28 a.m.