EnumValue-class | R Documentation |
This class is intended to be used to represent a value that originates from an enumerated constant. The idea is that this value carries around the value and the symbolic name. Additionally, if an object of a enumerated type is expected, an integer or character string could be supplied and its value compared to a definition of the enumeration in R.
It is expected that users would create new classes derived from this
one for each new enumeration type that is encountered. The definition
of the enumerated constant values would be identified from the class
name of the new class. See EnumValue
.
Objects can be created by calls of the form new("EnumValue", ...)
or via the function EnumValue
with an instance
of the appropriate target class specified.
.Data
:Object of class "integer"
,
giving the value. This must have the associated name
which makes it the symbolic constant.
Class "integer"
, from data part.
Class "vector"
, by class "integer"
.
Class "numeric"
, by class "integer"
.
signature(object = "EnumValue")
:
this takes care of displaying the value as a regular named integer
giving the human-readable form and its value.
Duncan Temple Lang (duncan@r-project.org)
EnumValue
# Define a new Enumeration class. setClass("Color", contains = "EnumValue") # Define the enumeration definition table. ColorEnum = c(Red = 1, Green = 2, Blue = 3) storage.mode(ColorEnum) = "integer" # Set the coercion methods to take a number or a string # to this class type. setAs("character", "Color", function(from) EnumValue(from, new("Color"))) setAs("numeric", "Color", function(from) EnumValue(from, new("Color"))) # Now we can use this class. as(1, "Color") as("Red", "Color") as("Blue", "Color") # These should give errors, so we enclose them in a call to try. try(as("Orange", "Color")) try(as(20, "Color"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.