Skip to content

ElianHugh/enumr

Repository files navigation

enumr

Lifecycle: experimental codecov R-CMD-check

Overview

{enumr} implements static enumerations in R. At their most basic, enums are lists that have unique name/value pairs (called 'members'), and are 'static' in that they cannot be modified after their definition.

Why use enums? Some examples:

  • Improve code self-documentation by explicitly defining name/value pairs
  • Reduce the occurrence of code-breaking typos
  • Prevent accidental overwriting of variables
  • Easily change values in the future

Installation

Release build

install.packages('enumr', repos = 'https://elianhugh.r-universe.dev')

Development build

pak::pkg_install('ElianHugh/enumr@main')
# or
devtools::install_github('ElianHugh/enumr@main')

Usage

Enumr implements two classes of enum: numeric and generic. Both are created with the enum() function, {enumr} handles the identification. You don't need to worry about how they work in implementation, just know that there are rules for what constitutes a numeric vs. generic enum.

Numeric enums only permit numeric values in their name/value pairs. Numeric values include formulas/equations that evaluate to a numeric value. Numeric enum members do not need to have values explicitly defined. Instead, each member's value is either the index of the member, or the value of the previous member plus 1 - this is called 'implicit definition'.

enum(
    cat,
    dog,
    bird
)

#> # A numeric enum: 3 members
#>  int cat : 1
#>  int dog : 2
#>  int bird : 3

Generic enum members can be of any type, even including enums themselves. Each name/value pair must be explicitly defined, and each value must evaluate to a value.

enum(
    a = mtcars,
    b = PlantGrowth,
    c = airquality
)

#> # A generic enum: 3 members
#>  df a : <32 × 11>
#>  df b : <30 × 2>
#>  df c : <153 × 6>

See the pkgdown site for more information on enums, and other features of the {enumr} package, such as coercion, NSE, and typed integration.

Inspiration

Code of Conduct

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