Rev 76770 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/switch.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2016 R Core Team% Distributed under GPL 2 or later\name{switch}\alias{switch}\title{Select One of a List of Alternatives}\description{\code{switch} evaluates \code{EXPR} and accordingly chooses one of thefurther arguments (in \code{\dots}).}\usage{switch(EXPR, \dots)}\arguments{\item{EXPR}{an expression evaluating to a number or a characterstring.}\item{\dots}{the list of alternatives. If it is intended that\code{EXPR} has a character-string value these will benamed, perhaps except for one alternative to be used as a\sQuote{default} value.}}\details{\code{switch} works in two distinct ways depending whether the firstargument evaluates to a character string or a number.If the value of \code{EXPR} is not a character string it is coerced tointeger. Note that this also happens for \code{\link{factor}}s, witha warning, as typically the character level is meant. If the integeris between 1 and \code{nargs()-1} then the corresponding element of\code{\dots} is evaluated and the result returned: thus if the firstargument is \code{3} then the fourth argument is evaluated andreturned.If \code{EXPR} evaluates to a character string then that string ismatched (exactly) to the names of the elements in \code{\dots}. Ifthere is a match then that element is evaluated unless it is missing,in which case the next non-missing element is evaluated, so forexample \code{switch("cc", a = 1, cc =, cd =, d = 2)} evaluates to\code{2}. If there is more than one match, the first matching elementis used. In the case of no match, if there is an unnamed element of\code{\dots} its value is returned. (If there is more than one suchargument an error is signaled.)The first argument is always taken to be \code{EXPR}: if it is namedits name must (partially) match.A warning is signaled if no alternatives are provided, as this isusually a coding error.This is implemented as a \link{primitive} function that only evaluatesits first argument and one other if one is selected.}\section{Warning}{It is possible to write calls to \code{switch} that can be confusingand may not work in the same way in earlier versions of \R. Forcompatibility (and clarity), always have \code{EXPR} as the firstargument, naming it if partial matching is a possibility. For thecharacter-string form, have a single unnamed argument as the defaultafter the named values.}\value{The value of one of the elements of \code{\dots}, or \code{NULL},invisibly (whenever no element is selected).The result has the visibility (see \code{\link{invisible}}) of theelement evaluated.}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth & Brooks/Cole.}\examples{require(stats)centre <- function(x, type) {switch(type,mean = mean(x),median = median(x),trimmed = mean(x, trim = .1))}x <- rcauchy(10)centre(x, "mean")centre(x, "median")centre(x, "trimmed")ccc <- c("b","QQ","a","A","bb")# note: cat() produces no output for NULLfor(ch in ccc)cat(ch,":", switch(EXPR = ch, a = 1, b = 2:3), "\n")for(ch in ccc)cat(ch,":", switch(EXPR = ch, a =, A = 1, b = 2:3, "Otherwise: last"),"\n")## switch(f, *) with a factor fff <- gl(3,1, labels=LETTERS[3:1])ff[1] # C## so one might expect " is C" here, butswitch(ff[1], A = "I am A", B="Bb..", C=" is C")# -> "I am A"## so we give a warning## Numeric EXPR does not allow a default value to be specified## -- it is always NULLfor(i in c(-1:3, 9)) print(switch(i, 1, 2 , 3, 4))## visibilityswitch(1, invisible(pi), pi)switch(2, invisible(pi), pi)}\keyword{programming}