[API] comtradR: Application

An API for detailed global trade data.

Thierry Warin https://warin.ca/aboutme.html (HEC Montréal and CIRANO (Canada))https://www.hec.ca/en/profs/thierry.warin.html
02-17-2020

The comtradR package provides an R interface for the United Nations Comtrade. comtradR is a fantastic CRAN package that provides country level shipping data for a variety of commodities.

Learn how to use the comtradR package by following this example below.

Extract the data


# Load the required library
library(comtradr)

Search for specific countries


# Search 

ct_country_lookup("Ecuador", "reporter")

[1] "Ecuador"

ct_country_lookup(c("USA", "China", "Canada", "South Africa", "France"), "partner")

[1] "Canada"               "China"               
[3] "China, Hong Kong SAR" "China, Macao SAR"    
[5] "France"               "South Africa"        
[7] "USA"                  "USA (before 1981)"   

Search for specific commodities


# Gives all the codes related to "banana"
ct_commodity_lookup("banana")

$banana
[1] "0803 - Bananas, including plantains; fresh or dried"                   
[2] "080300 - Fruit, edible; bananas, (including plantains), fresh or dried"
[3] "080390 - Fruit, edible; bananas, other than plantains, fresh or dried" 

Search for specific data


# Search data on all monthly banana exports for 2018
BananaExport <- ct_search(reporters = "Ecuador", 
               partners = c("All"), 
               trade_direction = "exports",
               commod_codes = "0803",
               start_date = 2018, 
               end_date = 2018)

str(BananaExport)

'data.frame':   75 obs. of  35 variables:
 $ classification        : chr  "H5" "H5" "H5" "H5" ...
 $ year                  : int  2018 2018 2018 2018 2018 2018 2018 2018 2018 2018 ...
 $ period                : int  2018 2018 2018 2018 2018 2018 2018 2018 2018 2018 ...
 $ period_desc           : chr  "2018" "2018" "2018" "2018" ...
 $ aggregate_level       : int  4 4 4 4 4 4 4 4 4 4 ...
 $ is_leaf_code          : int  0 0 0 0 0 0 0 0 0 0 ...
 $ trade_flow_code       : int  2 2 2 2 2 2 2 2 2 2 ...
 $ trade_flow            : chr  "Export" "Export" "Export" "Export" ...
 $ reporter_code         : int  218 218 218 218 218 218 218 218 218 218 ...
 $ reporter              : chr  "Ecuador" "Ecuador" "Ecuador" "Ecuador" ...
 $ reporter_iso          : chr  "ECU" "ECU" "ECU" "ECU" ...
 $ partner_code          : int  0 8 12 31 32 36 44 48 51 56 ...
 $ partner               : chr  "World" "Albania" "Algeria" "Azerbaijan" ...
 $ partner_iso           : chr  "WLD" "ALB" "DZA" "AZE" ...
 $ second_partner_code   : logi  NA NA NA NA NA NA ...
 $ second_partner        : chr  NA NA NA NA ...
 $ second_partner_iso    : chr  NA NA NA NA ...
 $ customs_proc_code     : chr  NA NA NA NA ...
 $ customs               : chr  NA NA NA NA ...
 $ mode_of_transport_code: chr  NA NA NA NA ...
 $ mode_of_transport     : chr  NA NA NA NA ...
 $ commodity_code        : chr  "0803" "0803" "0803" "0803" ...
 $ commodity             : chr  "Bananas, including plantains; fresh or dried" "Bananas, including plantains; fresh or dried" "Bananas, including plantains; fresh or dried" "Bananas, including plantains; fresh or dried" ...
 $ qty_unit_code         : int  8 8 8 8 8 8 8 8 8 8 ...
 $ qty_unit              : chr  "Weight in kilograms" "Weight in kilograms" "Weight in kilograms" "Weight in kilograms" ...
 $ alt_qty_unit_code     : logi  NA NA NA NA NA NA ...
 $ alt_qty_unit          : chr  NA NA NA NA ...
 $ qty                   : num  7.00e+09 3.61e+07 6.68e+07 1.57e+07 2.69e+08 ...
 $ alt_qty               : logi  NA NA NA NA NA NA ...
 $ netweight_kg          : num  6.78e+09 3.21e+07 5.99e+07 1.45e+07 2.63e+08 ...
 $ gross_weight_kg       : logi  NA NA NA NA NA NA ...
 $ trade_value_usd       : num  3.22e+09 1.43e+07 3.01e+07 6.71e+06 1.16e+08 ...
 $ cif_trade_value_usd   : logi  NA NA NA NA NA NA ...
 $ fob_trade_value_usd   : logi  NA NA NA NA NA NA ...
 $ flag                  : int  0 0 0 0 0 0 0 0 0 0 ...
 - attr(*, "url")= chr "https://comtrade.un.org/api/get?max=50000&type=C&freq=A&px=HS&ps=2018&r=218&p=all&rg=2&cc=0803&fmt=json&head=H"
 - attr(*, "time_stamp")= POSIXct[1:1], format: "2020-07-03 16:07:00"
 - attr(*, "req_duration")= num 4.34

