Function to check a variable using pattern matching.
The variable to pattern-match
Pairs of one pattern (value or list or magrittr sequence) and one result function
variable matches a
is called. For comparing optional types, it is a better habit to
match_with than a conditional statement.
pattern can be either:
an object or a primitive type (direct comparison with
a list (match if
variable is in the list),
magrittr functional sequence that matches if it returns
variable . The dot
. denotes the variable to be matched.
result-function takes no arguments, it will be called as is. Else, the only argument that will be sent is
You can also use the fallthrough function
fallthrough() to permit the matching to continue even if the current pattern is matched.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
library(magrittr) a <- 5 match_with(a, . %>% option(.), paste, none, function() "Error!" ) ##  5 match_with(a, 1, function() "Matched exact value", list(2, 3, 4), function(x) paste("Matched in list:", x), . %>% if (. > 4) ., function(x) paste("Matched in condition:", x) ) ##  "Matched in condition: 5"
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.