I use ifelse with pipe in a scenario where I use a redirected result in both state and ifelse. The following is a simplified version: it seems that ifelse + pipe is processing a condition other than . if put inside braces.
library("magrittr") FALSE %>% ifelse(., 'true', 'false') #> [1] "false" FALSE %>% ifelse(. == TRUE, 'true', 'false') #> Error in ifelse(., . == TRUE, "true", "false"): unused argument ("false") FALSE %>% {ifelse(. == TRUE, 'true', 'false')} #> [1] "false"
My original goal:
library("magrittr") NULL %>% ifelse(is.null(.), "", as.character(.)) #> Error in ifelse(., is.null(.), "", as.character(.)): unused argument (as.character(.)) NULL %>% {ifelse(is.null(.), "", as.character(.))} #> [1] ""
Using {} is good enough for me, but I would like to understand the reasons for this behavior.
Edit: Although this question discusses a related topic, and initially I see the idea of ββplacing ifelse inside curly braces there is no difference between using it simply . or using . into an expression / function call, which is the highlight of my question.
r magrittr
Ildi Czeller
source share