Skip to content

Commit

Permalink
- Fix broken CRAN check due to stale URL.
Browse files Browse the repository at this point in the history
- Add new `nato` dataset.
- Take out `how = "dstk"` functionality because the underlying API seems definitely dead now.
  • Loading branch information
alistaire47 committed Nov 1, 2020
1 parent a3b7489 commit bc614b0
Show file tree
Hide file tree
Showing 44 changed files with 938 additions and 676 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: passport
Type: Package
Title: Travel Smoothly Between Country Name and Code Formats
Version: 0.2.0.9000
Version: 0.2.0.9001
Authors@R:
c(
person(
Expand Down Expand Up @@ -39,6 +39,6 @@ URL: https://github.com/alistaire47/passport, https://alistaire47.github.io/pass
BugReports: https://github.com/alistaire47/passport/issues
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.0
RoxygenNote: 7.1.1
Roxygen: list(markdown = TRUE)
VignetteBuilder: knitr
10 changes: 4 additions & 6 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
# passport 0.2.0.9000
# passport 0.2.0.9001

* Add `nato` dataset of NATO/OTAN member defense expenditures.
* Remove `"dstk"` option for `parse_country()` as the Data Science Tookit API
appears dead.
* Refactor README and vigette to use explicit packages instead of tidyverse.

# passport 0.2.0

* Add `order_countries()` to make sorting vectors of countries simpler,
particularly when passed to the `limits` parameter of a ggplot2 discrete
scale.

* Change `country_format()` to accept a `factor` parameter along to
`as_country_name()`.

* Update documentation and tests to account for DSTK downtime.

# passport 0.1.1

* Added a `NEWS.md` file to track changes to the package.

* Added option to use geocoding web APIs to supercharge `parse_country()`.

* Added `convert_country()` constructor for ggplot2 convenience.

* Added essential API for converting between country names and codes.
29 changes: 16 additions & 13 deletions R/codes.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@
#'
#' @format A data.frame of 427 rows and 9 variables.
#'
#' Structure:
#' Variables:
#'
#' - `column`: The column name in the internal `passport:::countries`
#' data.frame. Valid for use in `from` and `to` parameters.
#' - `code`: `column` with hyphens for underscores, which is a valid IANA
#' \describe{
#' \item{`column`}{The column name in the internal `passport:::countries`
#' data.frame. Valid for use in `from` and `to` parameters.}
#' \item{`code`}{`column` with hyphens for underscores, which is a valid IANA
#' language tag for Unicode CLDR country names. Valid for use in `from` and
#' `to` parameters.
#' - `name`: Full name or code name for non-CLDR options.
#' - `notes`: Things to note, including deprecations, oddities, etc.
#' - `language`: Full language name parsed from `code`.
#' - `region`: Full country or region name parsed from `code`.
#' - `script`: Full language script name parsed from `code`.
#' - `variant`: Full variant parsed from `code`. Also used for
#' organization-standardized names.
#' - `extension`: Further specification of name type.
#' `to` parameters.}
#' \item{`name`}{Full name or code name for non-CLDR options.}
#' \item{`notes`}{Things to note, including deprecations, oddities, etc.}
#' \item{`language`}{Full language name parsed from `code`.}
#' \item{`region`}{Full country or region name parsed from `code`.}
#' \item{`script`}{Full language script name parsed from `code`.}
#' \item{`variant`}{Full variant parsed from `code`. Also used for
#' organization-standardized names.}
#' \item{`extension`}{Further specification of name type.}
#' }
#'
#'
#' @examples
#' # A searchable widget to find a code or name
Expand Down
62 changes: 62 additions & 0 deletions R/nato.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#' NATO Member Defense Expenditures
#'
#' A sample dataset of NATO/OTAN member defense expenditures.
#'
#' @source <https://www.nato.int/cps/en/natohq/news_167080.htm>
#'
#' @format A data.frame of 232 rows and 14 variables.
#'
#' Variables:
#'
#' \describe{
#' \item{`country_stanag`}{Country code in NATO STANAG format}
#' \item{`year`}{Year, from 2012 to 2019. 2018-2019 numbers may be estimates.}
#' \item{`Defense expenditure (USD, current prices)`}{
#' Defense expenditures in US dollars, using current prices and exchange rates.
#' }
#' \item{`Defense expenditure (USD, 2015 prices)`}{
#' Defense expenditures in US dollars, using 2015 prices and exchange rates.
#' }
#' \item{`Defense expenditure (% real GDP)`}{
#' Defense expenditure as a percentage of real gross domestic product.
#' Based on 2015 prices.
#' }
#' \item{`Defense expenditure annual real change (% GDP)`}{
#' Annual change in defense expenditure as a percentage of real gross
#' domestic product. Based on 2015 prices.
#' }
#' \item{`Real GDP (2015 prices)`}{
#' Real gross domestic product in 2015 US dollars and at 2015 exchange
#' rates.
#' }
#' \item{`GDP per capita (USD)`}{
#' Gross demestic product per capita in 2015 US dollars and at 2015
#' exchange rates.
#' }
#' \item{`Defense expenditure per capita (USD)`}{
#' Defense expenditure per capita in 2015 US dollars.
#' }
#' \item{`Military personnel`}{Number of military personnel}
#' \item{`Equipment expenditure (%)`}{
#' Percent of defense expenditure spent on equipment. Includes major
#' equipment expenditure and R&D devoted to major equipment.
#' }
#' \item{`Personnel expenditure (%)`}{
#' Percentage of defense expenditure spent on personnel. Includes both
#' military and civilian expenditure and pensions.
#' }
#' \item{`Infrastructure expenditure (%)`}{
#' Percentage of defense expenditure spent on infrastructure. Includes
#' NATO common infrastructure and national military construction.
#' }
#' \item{`Other expenditure (%)`}{
#' Percentage of defense expenditure spent on other categories besides
#' equipment, personnel, and infrastructure. Includes operations and
#' maintenance expenditure, other R&D expenditure, and other expenditure
#' not otherwise captured.
#' }
#' }
#'
#' @examples
#' as_country_name(nato$country_stanag, from = 'stanag')
"nato"
21 changes: 7 additions & 14 deletions R/parse_country.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,18 @@ fromJSON <- function(...) {
jsonlite::fromJSON(...)
}

parse_by_geocoding <- function(location, source = c("google", "dstk")) {
parse_by_geocoding <- function(location, source = c("google")) {
query <- vapply(location, utils::URLencode, character(1), reserved = TRUE)
source <- match.arg(source)

base_url <- c("google" = "https://maps.googleapis.com",
"dstk" = "http://www.datasciencetoolkit.org")[source]
base_url <- c("google" = "https://maps.googleapis.com")[source]
urls <- paste0(base_url, "/maps/api/geocode/json?&address=", query)
vapply(
urls,
function(url) {
response <- fromJSON(url)
if (response$status != "OK") {
message <- c("google" = "Google Maps geocoding API call failed; status = %s. Free usage tier is limited to 2500 queries per day.",
"dstk" = "Data Science Toolkit geocoding API call failed; status = %s")[source]
message <- c("google" = "Google Maps geocoding API call failed; status = %s. Free usage tier is limited to 2500 queries per day.")[source]
stop(sprintf(message, response$status))
}
components <- response$results$address_components[[1]]
Expand Down Expand Up @@ -76,12 +74,8 @@ parse_by_geocoding <- function(location, source = c("google", "dstk")) {
#' input. For more calls, see options that allow passing an API key like
#' `ggmap::geocode()` with `output = "all"` or `googleway::google_geocode()`.
#'
#' If `how = "dstk"`, `parse_country` will use the Data Science Toolkit
#' geocoding API.
#'
#' Note that due to their flexibility, the APIs may fail unpredictably, e.g.
#' `parse_country("foo", how = "google")` returns `"CH"` and
#' `parse_country("foo", how = "dstk")` returns `"DJ"` whereas `how = "regex"`
#' `parse_country("foo", how = "google")` returns `"CH"` whereas `how = "regex"`
#' fails with a graceful `NA` and warning.
#'
#'
Expand All @@ -90,11 +84,10 @@ parse_by_geocoding <- function(location, source = c("google", "dstk")) {
#' @param to Format to which to convert. Defaults to `"iso2c"`; see [`codes`]
#' for more options.
#' @param how How to parse; defaults to `"regex"`. `"google"`` uses the Google
#' Maps geocoding API; `"dstk"`` uses the Data Science Toolkit geocoding
#' API. See "Details" for more information.
#' Maps geocoding API. See "Details" for more information.
#' @param language If `how = "regex"`, the language from which to parse country
#' names. Currently accepts `"en"` (default) and `"de"`. Ignored if
#' `how = "google"` or `how = "dstk".`
#' `how = "google"`.
#'
#' @return A character vector or factor of ISO 2-character country codes or
#' other specified codes or names. Warns of any parsing failure.
Expand All @@ -115,7 +108,7 @@ parse_by_geocoding <- function(location, source = c("google", "dstk")) {
#' @export
parse_country <- function(x,
to = "iso2c",
how = c("regex", "google", "dstk"),
how = c("regex", "google"),
language = c("en", "de"),
factor = is.factor(x)) {
# parameter checking
Expand Down
43 changes: 16 additions & 27 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -93,35 +93,24 @@ parse_country(c("1600 Pennsylvania Ave, DC", "Eiffel Tower"), how = "google")

### II. Convert

If data comes with countries already coded, convert them with
`as_country_code()`:
If data comes with countries already coded,

```{r convert-1, message = FALSE}
# 2016 Olympic gold medal data
olympics <- read.table("https://raw.githubusercontent.com/nbremer/olympicfeathers/gh-pages/data/raw%20medal%20data/Rio%202016%20gold%20medal%20winners.txt",
sep = "\t", header = TRUE, na.strings = "",
stringsAsFactors = FALSE)
olympics %>% count(country = as_country_code(NOC, from = "ioc"), sort = TRUE)
```

or to convert to country names, use `as_country_name()`:
- convert them to ISO or other codes with `as_country_code()`
- convert them to country names with `as_country_name()`
- convert them to other languages with `as_country_name()`

```{r convert-2}
olympics %>%
count(country = as_country_name(NOC, from = "ioc"),
Event_gender) %>%
spread(Event_gender, n) %>%
arrange(desc(W))
```

or translate to another language:

```{r convert-3}
olympics$NOC %>%
unique() %>%
as_country_name(from = "ioc", to = "ta-my") %>%
head(10)
```{r convert-1, message = FALSE}
# NATO member defense expenditure data; see `?nato`
data("nato", package = "passport")
nato %>%
select(country_stanag) %>%
distinct() %>%
mutate(
country_iso = as_country_code(country_stanag, from = "stanag"),
country_name = as_country_name(country_stanag, from = "stanag", short = FALSE),
country_name_thai = as_country_name(country_stanag, from = "stanag", to = "ta-my")
)
```

Language formats largely follow [IETF language tag BCP
Expand Down
Loading

0 comments on commit bc614b0

Please sign in to comment.