asEnumValue | R Documentation |
This allows the user to specify a name or value when an enumerated constant is expected and this function ensures that it is valid. The function returns a formal object of that enumeration class containing both the name and value which can be passed to C code that expects an object of that type.
This is typically not called directly by the R user
but implicitly by generated code that coerces
user input to a particular type of
enumerated constant. Such code is typically
programmatically generated and uses the expression
as(val, "NameOfEnumType")
, with the appropriate
name for the type of enumeration.
This function also attempts to fix typos by using
agrep
to find close matches.
It can raise either an error or a warning
with information about the possible matches
and can continue on using them if fixCloseMatches
is
TRUE
.
asEnumValue(val, values, class = values@EnumName, fromString = NA, fixCloseMatches = TRUE, prefix = character(), S3 = is.null(getClassDef(class)))
val |
either the symbolic name or number which is to be validated
and coerced to an EnumValue of the particular |
values |
the |
class |
the class of the object that the function will return, i.e. the type of enumeration. The default is almost always the correct value for enumerations. For BitwiseValues, the target class is usually specified. |
fromString |
indicates whether the the value is being specified as a name or a number. This is rarely needed and is determined by the function. But it does allow us to treat a string as a number. |
fixCloseMatches |
a logical value.
For any elements of |
prefix |
a prefix string that is common to all of the symbolic names. This allows us to use this as a prefix for the name if it is not an exact match to the defined names for the collection. In other words, we can allow the caller to use a shorthand form without the common prefix, if there is one. |
S3 |
ignored. Related to using S3 classes and unlikely to be used. |
An object class class
which is typically obtained from the
EnumName
slot in the values
argument defining the possible enumerated values.
Duncan Temple Lang
The RGCCTranslationUnit
package at
http://www.omegahat.org/RGCCTranslationUnit.
edef = EnumDef("Color", c("red" = 1, "blue" = 2, "green" = 3, "yellow" = 4, "grey" = 10)) x = asEnumValue("red", edef) class(x) y = asEnumValue(c("red", "yellow"), edef) # Illustrates when we specify a value slightly incorrectly. # go on but issue a warning. asEnumValue(c("red", "gray"), edef, fixCloseMatches = TRUE) # raise an error. try(asEnumValue(c("red", "gray"), edef, fixCloseMatches = FALSE))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.