I have a case_when() inside a mutate() and I'd like R to stop and throw an error if the TRUE condition is fulfilled. This is for debugging purposes.
For example, values for mtcars$cyl are 4, 6 or 8. With the proper solution in place in the fourth line, this should be able to run without error:
mtcars %>%
mutate(test = case_when(
cyl > 3 ~ "ok",
TRUE ~ # code for throwing error here
))
This should throw the error:
mtcars %>%
mutate(test = case_when(
cyl < 3 ~ "ok",
TRUE ~ # code for throwing error here
))
I tried stop but this triggers the exception even if TRUE is never fulfilled.