Значения разных типов

Поток управления / Условные операторы / Операторы switch/case

new_tiger <- function() {
  structure(list(), class = "Tiger")
}

describe <- function(obj) {
  # The type name is a string to switch on
  switch(class(obj)[1],
         Tiger = "Tiger!!!",
         "NULL" = "Empty value",
         # Empty branch: falls through to the next
         integer = ,
         numeric = "Number",
         character = sprintf("string value '%s'",
                             obj),
         "Unknown type")
}

tiger <- describe(new_tiger())
# tiger = Tiger!!!
text <- describe("test")
# text = string value 'test'
whole <- describe(5L)
# whole = Number
fraction <- describe(3.14)
# fraction = Number
empty <- describe(NULL)
# empty = Empty value
flag <- describe(TRUE)
# flag = Unknown type

# A number picks a branch by position
second <- switch(2, "one", "two", "three")
# second = two

cat("tiger =", tiger, "\n")
cat("text =", text, "\n")
cat("whole =", whole, "\n")
cat("fraction =", fraction, "\n")
cat("empty =", empty, "\n")
cat("flag =", flag, "\n")
cat("second =", second, "\n")