With the update of the Comtrade API by the United Nations, thispackage has undergone a comprehensive rewrite. Most functions that wereavailable have been deprecated and there are breaking changes also inthe names of arguments and possible parameter values.
With the below examples, we hope to make the transition a littleeasier. Most of the design principles of the package have remainedsimilar.
The most important changes can be summarized in that the package: -extensively checks parameters for validity before submitting them -allows iso3 standardized country codes as inputs - queries the newparameters the UN added, such as a mode of transport.
You will see that most other things have stayed more or less the sameand the transition will be a breeze! 💨
#### Previously# Get all monthly data for a single year (API max of 12 months per call).q<-ct_search(reporters ="USA",partners =c("Germany","France","Japan","Mexico"),trade_direction ="imports",start_date =2012,end_date =2012,freq ="monthly")# monthly data for a specific span of months (API max of five months per call).q<-ct_search(reporters ="USA",partners =c("Germany","France","Japan","Mexico"),trade_direction ="imports",start_date ="2012-03",end_date ="2012-07",freq ="monthly")#### Now# Get all monthly data for a single year (API max of 12 months per call).q<-ct_get_data(reporter ="USA",partner =c("DEU","FRA","JPN","MEX"),flow_direction ="import",start_date =2012,end_date =2012,frequency ="M" )# monthly data for a specific span of months (API max of five months per call).q<-ct_get_data(reporter ="USA",partner =c("DEU","FRA","JPN","MEX"),flow_direction ="import",start_date ="2012-03",end_date ="2012-07",frequency ="M" )Countries passed to parametersreporters andpartners must be spelled as they appear in the Comtradecountry reference table. Functionct_country_lookup allowsus to query the country reference table.
No need to specify the Comtrade country name, just use iso3 codes, asyou can extract them from a myriad of other packages, e.g. countrycodes,rnaturalearth orgiscoR.
This has not changed!
Search trade related to specific commodities (say, tomatoes). We canquery the Comtrade commodity reference table to see all of the differentcommodity descriptions available for tomatoes.
If we want to search for shipment data on all of the commoditydescriptions listed, then we can simply adjust the parameters forct_commodity_lookup so that it will return only the codes,which can then be passed along toct_search.
#### Previously# The url of the API call.attributes(q)$url# The date-time of the API call.attributes(q)$time_stamp# The total duration of the API call, in seconds.attributes(q)$req_duration## [1] "https://comtradeapi.un.org/data/v1/get/C/A/HS?cmdCode=TOTAL&flowCode=M&partnerCode=280%2C276&reporterCode=842%2C841&period=2012&motCode=0&partner2Code=0&customsCode=C00&includeDesc=TRUE"## [1] "2023-12-23 08:47:51 -03"comtradr ships with a few different package dataobjects, and functions for interacting with and using the packagedata.
The package data was mostly stored in different databases and had tobe queried and updated separately. See below:
All package data can be referenced from one function, whichautomatically includes theupdate possibility.
## to get the parameter values for the mode_of_transport argumentct_get_ref_table("mode_of_transport")## to get the parameter values for the partner argumentct_get_ref_table("partner")## to update the parameter reference for the partner argumentct_get_ref_table("partner",update = T)## to get any commodity classification scheme, just pass in the code## you would use in commodity_classificationct_get_ref_table("HS")