# by single
switch_params(c("j", "m", "b"), j = 10, b = 2, m = 13)
# match with TRUE
switch_case(
1:10 == 9 ~ NA_integer_,
1:10 %% 3 == 0 ~ 1:10,
1:10 %% 4 == 0 ~ 11:20,
1:10 %% 5 == 0 ~ 21:30,
1:10 %% 2 == 0 ~ 31:40,
.default = -1L
)
# match within a vector
switch_in_case(
c(1, 2, 12, 4, 20, 21),
1:10 ~ 1,
11:20 ~ 2
)
switch_in_case(
c("a", "b", "d", "e", "g", "j"),
letters[1:3] ~ "a",
letters[5:6] ~ "e"
)
use_these <- c(1, 3, 2, 5)
switch_in_case(
1:10,
use_these ~ TRUE,
.default = FALSE
)
ne <- new.env()
ne$use_these2 <- use_these
# error
try(switch_in_case(
1:10,
use_these2 ~ TRUE
))
switch_in_case(
1:10,
use_these2 ~ TRUE,
.envir = ne
)
switch_in_case(
seq.int(1, 60, 6),
1:10 ~ "a",
11:20 ~ "b",
c(22, 24, 26) ~ "c",
30:Inf ~ "d"
)
# Use functions
switch_in_case(
1:6,
c(1, 3, 5) ~ exp,
c(2, 4) ~ log
)
Run the code above in your browser using DataLab