Skip to contents

🚧 Package soon to be archived! 🚧

This package will soon be archived in favor of a relaunch of the comtradr package.

While we are still making changes to comtradr I will keep this package online, because the main function and API to the Goods endpoint of Comtrade is working, but please be aware, that the package might be archived at any time.

Installation

You can install the development version of untrader like so:

devtools::install_github('datapumpernickel/untrader')

Example

This is a basic example which shows you how to solve a common problem:

library(untrader)
library(ggplot2)
library(stringr)

## you need to set your API key first
# set_primary_comtrade_key()

exports <- get_comtrade_data(frequency =  'A',
                   commodity_classification = 'HS',
                   commodity_code = c('2204','2203'),
                   flow_direction = 'export',
                   reporter = c("ARG","GBR"),
                   partner = 'world',
                   start_date = "2018",
                   end_date = "2021",
                   verbose = T,
                  process = T)
#> ✔ Checked validity of frequency!
#> ✔ Checked validity of commodity_classification.
#> ✔ Checked validity of flow_direction.
#> ✔ Checked validity of commodity_code.
#> ✔ Checked validity of reporter.
#> ✔ Checked validity of partner.
#> ✔ Checked validity of start and end dates.
#> ℹ URL that will be queried:
#>   https://comtradeapi.un.org/data/v1/get/C/A/HS?cmdCode=2204%2C2203&flowCode=X&partnerCode=0&reporterCode=32%2C826&period=2018%2C2019%2C2020%2C2021&motCode=0&partner2Code=0&customsCode=C00&includeDesc=TRUE
#> ℹ Performing request, which can take a few seconds, depending on the amount of
#>   data queried
#> ✔ Got a response object from Comtrade. Use `process = F` if there is an error
#>   after this step to find issues with the response object.

ggplot(exports) +
  geom_col(aes(
    x = period,
    y = primaryValue / 1000000,
    fill = str_wrap(cmdDesc,30)
  ),
  position = 'dodge') +
  facet_wrap(. ~ reporterDesc) +
  theme_minimal() +
  scale_fill_manual(name = "HS Code", values = c('#F3B562','#F06060'))+
  ylab("Exports in Million USD") +
  xlab("Year") +
  labs(title = 'Exports of Wine and Beer from Great Britain and Argentina')


str(exports)
#> 'data.frame':    16 obs. of  47 variables:
#>  $ typeCode                : chr  "C" "C" "C" "C" ...
#>  $ freqCode                : chr  "A" "A" "A" "A" ...
#>  $ refPeriodId             : int  20180101 20180101 20180101 20180101 20190101 20190101 20190101 20190101 20200101 20200101 ...
#>  $ refYear                 : int  2018 2018 2018 2018 2019 2019 2019 2019 2020 2020 ...
#>  $ refMonth                : int  52 52 52 52 52 52 52 52 52 52 ...
#>  $ period                  : chr  "2018" "2018" "2018" "2018" ...
#>  $ reporterCode            : int  32 32 826 826 32 32 826 826 32 32 ...
#>  $ reporterISO             : chr  "ARG" "ARG" "GBR" "GBR" ...
#>  $ reporterDesc            : chr  "Argentina" "Argentina" "United Kingdom" "United Kingdom" ...
#>  $ flowCode                : chr  "X" "X" "X" "X" ...
#>  $ flowDesc                : chr  "Export" "Export" "Export" "Export" ...
#>  $ partnerCode             : int  0 0 0 0 0 0 0 0 0 0 ...
#>  $ partnerISO              : chr  "W00" "W00" "W00" "W00" ...
#>  $ partnerDesc             : chr  "World" "World" "World" "World" ...
#>  $ partner2Code            : int  0 0 0 0 0 0 0 0 0 0 ...
#>  $ partner2ISO             : chr  "W00" "W00" "W00" "W00" ...
#>  $ partner2Desc            : chr  "World" "World" "World" "World" ...
#>  $ classificationCode      : chr  "H5" "H5" "H5" "H5" ...
#>  $ classificationSearchCode: chr  "HS" "HS" "HS" "HS" ...
#>  $ isOriginalClassification: logi  TRUE TRUE TRUE TRUE TRUE TRUE ...
#>  $ cmdCode                 : chr  "2203" "2204" "2203" "2204" ...
#>  $ cmdDesc                 : chr  "Beer made from malt" "Wine of fresh grapes, including fortified wines; grape must other than that of heading no. 2009" "Beer made from malt" "Wine of fresh grapes, including fortified wines; grape must other than that of heading no. 2009" ...
#>  $ aggrLevel               : int  4 4 4 4 4 4 4 4 4 4 ...
#>  $ isLeaf                  : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
#>  $ customsCode             : chr  "C00" "C00" "C00" "C00" ...
#>  $ customsDesc             : chr  "TOTAL CPC" "TOTAL CPC" "TOTAL CPC" "TOTAL CPC" ...
#>  $ mosCode                 : chr  "0 " "0 " "0 " "0 " ...
#>  $ motCode                 : int  0 0 0 0 0 0 0 0 0 0 ...
#>  $ motDesc                 : chr  "TOTAL MOT" "TOTAL MOT" "TOTAL MOT" "TOTAL MOT" ...
#>  $ qtyUnitCode             : int  7 7 7 7 7 7 7 -1 7 7 ...
#>  $ qtyUnitAbbr             : chr  "l" "l" "l" "l" ...
#>  $ qty                     : num  2.30e+07 2.69e+08 5.34e+08 1.23e+08 2.98e+07 ...
#>  $ isQtyEstimated          : logi  FALSE FALSE TRUE TRUE FALSE FALSE ...
#>  $ altQtyUnitCode          : int  7 7 -1 -1 7 7 -1 -1 7 7 ...
#>  $ altQtyUnitAbbr          : chr  "l" "l" "N/A" "N/A" ...
#>  $ altQty                  : num  2.30e+07 2.69e+08 0.00 0.00 2.98e+07 ...
#>  $ isAltQtyEstimated       : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
#>  $ netWgt                  : num  2.44e+07 2.69e+08 5.37e+08 1.35e+08 3.05e+07 ...
#>  $ isNetWgtEstimated       : logi  FALSE FALSE TRUE TRUE FALSE FALSE ...
#>  $ grossWgt                : num  NA NA 0 0 0 0 0 0 0 0 ...
#>  $ isGrossWgtEstimated     : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
#>  $ cifvalue                : num  NA NA NA NA NA NA NA NA 0 0 ...
#>  $ fobvalue                : num  1.39e+07 8.20e+08 6.53e+08 8.25e+08 1.72e+07 ...
#>  $ primaryValue            : num  1.39e+07 8.20e+08 6.53e+08 8.25e+08 1.72e+07 ...
#>  $ legacyEstimationFlag    : int  0 0 0 0 0 0 2 6 0 0 ...
#>  $ isReported              : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
#>  $ isAggregate             : logi  TRUE TRUE TRUE TRUE TRUE TRUE ...

Next steps

  • Implement the different modes of transportation and different commodity codes, as well as customCodes and partner2codes

  • Implement access to the services API

  • Include some more links to the UN Comtrade API FAQ.