ots_create_tidy_data: Downloads and processes the data from the API to return a...

View source: R/ots_create_tidy_data.R

ots_create_tidy_dataR Documentation

Downloads and processes the data from the API to return a human-readable tibble

Description

Accesses api.tradestatistics.io and performs different API calls to transform and return tidy data.

Usage

ots_create_tidy_data(
  years = 2020,
  importers = "all",
  exporters = "all",
  sectors = "all",
  industries = "all",
  table = "itpde_imp",
  max_attempts = 5,
  use_cache = FALSE,
  file = NULL
)

Arguments

years

Year contained within the years specified in api.tradestatistics.io/year_range (e.g., 2002 or seq(2002, 2010, 2)). Default set to 2019.

importers

Importers (e.g. "chl", "Chile" or c("chl", "Peru")). Default set to "all".

exporters

Exporters (e.g. "chl", "Chile" or c("chl", "Peru")). Default set to "all".

sectors

Sectors (e.g. "Agriculture", 1 or search matches for "agri") to filter sectors. Default set to "all".

industries

Industries (e.g. "Cereal products", 5 or search matches for "cereal") to filter industries. Default set to "all".

table

Character string to select the table to obtain the data. Default set to itpde_imp (aggregate trade by importer-year). Run ots_tables in case of doubt.

max_attempts

How many times to try to download data in case the API or the internet connection fails when obtaining data. Default set to 5.

use_cache

Logical to save and load from cache. If TRUE, the results will be cached in memory if file is NULL or on disk if 'file' is not NULL. Default set to FALSE.

file

Optional character with the full file path to save the data. Default set to NULL.

Value

A tibble that describes bilateral trade metrics (imports, exports, trade balance and relevant metrics such as exports growth w/r to last year) between a reporter and partner country.

Examples

## Not run: 
# The next examples can take more than 5 seconds to compute,
# so these are just shown without evaluation according to CRAN rules

# Run `ots_countries` to display the full table of countries
# Run `ots_sectors` to display the full table of sectors

# Agricultural trade (sector)
ots_create_tidy_data(years = 2020, sectors = "Agriculture",
 table = "itpde_sec")

# Cereal products trade (industry-importer)
ots_create_tidy_data(years = 2020, importers = "chl",
 industries = "Cereal products", table = "itpde_imp_ind")

# Aggregate trade (bilateral)
ots_create_tidy_data(years = 2020, importers = "chl",
 exporters = c("arg", "bra"), table = "itpde_imp_exp")

## End(Not run)

tradestatistics documentation built on July 21, 2026, 5:09 p.m.