I am using ifelse with pipe in a scenario where I use the forwarded result in both the condition and ifelse branches. The below is a simplified version: it seems that ifelse + pipe only handles condition different from .
if put inside curly 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 I originally see the idea of putting ifelse inside curly braces there there is no differentiation between using simply .
or using .
in a expression / function call which is the main point of my question.