Evaluate One of Several Expressions

DESCRIPTION:
Branches the evaluation depending on the value of the first argument.

USAGE:
switch(EXPRESSION, ...)

REQUIRED ARGUMENTS:
EXPRESSION:
an S-PLUS object which is character or numeric when evaluated. This is used to choose which of the remaining arguments to evaluate. An error occurs if this is of length zero.
...:
one of these arguments (at most) will be evaluated, and returned as the value of switch.

VALUE:
the value of the expression that is picked, or NULL if no expression is picked.

SIDE EFFECTS:
any side effects of the selected expression are performed.

DETAILS:
switch evaluates EXPRESSION (the first argument). If the value of EXPRESSION is of mode "character", the function looks among the names of the remaining arguments for one that exactly (not partially) matches the character string (only using the first element of the value). If there is a match, the corresponding argument is evaluated and returned as the value of switch; otherwise the first unnamed argument is evaluated and returned. In the case when EXPRESSION is character, some arguments associated with a name can be omitted; if the value of EXPRESSION matches a name without an argument, the next non-omitted argument is selected - see the examples section.

If the first argument is not character, it is coerced to integer; if it is in the range 1 to nargs()-1, then the corresponding argument from the ... is evaluated and returned as the value of switch.


SEE ALSO:
if , ifelse , Syntax .

EXAMPLES:
switch(dist, cauchy=rcauchy(1), logistic=rlogis(1), norm=rnorm(1))

x <- 2 switch(x, 2+2, print("this one is chosen"), { y <- exp(5); z <- sin(2.1)})

switch(as.character(answer), yes=, YES=1, no=, NO=2, 3)