From 4bf59d64812249ab842088612e8b62c678e4db2b Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Wed, 14 Jul 2021 22:03:13 -0400 Subject: [PATCH] remove countrycode_factory and prep 1.3.0 --- .Rbuildignore | 1 + DESCRIPTION | 6 +-- NAMESPACE | 1 - NEWS.md | 3 +- R/countrycode.R | 2 +- R/countrycode_factory.R | 56 ----------------------- README.md | 30 ++++++++----- man/countrycode.Rd | 2 +- man/countrycode_factory.Rd | 84 ----------------------------------- tests/testthat/test-factory.R | 30 ------------- 10 files changed, 27 insertions(+), 188 deletions(-) delete mode 100644 R/countrycode_factory.R delete mode 100644 man/countrycode_factory.Rd delete mode 100644 tests/testthat/test-factory.R diff --git a/.Rbuildignore b/.Rbuildignore index 6ec2187..36a3e1f 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -7,6 +7,7 @@ README.md LICENSE* joss dictionary +.lintr revdep* .travis.yml ^.*\.Rproj$ diff --git a/DESCRIPTION b/DESCRIPTION index 69444ec..727a60d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: countrycode Title: Convert Country Names and Country Codes -Version: 1.2.0 -Date: 2020-05-22 +Version: 1.3.0 +Date: 2021-07-14 Authors@R: c(person(given = "Vincent", family = "Arel-Bundock", @@ -23,7 +23,7 @@ Description: Standardize country names, convert them into one of 40 different coding schemes, convert between coding schemes, and assign region descriptors. License: GPL-3 -URL: https://vincentarelbundock.github.io/countrycode +URL: https://vincentarelbundock.github.io/countrycode/ BugReports: https://github.com/vincentarelbundock/countrycode/issues Depends: R (>= 2.10) diff --git a/NAMESPACE b/NAMESPACE index 710bb96..fe48c9f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,5 @@ # Generated by roxygen2: do not edit by hand export(countrycode) -export(countrycode_factory) export(countryname) export(guess_field) diff --git a/NEWS.md b/NEWS.md index cc4b295..f8365df 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,5 @@ -# countrycode 1.2.0.9000 +# countrycode 1.3.0 -* new function: countrycode_factory * destination argument accepts a vector of strings and tries one after the other * countryname(warn=TRUE) by default * better class checks diff --git a/R/countrycode.R b/R/countrycode.R index 5e5da07..864c2d6 100644 --- a/R/countrycode.R +++ b/R/countrycode.R @@ -83,7 +83,7 @@ #' #' \dontrun{ #' # Download the dictionary of US states from Github -#' state_dict <- "https://raw.githubusercontent.com/vincentarelbundock/countrycode/main/data/custom_dictionaries/us_states.csv" +#' state_dict <- "https://bit.ly/2ToSrFv" #' state_dict <- read.csv(state_dict) #' #' # The "state.regex" column includes regular expressions, so we set an attribute. diff --git a/R/countrycode_factory.R b/R/countrycode_factory.R deleted file mode 100644 index 19e5f33..0000000 --- a/R/countrycode_factory.R +++ /dev/null @@ -1,56 +0,0 @@ -#' Create new conversion functions with alternative dictionaries -#' and/or defaults arguments. -#' -#' This function can be used to set new default arguments for the -#' `countrycode` functions, or to create alternative functions using -#' your own conversion dictionary. The Examples section shows how to -#' use `countrycode_factory` to create a `statecode` function which -#' can be used to convert US states to abbreviations or long names. -#' -#' @inheritParams countrycode -#' @examples -#' \dontrun{ -#' # Download the dictionary of US states from Github -#' state_dict <- "https://raw.githubusercontent.com/vincentarelbundock/countrycode/main/data/custom_dictionaries/us_states.csv" -#' state_dict <- read.csv(state_dict) -#' -#' # The "state.regex" column includes regular expressions, so we set an attribute. -#' attr(state_dict, "origin_regex") <- "state.regex" -#' -#' # Set default values for the custom conversion function -#' statecode <- countrycode_factory( -#' origin = "state.regex", -#' destination = "abbreviation", -#' custom_dict = state_dict) -#' -#' # Convert -#' x <- c("Alabama", "New Mexico") -#' statecode(x, "state.regex", "abbreviation") -#' -#' x <- c("AL", "NM", "VT") -#' statecode(x, "abbreviation", "state") -#' } -#' @export -countrycode_factory <- function( - origin = NULL, - destination = NULL, - warn = NULL, - nomatch = NULL, - custom_dict = NULL, - custom_match = NULL) { - - # default function - out = countrycode - - # change defaults - for (name in names(formals())) { - arg <- get(name) - if (!is.null(arg)) { - formals(out)[[name]] <- arg - } - } - - # return function - out - -} diff --git a/README.md b/README.md index 42a5658..817e303 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,6 @@ If you use `countrycode` in your research, we would be very grateful if you coul - [`custom_match`: Override default values](https://github.com/vincentarelbundock/countrycode#custom_match-override-default-values) - [`warn`: Silence warnings](https://github.com/vincentarelbundock/countrycode#warn-silence-warnings) * [`countryname`: Convert country names from any language](https://github.com/vincentarelbundock/countrycode#countryname-convert-country-names-from-any-language) -* [`countrycode_factory`: Custom conversion functions and "crosswalks"](https://github.com/vincentarelbundock/countrycode#countrycode_factory-custom-conversion-functions-and-crosswalks) * [Contributions](https://github.com/vincentarelbundock/countrycode#contributions) # Why `countrycode`? @@ -353,9 +352,9 @@ The function `countryname` tries to convert country names from any language. For [1] "ZWE" "AFG" "BRB" "SWE" "GBR" "SGS" ``` -# `countrycode_factory`: Custom conversion functions and "crosswalks" +# Custom conversion functions and "crosswalks" -The `countrycode_factory` function allows you to create alternative functions with different default arguments and/or dictionaries. For example, we can create: +It is easy to to create alternative functions with different default arguments and/or dictionaries. For example, we can create: * `name_to_iso3c` function that sets new defaults for the `origin` and `destination` arguments, and automatically converts country names to iso3c * `statecode` function to convert US state codes using a custom dictionary by default, that we download from the internet. @@ -366,8 +365,12 @@ The `countrycode_factory` function allows you to create alternative functions wi ################################# # Custom defaults -name_to_iso3c <- countrycode_factory( - origin = "country.name", destination = "iso3c") +name_to_iso3c <- function(sourcevar, + origin = "country.name", + destination = "iso3c", + ...) { + countrycode(sourcevar, origin = origin, destination = destination, ...) +} name_to_iso3c(c("Algeria", "Canada")) #> [1] "DZA" "CAN" @@ -383,11 +386,18 @@ state_dict <- read.csv(state_dict) # Identify regular expression origin codes attr(state_dict, "origin_regex") <- "state.regex" -# Set default values for the custom conversion function -statecode <- countrycode_factory( - origin = "state.regex", - destination = "abbreviation", - custom_dict = state_dict) +# Define a custom conversion function +statecode <- function(sourcevar, + origin = "state.regex", + destination = "abbreviation", + custom_dict = state_dict, + ...) { + countrycode(sourcevar, + origin = origin, + destination = destination, + custom_dict = custom_dict, + ...) +} # VoilĂ ! x <- c("Alabama", "New Mexico") diff --git a/man/countrycode.Rd b/man/countrycode.Rd index 35f37b0..cbaaa87 100644 --- a/man/countrycode.Rd +++ b/man/countrycode.Rd @@ -105,7 +105,7 @@ countrycode(c('United States', 'Algeria'), 'country.name', 'iso3c', \dontrun{ # Download the dictionary of US states from Github - state_dict <- "https://raw.githubusercontent.com/vincentarelbundock/countrycode/main/data/custom_dictionaries/us_states.csv" + state_dict <- "https://bit.ly/2ToSrFv" state_dict <- read.csv(state_dict) # The "state.regex" column includes regular expressions, so we set an attribute. diff --git a/man/countrycode_factory.Rd b/man/countrycode_factory.Rd deleted file mode 100644 index 336d46f..0000000 --- a/man/countrycode_factory.Rd +++ /dev/null @@ -1,84 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/countrycode_factory.R -\name{countrycode_factory} -\alias{countrycode_factory} -\title{Create new conversion functions with alternative dictionaries -and/or defaults arguments.} -\usage{ -countrycode_factory( - origin = NULL, - destination = NULL, - warn = NULL, - nomatch = NULL, - custom_dict = NULL, - custom_match = NULL -) -} -\arguments{ -\item{origin}{A string which identifies the coding scheme of origin (e.g., \code{"iso3c"}). See -\code{\link{codelist}} for a list of available codes.} - -\item{destination}{A string or vector of strings which identify the coding -scheme of destination (e.g., \code{"iso3c"} or \code{c("cowc", "iso3c")}). See -\code{\link{codelist}} for a list of available codes. When users supply a -vector of destination codes, they are used sequentially to fill in -missing values not covered by the previous destination code in the -vector.} - -\item{warn}{Prints unique elements from sourcevar for which no match was found} - -\item{nomatch}{When countrycode fails to find a match for the code of -origin, it fills-in the destination vector with \code{nomatch}. The default -behavior is to fill non-matching codes with \code{NA}. If \code{nomatch = NULL}, -countrycode tries to use the origin vector to fill-in missing values in the -destination vector. \code{nomatch} must be either \code{NULL}, of length 1, or of the same -length as \code{sourcevar}.} - -\item{custom_dict}{A data frame which supplies a new dictionary to -replace the built-in country code dictionary. Each column -contains a different code and must include no duplicates. The -data frame format should resemble \code{\link{codelist}}. Users -can pre-assign attributes to this custom dictionary to affect -behavior (see Examples section): -\itemize{ -\item "origin.regex" attribute: a character vector with the names -of columns containing regular expressions. -\item "origin.valid" attribute: a character vector with the names -of columns that are accepted as valid origin codes. -}} - -\item{custom_match}{A named vector which supplies custom origin and -destination matches that will supercede any matching default result. The name -of each element will be used as the origin code, and the value of each -element will be used as the destination code.} -} -\description{ -This function can be used to set new default arguments for the -\code{countrycode} functions, or to create alternative functions using -your own conversion dictionary. The Examples section shows how to -use \code{countrycode_factory} to create a \code{statecode} function which -can be used to convert US states to abbreviations or long names. -} -\examples{ -\dontrun{ - # Download the dictionary of US states from Github - state_dict <- "https://raw.githubusercontent.com/vincentarelbundock/countrycode/main/data/custom_dictionaries/us_states.csv" - state_dict <- read.csv(state_dict) - - # The "state.regex" column includes regular expressions, so we set an attribute. - attr(state_dict, "origin_regex") <- "state.regex" - - # Set default values for the custom conversion function - statecode <- countrycode_factory( - origin = "state.regex", - destination = "abbreviation", - custom_dict = state_dict) - - # Convert - x <- c("Alabama", "New Mexico") - statecode(x, "state.regex", "abbreviation") - - x <- c("AL", "NM", "VT") - statecode(x, "abbreviation", "state") -} -} diff --git a/tests/testthat/test-factory.R b/tests/testthat/test-factory.R deleted file mode 100644 index fcc405d..0000000 --- a/tests/testthat/test-factory.R +++ /dev/null @@ -1,30 +0,0 @@ -test_that("new function: name_to_iso3c", { - name_to_iso3c <- countrycode_factory( - origin = "country.name", destination = "iso3c") - expect_equal(c("DZA", "CAN"), name_to_iso3c(c("Algeria", "Canada"))) -}) - - -test_that("statecode", { - # Download dictionary - state_dict <- "https://raw.githubusercontent.com/vincentarelbundock/countrycode/main/data/custom_dictionaries/us_states.csv" - state_dict <- read.csv(state_dict, stringsAsFactors = FALSE) - - # Identify regular expression origin codes - attr(state_dict, "origin_regex") <- "state.regex" - - # Set default values for the custom conversion function - statecode <- countrycode_factory( - origin = "state.regex", - destination = "abbreviation", - custom_dict = state_dict) - - # VoilĂ ! - x <- c("Alabama", "New Mexico") - expect_equal(c("AL", "NM"), statecode(x, "state.regex", "abbreviation")) - - x <- c("AL", "NM", "VT") - expect_equal(c("Alabama", "New Mexico", "Vermont"), - statecode(x, "abbreviation", "state")) - -})