Package 'tradestatistics'

Title: Open Trade Statistics API Wrapper and Utility Program
Description: Access 'Open Trade Statistics' API from R to download international trade data.
Authors: Mauricio Vargas [aut, cre, cph] , Joshua Kunst [ctb] (contributed to different parts of the pre-release code), Alexey Kravchenko [ctb] (reviewed 2021 version of the API), Emma Mendelsohn [ctb] (updated the functions to take available years from the API instead of hardcoded values), Daniela de los Santos [ctb] (proposed improvements to default parameters), Emily Riederer [rev] (reviewed the package for rOpenSci, see https://github.com/ropensci/onboarding/issues/274), Mark Padgham [rev] (reviewed the package for rOpenSci, see https://github.com/ropensci/onboarding/issues/274), Amanda Dobbyn [rev] (reviewed a previous package that evolved into the current package for rOpenSci, see https://github.com/ropensci/onboarding/issues/217), Jorge Cimentada [rev] (reviewed a previous package that evolved into the current package for rOpenSci, see https://github.com/ropensci/onboarding/issues/217), UN Comtrade [dtc], The World Bank [dtc]
Maintainer: Mauricio Vargas <[email protected]>
License: Apache License (>= 2)
Version: 5.0.0
Built: 2024-08-23 13:06:16 UTC
Source: https://github.com/ropensci/tradestatistics

Help Index


OTS Commodities

Description

Official commodity names from the Harmonized System rev 2012 (HS12, six digits detail).

Usage

ots_commodities

Format

A data frame with 5,302 observations on the following 4 variables

commodity_code

HS six digits commodity code (e.g. 010110)

commodity_code_short

HS four digits commodity code (e.g. 0101)

commodity_fullname_english

HS six digits commodity name (e.g. 'Horses, asses, mules and hinnies; live, pure-bred breeding animals')

section_code

HS section code (e.g. '01')

Source

Open Trade Statistics


OTS Commodities Short

Description

Official commodity names from the Harmonized System rev 2012 (HS12, four digits detail).

Usage

ots_commodities_short

Format

A data frame with 1,225 observations on the following 2 variables

commodity_code

HS four digits commodity code (e.g. 0101)

commodity_fullname_english

HS four digits commodity names (e.g. 'Horses, asses, mules and hinnies; live')

Source

Open Trade Statistics


String matching of official commodity/section names and Harmonized System (HS) codes according to the United Nations nomenclature

Description

Takes a text string and searches within the package data for all matching commodity codes in the context of valid API commodity codes.

Usage

ots_commodity_code(commodity = NULL, section = NULL)

Arguments

commodity

A text string such as "Animals", "COPPER" or "fruits".

section

A text string such as "meat", "FISH" or "Dairy".

Value

A tibble with all possible matches (no uppercase distinction) showing the commodity name and commodity code

Examples

ots_commodity_code(commodity = "ANIMALS ")
ots_commodity_code(section = "  fish")
ots_commodity_code(commodity = "Milk", section = "Dairy")

OTS Countries

Description

Official country names, ISO-3 codes, continent and EU membership.

Usage

ots_countries

Format

A data frame with 275 observations on the following 5 variables

country_iso

ISO-3 code of the country (e.g. "deu" means Germany)

country_name_english

Country name (e.g. Germany)

country_fullname_english

Country name with indications (e.g. Germany as "Germany (former Federal Republic of Germany until 1990)")

continent_name_english

Continent where the country belongs to (e.g., Europe)

continent_id

Numeric id of the continent where the country belongs to (e.g., 5)

Source

Open Trade Statistics


OTS Countries Colors

Description

Unofficial colors to ease visualization for countries.

Usage

ots_countries_colors

Format

A data frame with 275 rows and 3 variables

country_iso

ISO code of the country (e.g. "chl" means Chile)

continent_id

Numeric id of the continent

country_color

Country hex color (e.g. '#D05555')

Source

Open Trade Statistics


String matching of official country names and ISO-3 codes according to the United Nations nomenclature

Description

Takes a text string and searches within the package data for a country code in the context of valid API country codes.

Usage

ots_country_code(countryname = NULL)

Arguments

countryname

A text string such as "Chile", "CHILE" or "CHL".

Value

A single character if there is a exact match (e.g. ots_country_code("Chile")) or a tibble in case of multiple matches (e.g. ots_country_code("Germany"))

Examples

ots_country_code("Chile ")
ots_country_code("america")
ots_country_code("UNITED  STATES")
ots_country_code(" united_")

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,
  reporters = "all",
  partners = "all",
  commodities = "all",
  sections = "all",
  table = "yr",
  max_attempts = 5,
  use_cache = FALSE,
  file = NULL
)

Arguments

years

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

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".

commodities

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

sections

HS section codes (e.g. "01"). 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.

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_commodities` to display the full table of commodities

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

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

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

## End(Not run)

GDP Deflator

Description

Year to year GDP deflator some of the countries in the OTS database. For countries not available in the World Bank database, rows labelled as "wld" are provided, which were computed as the weighted median for each year using the GDP of listed countries for each year expressed as constant dollars of the year 2010.

Usage

ots_gdp_deflator

Format

A data frame with 8,010 observations on the following 4 variables

year_from

Integer values in the range 1980-2020

year_to

Integer values in the range 1981-2021

country_iso

ISO code of the country (e.g. "chl" means Chile)

gdp_deflator

Numeric value expressed as one plus 1-year deflator

Source

Open Trade Statistics


Expresses tidy data from the API in dollars of a reference year

Description

Uses GDP deflator records from The World Bank to convert trade records and express them in dollars of the same year. The records are internally subsetted to World's values, because country specific levels would largely re-scale observations for reporters that reflect unstable macroeconomic policies.

Usage

ots_gdp_deflator_adjustment(trade_data = NULL, reference_year = NULL)

Arguments

trade_data

A tibble obtained by using ots_create_tidy_data. Default set to NULL.

reference_year

Year contained within the years specified in api.tradestatistics.io/year_range (e.g. 2010). Default set to NULL.

Examples

## Not run: 
# The next example can take more than 5 seconds to compute,
# so this is shown without evaluation according to CRAN rules

# Convert dollars of 2010 to dollars of 2000
d <- ots_create_tidy_data(years = 2010, reporters = "chl", partners = "chn")
ots_gdp_deflator_adjustment(trade_data = d, reference_year = 2000)

## End(Not run)

OTS Sections

Description

Official section names from the Harmonized System rev 2012 (HS12).

Usage

ots_sections

Format

A data frame with 22 rows and 2 variables

section_code

HS section code (e.g. '01')

section_fullname_english

HS section name (e.g. 'Live animals and animal products')

Source

Adapted from UN COMTRADE


OTS Sections Colors

Description

Unofficial colors to ease visualization for the sections in the Harmonized System rev 2012 (HS12).

Usage

ots_sections_colors

Format

A data frame with 22 rows and 2 variables

section_code

HS section code (e.g. '01')

section_color

HS section color (e.g. '#74c0e2')

Source

Open Trade Statistics


OTS Tables

Description

Existing API tables with both description and source.

Usage

ots_tables

Format

A data frame with 12 rows and 3 variables

table

Table name

description

Description of table contents

source

Source for the data (OTS tables are processed after UN Comtrade raw data)

Source

Open Trade Statistics