Learn R Programming

tradestatistics (version 1.1)

ots_create_tidy_data: 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 = 2018,
  reporters = "usa",
  partners = "all",
  products = "all",
  sections = "all",
  groups = "all",
  table = "yr",
  max_attempts = 5,
  use_cache = FALSE,
  file = NULL,
  use_localhost = FALSE
)

Arguments

years

Year contained within the years specified in api.tradestatistics.io/year_range (e.g. c(1980,1985), c(1980:1981) or 1980). Default set to 1962.

reporters

ISO code for reporter country (e.g. "chl", "Chile" or c("chl", "Peru")). Default set to "all".

partners

ISO code for partner country (e.g. "chl", "Chile" or c("chl", "Peru")). Default set to "all".

products

HS product codes (e.g. "0101", "01" or search matches for "apple") to filter products. Default set to "all".

sections

unofficial product sections (e.g. "01" or search matches for "animals") to filter sections Default set to "all".

groups

HS product groups (e.g. "01" or search matches for "animals") to filter groups. Default set to "all".

table

Character string to select the table to obtain the data. Default set to yr (Year - Reporter). 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.

use_localhost

Logical to determine if the base URL shall be localhost instead of api.tradestatistics.io. Default set to FALSE.

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

Run this code
# 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_products` to display the full table of products

# What does Chile export to China? (1980)
ots_create_tidy_data(years = 1980, reporters = "chl", partners = "chn")

# What can we say about Horses export in Chile and the World? (1980)
ots_create_tidy_data(years = 1980, products = "0101", table = "yc")
ots_create_tidy_data(years = 1980, reporters = "chl", products = "0101", table = "yrc")

# What can we say about the different types of apples exported by Chile? (1980)
ots_create_tidy_data(years = 1980, reporters = "chl", products = "apple", table = "yrc")
# }

Run the code above in your browser using DataLab