create.enum: Create an enumeration (an enumeration is a list of constants)

Description Usage Arguments Details Value Examples

View source: R/create_enum.R

Description

This helper function builds an enum type using the provided arguments.

Usage

1
2
3
create.enum(allowed.values, value.names = if (is.null(names(allowed.values)))
  allowed.values else names(allowed.values), descriptions = value.names,
  ensure.valid.value.names = TRUE)

Arguments

allowed.values

Vector with all allowed values and (optionally) the enum names as names of the vector elements.

value.names

Vector of character strings containing the names that correspond to the allowed values If the names are neither provided in the allowed values nor in the parameter 'value.names' the enum values are taken as names. Duplicated names will be made unique only if the parameter ensure.valid.value.names is set to TRUE.

descriptions

Vector with more detailled descriptive information for each enum value

ensure.valid.value.names

TRUE to convert invalid characters into syntacically allowed names and make duplicated names unique (by appending a number). FALSE leaves value names like they are. To use enum names that contain invalid characters you have to quote them, e. g. my.enum$`a special name`

Details

Setting the enum names via the value.names parameter is mainly useful to load the elements of an enum from a data base or config file (e. g. CSV file) to create an enum type.

Since the returned enumeration object is also a list you can also use it like a list, e. g. accessing single enum values by using the $ operator (ENUM$VALUE_NAME). This is the reason for a list instead of an atomic vector as internal representation.

Value

An object of class "enumeration" that represents an enumeration via a list with named elements

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
               
# This is the easiest way to create an enumeration if the enum values are not important
DRINKS <- create.enum(c("COFFEE", "TEA", "SOFT DRINK"))

# This is the most intuitive way of creating an enumeration
COLOR.ENUM <- create.enum(c(BLUE = 1L, RED = 2L, BLACK = 3L))

COLOR.ENUM <- create.enum(c(1L, 2L, 3L), c("BLUE", "RED", "BLACK"))
# returns an enumeration type that internally is constructed similar to this:
# COLOR.ENUM <- list(BLUE = 1L, RED = 2L, BLACK = 3L)

aryoda/R_enumerations documentation built on Nov. 7, 2017, 7:12 a.m.