ropensci / comtradr

Functions for Interacting with the UN Comtrade API
https://docs.ropensci.org/comtradr
64 stars 17 forks source link

Error when searching for EU #31

Closed evoeten closed 2 years ago

evoeten commented 3 years ago

Thanks for this great package! I have an issue with looking for EU trade statistics. Every time I look for the EU I get the same error massage. I suspect this may be related to the special character in the string.

df <- ct_search(reporters = "USA", partners = c("China", "EU-28", "Rep. of Korea" ), trade_direction = "exports")

Error: Result 2 must be a single string, not a character vector of length 0 Run

ChrisMuir commented 3 years ago

Hello, this is actually expected behavior. The Comtrade DB does not include EU-28 as a partner country, only as a reporter country. Check it out:

comtradr::ct_country_lookup("EU-28", type = "partner")
#> [1] "No matching results found"

The func ct_country_lookup() uses regex for semi-fuzzy look ups, so we can see all of the partner countries that contain the string eu:

comtradr::ct_country_lookup("EU", type = "partner")
#> [1] "Africa CAMEU region, nes" "Eastern Europe, nes"      "Europe EFTA, nes"         "Europe EU, nes"           "Neutral Zone"            
#> [6] "Other Europe, nes"

And we can see that EU-28 is a valid reporter country:

comtradr::ct_country_lookup("EU", type = "reporter")
#> [1] "EU-28"

Because of this issue, I'm working on updates to the error message returned when a bad country string is passed in. I should be pushing these edits this weekend. So now the output looks like this:

df <- ct_search(reporters = "USA",
                partners = c("China",
                             "EU-28",
                             "Rep. of Korea"),
                trade_direction = "exports")
#> Error: From arg 'partners', these values were not found in the country database: EU-28
ChrisMuir commented 3 years ago

Improved the error messaging in commit 1dbc9f4.