MyNixOS website logo
Description

Interface with the United Nations Comtrade API.

Interface with and extract data from the United Nations 'Comtrade' API <https://comtradeplus.un.org/>. 'Comtrade' provides country level shipping data for a variety of commodities, these functions allow for easy API query and data returned as a tidy data frame.

comtradr

CRAN_Status_Badge Status at rOpenSci Software PeerReview R-CMD-check Codecov testcoverage

Interface with and extract data from the United

Nations Comtrade API https://comtradeplus.un.org/. Comtrade provides country level shipping data for a variety of commodities, these functions allow for easy API query and data returned as a tidy data frame. More info can be found here. Full API documentation can be found here.

Please report issues, comments, or feature requests. We are very much looking for feedback on the usability of the new functions.

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

For information on citation of this package, use citation("comtradr")

Installation 🛠️

You can install the package with:

install.packages("comtradr")

To install the dev version from github, use:

# install.packages("devtools")
devtools::install_github("ropensci/comtradr@dev")

Usage

Authentication 🔐

Do not be discouraged by the complicated access to the token - you can do it! 💪

As stated above, you need an API token, see the FAQ of Comtrade for details on how to obtain it:

➡️ https://uncomtrade.org/docs/api-subscription-keys/

You need to follow the detailed explanations, which include screenshots, in the Wiki of Comtrade to the letter. ☝️ I am not writing them out here, because they might be updated regularly. However, once you are signed up, select the comtrade - v1 product, which is the free API.

Storing the API key

If you are in an interactive session, you can call the following function to save your API token to the environment file for the current session.

library(comtradr)

set_primary_comtrade_key()

If you are not in an interactive session, you can register the token once in your session using the following base-r function.

Sys.setenv('COMTRADE_PRIMARY' = 'xxxxxxxxxxxxxxxxx')

If you would like to set the comtrade key permanently, we recommend editing the project .Renviron file, where you need to add a line with COMTRADE_PRIMARY = xxxx-your-key-xxxx.

ℹ️ Do not forget the line break after the last entry. This is the easiest by taking advantage of the great usethis package.

usethis::edit_r_environ(scope = 'project')

Example 1 ⛴️

Now we can get to actually request some data. Let us query the total trade between China and Germany and Argentina, as reported by China.


# Country names passed to the API query function must be spelled in ISO3 format. 
# For details see: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3 

# You can request a maximum interval of twelve years from the API
example1 <- comtradr::ct_get_data(
  reporter = 'CHN',
  partner = c('ARG', 'DEU'),
  start_date = 2010,
  end_date = 2012
)

# Inspect the return data
str(example1)

Example 2 ⛴️

Return all exports related to Wine from Argentina to all other countries, for years 2007 through 2011.

library(comtradr)

# Fetch all shrimp related commodity codes from the Comtrade commodities DB.
# This vector of codes will get passed to the API query.
wine_codes <- ct_commodity_lookup("wine", return_code = TRUE, return_char = TRUE)

# API query.
example2 <- ct_get_data(
  reporter =  "ARG",
  flow_direction = "export",
  partner = "all_countries",
  start_date = 2007,
  end_date = 2011,
  commodity_code = wine_codes
)

# Inspect the output
str(example2)

Bulk Download Example 📦

To download bulk files, use the function ct_get_bulk. Usage is documented in the package vignettes, see here for an example.

Attention, this downloads large files (often more than one Gigabyte in size) and requires a premium key.

hs0_all <- comtradr::ct_get_bulk(
  reporter = c("DEU"), # only some examples here,
  commodity_classification = 'H0',
  frequency = 'A',
  verbose = T,
  start_date = 2020, # only one year here
  end_date = 2020)

Data availability

See here for an overview of available commodity classifications.

Comtradr has relaunched recently 🚧

The Comtrade API has been undergoing extensive updates. At this point the legacy API has been taken offline (see here).

In order to accommodate the new syntax, as well as adding the new functionality, the comtradr package has been undergoing an extensive re-write. Additionally, it is not possible anymore to query the API without an API token. There still is a free version, but unlike before you need to be a registered user to obtain the token. See the FAQ for details on how to obtain the new access tokens.

ropensci_footer

Metadata

Version

1.0.1

License

Unknown

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows