Search code examples
rmagrittr

magrittr pipe not evaluating a dot passed a second pipe within a function argument


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"

Solution

  • Pipe starting with . generates a function.

    For example, . %>% identity is same as function(.) identity(.).

    Thus,

    # Error
    c('a', 'b', 'c') %>% `names<-`(., . %>% identity())
    

    is regarded as

    c('a', 'b', 'c') %>% `names<-`(., function(.) identity(.))
    

    which means the second argument of names<- is function, not a character vector.

    This is documented in Using the dot-place holder as lhs.

    In order to workaround, try

    c('a', 'b', 'c') %>% `names<-`(., (.) %>% identity())