citesdb

A high-performance database of shipment-level CITES trade data

10
4
R

output:
github_document:
html_preview: FALSE

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

library(dplyr)

citesdb

unclass(desc::desc_get_authors(here::here("DESCRIPTION"))) %>%
  purrr::keep(~"aut" %in% .$role) %>%
  purrr::map_chr(~paste(.$given, .$family)) %>%
  glue::glue_collapse(sep = ", ", last = " and ") %>%
  paste0("Authors: _", ., "_") %>%
  cat()

License: MIT{data-external=“1”}
rOpensci_Badge
Published in the Journal of Open Source Software{data-external=“1”}
DOI{data-external=“1”}
CircleCI{data-external=“1”}
codecov{data-external=“1”}
Project Status: Active – The project has reached a stable, usable state and is being actively developed.

citesdb is an R package to conveniently analyze the full CITES shipment-level wildlife trade database, available at https://trade.cites.org/. This data consists of over 40 years and 20 million records of reported shipments of wildlife and wildlife products subject to oversight under the Convention on International Trade in Endangered Species of Wild Fauna and Flora. The source data are maintained by the UN Environment World Conservation Monitoring Centre.

Installation

Install the citesdb package with this command:

devtools::install_github("ropensci/citesdb")

Note that since citesdb installs a source dependency from GitHub, you
will need package build tools.

options(width = 120)
knitr::opts_chunk$set(cache = TRUE)
citesdb::cites_disconnect()

Usage

Getting the data

When you first load the package, you will see a message like this:

library(citesdb)
#> Local CITES database empty or corrupt. Download with cites_db_download()

Not to worry, just do as it says and run cites_db_download(). This will fetch the most recent database from online, an approximately 158 MB download. It will expand to over 1 GB in the local database. During the download and database building, up to 3.5 GB of disk space may be used temporarily.

Using the database

Once you fetch the data, you can connect to the database with the cites_db() command. The cites_shipments() command loads a remote tibble that is backed by the database but is not loaded into R. You can use this command to analyze CITES data without ever loading it into memory, gathering your results with the dplyr function collect(). For example:

if (!citesdb::cites_status()) citesdb::cites_db_download()
library(citesdb)
library(dplyr)

start <- Sys.time()

cites_shipments() %>%
  group_by(Year) %>%
  summarize(n_records = n()) %>%
  arrange(desc(Year)) %>%
  collect()

stop <- Sys.time()

(Note that running collect() on all of cites_shipments() will load a >3 GB data frame into memory!)

The back-end database, duckdb, is very fast and powerful, making analyses on such large data quite snappy using normal desktops and laptops. Here’s the timing of the above query, which processes over 20 million records:

stop - start

If you are using a recent version of RStudio interactively, loading the CITES package also brings up a browsable pane in the “Connections” tab that lets you explore and preview the database, as well as interact with it directly via SQL commands.

If you don’t need any of the bells and whistles of this package, you can download the raw data as a single compressed TSV file from the releases page, or as a .zip file of many CSV files from the original source at https://trade.cites.org/.

Metadata

The package database also contains tables of field metadata, codes used, and CITES countries. This information comes from “A guide to using the CITES Trade Database”, on the CITES website. Convenience functions cites_metadata(), cites_codes(), and cites_parties() access this information:

head(cites_metadata())

head(cites_codes())

head(cites_parties())
citesdb::cites_disconnect()

More information on the release of shipment-level CITES data can be found in the ?guidance help file.

Related work

The rcites package provides access to the Speciesplus/CITES Checklist API, which includes metadata about species and their protected status through time.

Citation

If you use citesdb in a publication, please cite both the package and source data:

print(citation("citesdb"), style = "textVersion")

Contributing

Have feedback or want to contribute? Great! Please take a look at the contributing guidelines before filing an issue or pull request.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Created by EcoHealth Alliance{data-external=“1”}