When using dot a second time to reuse the data on the left of a pipe, passing the dot to a function . %>% f()
is different to putting the dot inside the function brackets f(.)
. Why is this?
Debugging the %>% operator shows that . %>% identity() evaluates to a functional sequence rather than a character vector, which causes names<-
to fail. I don't know how to force the evaluation of this.
# Error
c('a', 'b', 'c') %>% `names<-`(., . %>% identity())
# Works
c('a', 'b', 'c') %>% `names<-`(., identity(.))
c('a', 'b', 'c') %>% `names<-`(., . %>% identity())
Error in as.vector(x, "character") : cannot coerce type 'closure' to vector of type 'character'
c('a', 'b', 'c') %>% `names<-`(., identity(.))
# a b c
#"a" "b" "c"