case: Modified switch function

Description Usage Arguments Details Value See Also Examples

View source: R/coding.R

Description

An altered switch statement for stricter flow control.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
  case(EXPR, ...)

  ## S3 method for class 'double'
 case(EXPR, ...)

  ## S3 method for class 'integer'
 case(EXPR, ...)

  ## S3 method for class 'character'
 case(EXPR, ...)

Arguments

EXPR

A character or numeric scalar based on which a decision is made.

...

Additional arguments from which to select an alternative.

Details

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.

Value

Selected value from ....

See Also

base::switch

Other coding-functions: L, LL, assert, check, collect, contains, flatten, listing, map_names, map_values, must, set, sql, unnest

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 'numeric' method
(x <- case(0, "a", "b", "c"))
stopifnot(identical(x, "a"))
(x <- case(99, "a", "b", "c"))
stopifnot(identical(x, "c"))

# 'character' method
(x <- case("b", a = "x", b = "y", c = "z"))
stopifnot(identical(x, "y"))
(x <- try(case("d", a = "x", b = "y", c = "z"), silent = TRUE))
stopifnot(inherits(x, "try-error"))

pkgutils documentation built on May 2, 2019, 5:49 p.m.

Related to case in pkgutils...