as_enum: Coerce lists, environments, and factors to enumerations

Description Usage Arguments Value See Also Examples

View source: R/as_enum.r

Description

as_enum() turns an existing object into an enumeration. Coercion can be used when you want to create an enum, but don't know what the exact values each member will contain at runtime. This is comparable to the as.list method.

As an S3 generic, as_enum() holds methods for:

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
as_enum(x, ...)

## S3 method for class 'list'
as_enum(x, ...)

## S3 method for class 'environment'
as_enum(x, ..., .all_names = FALSE, .sorted = TRUE)

## S3 method for class 'factor'
as_enum(x, ..., .sorted = TRUE)

## S3 method for class 'data.frame'
as_enum(x, ..., use_cols = FALSE, use_rows = FALSE)

## S3 method for class ''NULL''
as_enum(x, ...)

## Default S3 method:
as_enum(x, ...)

Arguments

x

the object to coerce to enum

...

parameters to pass to further methods

.all_names

whether to include variables with a dot .

.sorted

whether the object elements should be sorted

use_cols

cols of the data frame are used for name/value pairs instead of the data frame's observations

use_rows

rows of the data frame are used for name/value pairs instead of the data frame's observations

Value

An enumeration (enum), a list of unique name/value pairs

See Also

enum() create an enum from supplied arguments. new_generic_enum() and new_numeric_enum() create enums from a list of arguments.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
as_enum(list(x = 5, y = "str"))

as_enum(rlang::env(a = 1, b = "str"))

as_enum(factor(c("January", "February", "December"), levels = month.name))

as_enum(mtcars)

as_enum(mtcars, use_cols = TRUE)

as_enum(mtcars, use_rows = TRUE)

ElianHugh/enumr documentation built on Dec. 17, 2021, 6:25 p.m.