case {pkgutils} | R Documentation |
An altered switch
statement for stricter flow
control.
case(EXPR, ...) ## S3 method for class 'double' case(EXPR, ...) ## S3 method for class 'integer' case(EXPR, ...) ## S3 method for class 'character' case(EXPR, ...)
EXPR |
A character or numeric scalar based on which a decision is made. |
... |
Additional arguments from which to select an alternative. |
If EXPR
is a character scalar, the behaviour is
like the one of switch
with the exception that
unmatched values within ...
cause an error. If
EXPR
is of mode ‘numeric’, the behaviour is
like switch
but counting starts at 0 and a value
larger than the number of elements within ...
selects the last element. It is an error if EXPR
is negative or if ...
contains no arguments at
all.
Selected value from ...
.
base::switch
Other coding-functions: L
,
LL
, assert
,
check
, collect
,
contains
, flatten
,
listing
, map_names
,
map_values
, must
,
set
, sql
,
unnest
# 'numeric' method
(x <- case(0, "a", "b", "c"))
## [1] "a"
stopifnot(identical(x, "a"))
(x <- case(99, "a", "b", "c"))
## [1] "c"
stopifnot(identical(x, "c"))
# 'character' method
(x <- case("b", a = "x", b = "y", c = "z"))
## [1] "y"
stopifnot(identical(x, "y"))
(x <- try(case("d", a = "x", b = "y", c = "z"), silent = TRUE))
## [1] "Error in case.character(\"d\", a = \"x\", b = \"y\", c = \"z\") : \n unmatched 'EXPR' value\n"
## attr(,"class")
## [1] "try-error"
## attr(,"condition")
## <simpleError in case.character("d", a = "x", b = "y", c = "z"): unmatched 'EXPR' value>
stopifnot(inherits(x, "try-error"))