The Comtrade API requires that searches for specific commodities be done using commodity codes. This is a helper function for querying the Comtrade commodity database. It takes as input a vector of commodities or commodity codes. Output is a list or vector of commodity descriptions or codes associated with the input search_terms. For use with the UN Comtrade API, full API docs can be found at https://comtrade.un.org/data/doc/api/
Usage
ct_commodity_lookup(
search_terms,
return_code = FALSE,
return_char = FALSE,
verbose = TRUE,
ignore.case = TRUE,
...
)
Arguments
- search_terms
Commodity names or commodity codes, as a char or numeric vector.
- return_code
Logical, if set to FALSE, the function will return a set of commodity descriptions along with commodity codes (as a single string for each match found), if set to TRUE it will return only the commodity codes. Default value is FALSE.
- return_char
Logical, if set to FALSE, the function will return the matches as a named list, if set to TRUE it will return them as a character vector. Default value is FALSE.
- verbose
Logical, if set to TRUE, a warning message will print to console if any of the elements of input "search_terms" returned no matches (message will indicate which elements returned no data). Default is TRUE.
- ignore.case
logical, to be passed along to arg ignore.case within
grepl
. Default value is TRUE.- ...
additional args to be passed along to
grepl
.
Value
A list or character vector of commodity descriptions and/or commodity codes that are matches with the elements of "search_terms".
Details
This function uses regular expressions (regex) to find matches within the commodity DB. This means it will treat as a match any commodity description that contains the input search term. For more on using regex within R, see https://stat.ethz.ch/R-manual/R-devel/library/base/html/regex.html
Examples
# Look up commodity descriptions related to "halibut"
ct_commodity_lookup("halibut",
return_code = FALSE,
return_char = FALSE,
verbose = TRUE)
#> $halibut
#> [1] "030221 - Fish; halibut (reinhardtius hippoglossoides, hippoglossus hippoglossus, hippoglossus stenolepis), fresh or chilled (excluding fillets, livers, roes and other fish meat of heading no. 0304)"
#> [2] "030331 - Fish; halibut (reinhardtius hippoglossoides, hippoglossus hippoglossus, hippoglossus stenolepis), frozen (excluding fillets, livers, roes and other fish meat of heading no. 0304)"
#>
# Look up commodity codes related to "shrimp".
ct_commodity_lookup("shrimp",
return_code = TRUE,
return_char = FALSE,
verbose = TRUE)
#> $shrimp
#> [1] "030613" "030616" "030617" "030623" "030626" "030627" "030635" "030636"
#> [9] "030695" "160520" "160521" "160529" "160540"
#>