This function is an interface for REST Countries API. It allows to request and download information about countries, such as: currency, capital city, language spoken, flag, neighbouring countries, and much more. NOTE: Internet access is needed to download information from the API. At times the API may be unstable or slow to respond.
Usage
country_info(
countries = NULL,
fields = NULL,
fuzzy_match = TRUE,
match_info = FALSE,
collapse = TRUE
)
Arguments
- countries
A vector of countries for which we wish to download information. The function also supports fuzzy matching capabilities to facilitate querying. Information is only returned for the 249 countries in the ISO standard
3166
.- fields
Character vector indicating the fields to query. A description of the accepted fields can be found here. Alternatively, a list of accepted field names can be obtained with the function
list_fields()
.- fuzzy_match
Logical value indicating whether to allow fuzzy matching of country names. Default is
TRUE
.- match_info
Logical value indicating whether to return information on country names matched to each input in
countries
. IfTRUE
, two additional columns will be added to the output (matched_country
andis_country
). Default isFALSE
.- collapse
Logical value indicating whether to collapse multiple columns relating to a same field together. Default is
TRUE
. For some specific fields (currencies, languages, names), multiple columns will be returned. This happens because countries can take multiple values for these fields. For example,country_info("Switzerland", "languages", collapse = FALSE)
will return 4 columns for the field languages. Whencollapse = TRUE
, these four columns will be collapsed into one string, with values separated by semicolons.
Value
Returns the requested information about the countries in a table. The rows of the table correspond to entries in countries
, columns correspond to requested fields
.
Examples
# Run examples only if a connection to the API is available:
if (check_countries_api(warnings = FALSE)){
# The example below queries information on the currency used in Brazil, US and France:
info <- country_info(countries = "Brazil", fields = "capital")
# data for multiple countries can be requested
info <- country_info(countries = c("Brazil", "USA", "FR"), fields = "capital")
#' # Data can be returned for all countries by leaving - countries - empty
info <- country_info(fields = "capital")
# All available fields can be requested by leaving fields empty
info <- country_info(countries = c("Brazil", "USA", "FR"))
# All information for all countries can be downloaded by leaving both arguments empty
info <- country_info()
}