Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

magrittr

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"
like image 789
Charlie Avatar asked Jul 25 '19 06:07

Charlie


1 Answers

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())
like image 170
atusy Avatar answered Oct 20 '22 06:10

atusy