and

r-universe status badge License: MIT R build status Dependencies

and constructs language-aware lists in R. It extends the functionality of functions like knitr::combine_words() and glue::glue_collapse() to make “and”-separated and “or”-separated lists that automatically conform to the user’s R language settings.

Installation

You can install the released version of and from CRAN with:

install.packages("and")

or the development version of and from GitHub with:

# install.packages("pak")
pak::pkg_install("rossellhayes/and")

Usage

and() creates “and”-separated lists from vectors.

names <- c("John", "Paul", "George", "Ringo")
and(names)
#> [1] "John, Paul, George, and Ringo"

But the Oxford comma is less common in other varieties of English, what happens if I change my R language to British English?

Sys.setenv(LANG = "en_GB")
and(names)
#> [1] "John, Paul, George and Ringo"

What about other languages?

Sys.setenv(LANG = "es")
and(names)
#> [1] "John, Paul, George y Ringo"
Sys.setenv(LANG = "fr")
and(names)
#> [1] "John, Paul, George et Ringo"
Sys.setenv(LANG = "hi")
and(names)
#> [1] "John, Paul, George, और Ringo"

Handling the nuances

Creating a list is not as simple as putting a different word between the last two items. For example, in Spanish, the word for “and” changes if the next word starts with an i or y:

princess_bride <- c("Vizzini", "Fezzik", "Inigo Montoya")
Sys.setenv(LANG = "es")
and(princess_bride)
#> [1] "Vizzini, Fezzik e Inigo Montoya"

“or”-separated lists

Everything and() can do or() can do better. Just use or() to create “or”-separated lists with all the same contextual awareness.

outcomes <- c("win", "lose", "draw")
Sys.setenv(LANG = "en_US")
or(outcomes)
#> [1] "win, lose, or draw"
Sys.setenv(LANG = "ja")
or(outcomes)
#> [1] "win、lose、またはdraw"

Hardcoding language

Don’t want the language of you string to depend on the user’s environment variables? You can explicitly set the language using the language argument.

and(names, language = "en_US")
#> Warning in Sys.setlocale("LC_MESSAGES", ""): OS reports request to set locale to
#> "" cannot be honored
#> [1] "John, Paul, George, and Ringo"
and(names, language = "en_GB")
#> Warning in Sys.setlocale("LC_MESSAGES", ""): OS reports request to set locale to
#> "" cannot be honored
#> [1] "John, Paul, George and Ringo"
and(names, language = "es")
#> Warning in Sys.setlocale("LC_MESSAGES", ""): OS reports request to set locale to
#> "" cannot be honored
#> [1] "John, Paul, George y Ringo"
and(names, language = "fr")
#> Warning in Sys.setlocale("LC_MESSAGES", ""): La requête OS pour spécifier la
#> localisation à "" n'a pas pu être honorée
#> [1] "John, Paul, George et Ringo"

Please note that and is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.