Data Wrangling


# Select the data you need (in this case you only need the partner and qty columns)

quantitybanana <- dplyr::select(BananaExport, partner, qty)

# Rename the "partner" column and call it "NAME" to match the "world@data" data frame

names(quantitybanana)[names(quantitybanana) == "partner"] <- "NAME"

# Rename USA and call it "United States of America" to match the "world@data" data frame

quantitybanana$NAME <- gsub("USA", "United States of America", quantitybanana$NAME)

quantitybanana$NAME <- gsub("Russian Federation", "Russia", quantitybanana$NAME)

Visualization


# Open following libraries

library(spdep)
library(maptools)
library(leaflet)
library(dplyr)
library(maps)
library(rgdal)

# Open your shp file with the readOGR function 
world <- readOGR('world.shp')

OGR data source with driver: ESRI Shapefile 
Source: "/home/thibaults/portfolio/warin/_posts/api-comtradr-application/world.shp", layer: "world"
with 241 features
It has 94 fields
Integer64 fields read as strings:  POP_EST NE_ID 

Shapefile link.

Remember, the .zip file must contain at least the .shp, .shx, .dbf, and .prj files for the .shp to work properly


# Use "Left join" function to combine your two data frames ("quantitybanana" and "world@data")

world@data <- left_join(world@data, quantitybanana, by = "NAME")

# Create labels

world@data$NAME <- as.character(world@data$NAME)

labels <- sprintf("<strong>%s</strong><br/>%g", world@data$NAME, world@data$qty) %>% lapply(htmltools::HTML)

# Determinate the intervalls that will be shown in the map legend
bins <- c(0, 100, 1000, 5000, 10000, 50000, 100000, 250000, 500000, 1000000, 5000000, 10000000, Inf)

# Choose a color scheme for your map

colors <- colorBin("YlOrRd", domain = world@data$qty, bins = bins)

# Create custom icon to add to your map
  
bananaIcon <- makeIcon(
  iconUrl = "banana.png",
  iconWidth = 40, iconHeight = 40,
  iconAnchorX = , iconAnchorY = 
)

# Plot the data using leaflet

leaflet(world) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addMarkers(lng=-78.467834, lat=-0.180653, popup="Ecuador", icon = bananaIcon) %>%
  addLegend(pal = colors, values = world@data$qty, opacity = 0.7, title = NULL, position = "bottomleft") %>%
  addPolygons(fillColor = ~colors(world@data$qty),
              weight = 2,
              opacity = 1,
              color = "white",
              dashArray = 1,
              fillOpacity = 0.5,
              highlight = highlightOptions(weight = 2,
                                           color = "black",
                                           dashArray = 1,
                                           fillOpacity = 0.5,
                                           bringToFront = TRUE),
              label = labels
              )