Equivalent to switch in tidyverse running a list of commands

Since you're using case_when() this is already vectorized. The code above works (at least for me).

library(tidyverse)
x <- Sys.info() %>% pluck("sysname")
case_when(x == "Linux" ~{
  (a <- "L1")
  (b <- "L2")
},
x == "Windows" ~  {
  (c <- "W1")
  (d <- "W2")
},
TRUE ~ {
  ("Not Linux or Windows")
})
#> [1] "Not Linux or Windows"

Created on 2018-11-06 by the reprex package (v0.2.1.9000)
(Note that in order to create a reprex, I don't actually call the reprex library, reprex is run on the code itself. See the community reprex FAQ for more details).

1 Like