A simple R package for the UN Comtrade API
I have recently been doing a lot of work in R, a statistical programming language, and have started accumulating a small collection of functions that I regularly use. One of the tools I have particularly come to rely on is this function to obtain data from the United Nation's Comtrade database, a great source for country-to-country trade statistics. Since there seemed to be no R package that provides access to their new API (which is still in beta), I simply wrote a function for myself based on the sample code on their homepage and kept it with my personal files. Motivated by posts like this one, however, I decided to put the code into an R package and share it here for others to use.
You can get the source file here: comtrade.tar.gz, MD5 sum:
The package is identical to the sample code on the UN homepage and simply adds documentation. Note that they say that their API may change at any point, and since I have no plans to keep this updated (for now, at least), this may simply stop working in the future. Nevertheless, here is how to set up the package directly from within R:
download.file(url = "https://media.portblue.net/resources/150429_uncomtrade-r-package/comtrade.tar.gz", destfile = "comtrade.tar.gz") library(tools) md5sum("comtrade.tar.gz")
If the MD5 checksum corresponds to what you see above, go ahead and install the file and start pulling data from the UN using the
get.Comtrade() function call:
install.packages("comtrade.tar.gz", repos = NULL, type = "source") library(comtrade) (s1 <- get.Comtrade(r = "842", p = "124,484"))