switchCase {switchcase} | R Documentation |
The switch-case construct
Description
switchCase()
provides the functionality of a typical
switch-case statement as it is available in other programming languages.
Usage
switchCase(expr, ..., break.case = TRUE)
Arguments
expr |
The expression which is evaluated and the value of which is tested in the alternative 'case' code branches. |
... |
The alternative code branches ('case' blocks). Each alternative is
produced with the |
break.case |
An optional logical value with default |
Details
The expr
argument is the expression that is evaluated and
tested. Using the ..expr
placeholder, this expression can be used in
the first element (the test condition) of an alternative branch provided
via the ...
argument. If, for example, the first element of an
alternative branch is ..expr > 10
then the condition of this
alternative 'case' will be met if the value of swichCase()
's
expr
argument is larger than 10
. Each alternative 'case'
branch can return a value if its condition is fulfilled. This value needs
to be provided as the third element of the alternative 'case' branch (the
third argument of the alt()
function that is used to create
alternative branches) and is NULL
by default resulting in no return
whatsoever. If multiple branches are executed (e.g. because
break.case = FALSE
) and want to return a value then only the 'case'
branch that is execxuted last will return its value. A default
'case' which is executed if no other alternative branch of the switch-case
statement is applicable can be modeled by setting the first element of an
alternative (which would normally contain the test condition) to
NULL
. If multiple defaults are found, only the first one is
executed. Technical note: All code is executed in the enviroment from which
switchCase()
is called so it is easy to access variables and
functions from there.
Value
The return value of the 'case' alternative that is executed last. If there is no applicable alternative or no applicable alternative returns a value then nothing is retuned.
See Also
Other switchcase:
alt()
Examples
# Calculation and interpretation of a person's body-mass index (mass in kilograms)
bmi <- function(mass, size) {
ind <- mass / size^2
switchCase(
ind,
alt(
..expr <= 15,
{ cat("Your body mass index is ", ind, " which is very severely underweight.\n") },
"very severely underweight"
),
alt(
..expr > 15 & ..expr <= 16,
{ cat("Your body mass index is ", ind, " which is severely underweight.\n") },
"severely underweight"
),
alt(
..expr > 16 & ..expr <= 18.5,
{ cat("Your body mass index is ", ind, " which is underweight.\n") },
"underweight"
),
alt(
..expr > 18.5 & ..expr <= 25,
{ cat("Your body mass index is ", ind, " which is normal.\n") },
"normal"
),
alt(
..expr > 25 & ..expr <= 30,
{ cat("Your body mass index is ", ind, " which is overweight.\n") },
"overweight"
),
alt(
..expr > 30 & ..expr <= 35,
{ cat("Your body mass index is ", ind, " which is moderately obese.\n") },
"moderately obese"
),
alt(
..expr > 35 & ..expr <= 40,
{ cat("Your body mass index is ", ind, " which is severely obese.\n") },
"severly obese"
),
alt(
..expr > 40,
{ cat("Your body mass index is ", ind, " which is Very severely obese.\n") },
"very severly obese"
)
)
}
bmi.person1 <- bmi(82.5, 1.79)
cat("Person1 turned out to be ", bmi.person1)