case: Case as an extension of switch

View source: R/case.R

caseR Documentation

Case as an extension of switch

Description

Takes a name (i.e., any atomic scalar) and matches that name to the name matching an element of ... where matching can be achieved by matching the name of an argument in .... When names are supplied via .names, they substituted for the names of arguments in .... When no match is found and .def = 'err' an error is thrown, otherwise, the value of .def is returned.

Usage

case(.name, ..., .names = NULL, .def = "err")

Arguments

.name

NULL or an atomic scalar naming an argument in .... NULL is converted to 'NULL' and NA is converted to 'NA'. Coerced to mode character.

...

Any number of uniquely named arguments to select from to be the return value. Selection is based on which element's name equals the value of .name. When there is no match, if .def = 'err', an error is thrown, otherwise .def is returned as the default value.

.names

Either NULL or a complete character vec. If this argument is of mode character, it is split using pipe delimiters. Its length (after potential splitting) must match the number of ... arguments. Values of .names must be unique.

.def

The default value to return if no match is found, unless .def is the character scalar 'err'. In that case, if no match is found, an error is thrown.

Value

Either the value of an argument selected from ... or the value of .def.

See Also

Other forks: fork_help(), swap_help()

Examples

case("one", one = 1, two = letters, .def = "default")
case("two", one = 1, two = letters, .def = "default")
case("three", one = 1, two = letters, .def = "default")
case("three", 1, letters, .names = c("three", "four"), .def = "default")

j-martineau/uj documentation built on Sept. 14, 2024, 4:40 a.m.