I expected that the result of the case_when would be the same as the result of evaluating the relevant RHS. In my example, the second LHS is true so I expect the value to be x[2] which is 2. Instead, case_when seems to be evaluating every RHS, deciding that the result should have length 2, and extending the value to c(2, 2).
My actual use case was formatting a result which has varying number of values. Here is an example which is closer to what I was doing:
format_x = function(x) {
dplyr::case_when(
length(x)==1 ~ as.character(x),
length(x)==2 ~ paste(x[1], 'and', x[2])
)
}
format_x(2)
#> [1] "2"
format_x(1:2)
#> [1] "1 and 2" "1 and 2"
The repeated result for format_x(1:2) is confusing and not what I intended.
I don't see the relevance of the linked issues and SO, they all seem to have to do with NSE which I am not using here.