| ct_trade | R Documentation |
Download merchandise trade data from the UN Comtrade database. Returns bilateral trade flows between reporter and partner countries, optionally filtered by commodity code and trade flow direction.
ct_trade(
reporter,
partner = "0",
commodity = "TOTAL",
flow = c("X", "M"),
year = NULL,
frequency = "A",
classification = "HS",
cache = TRUE
)
reporter |
Character. Reporter country ISO3 code (e.g., |
partner |
Character. Partner country ISO3 code, or |
commodity |
Character. HS commodity code(s). |
flow |
Character. Trade flow: |
year |
Integer. Year(s) to query (1962-present). Can be a vector. Maximum 12-year span per query on the free tier. Default: most recent available year. |
frequency |
Character. |
classification |
Character. Commodity classification system. Default
|
cache |
Logical. Cache results locally for 24 hours. Default |
A data.frame with columns:
Reporter country code
Reporter country name
Partner country code
Partner country name
Trade flow code (X, M, RX, RM)
Trade flow description
Commodity code
Commodity description
Reference year
Reference period (year or year-month)
Trade value in US dollars
Net weight in kilograms
Quantity in supplementary units
Supplementary quantity unit
op <- options(comtrade.cache_dir = tempdir())
# The Comtrade API is rate-limited and can return a non-JSON response
# under load. tryCatch so the examples degrade gracefully if that
# happens during the CRAN check.
# UK total exports to the world, 2023
uk <- tryCatch(ct_trade("GBR", year = 2023, flow = "X"),
error = function(e) NULL)
if (!is.null(uk)) head(uk)
# US imports of crude petroleum from Saudi Arabia
tryCatch(ct_trade("USA", partner = "SAU", commodity = "2709", flow = "M",
year = 2020:2023),
error = function(e) NULL)
# Australia's top-level trade with China
tryCatch(ct_trade("AUS", partner = "CHN", year = 2023),
error = function(e) NULL)
options(op)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.