Error in magrittr pipe when using `` magrittr :: `%>%` ``

For some reason, I played with the magrittr pipe syntax and came across a strange error that occurs when you scope explicitly qualify the %>% call. I know that using the syntax below destroys the purpose of the channel, but I am curious why the error occurs.

The first call to sum works as expected and outputs 1 .

The second call causes an error: Error in pipes[[i]] : subscript out of bounds .

 library(magrittr) `%>%`(1,sum()) magrittr::`%>%`(1,sum()) 

Looking at the source code of the channel, I think that the cause of the error is related to the first lines that control the environment, but I’m sure what the problem is.

 function (lhs, rhs) { parent <- parent.frame() env <- new.env(parent = parent) chain_parts <- split_chain(match.call(), env = env) 

Can anyone explain this behavior?

+5
source share
1 answer

The arguments in the pipe (%>%,% $%, etc.) are all the same pipe() function in magrittr. One of the first things that a function does is to break the call into its component parts using the internal, not exported split_chain function.

split_chain() takes the first call element (the function used in this case, one of the pipe operators) and runs it through another internal unexported function called is_pipe() , which looks like this:

 function(pipe) { identical(pipe, quote(`%>%`)) || identical(pipe, quote(`%T>%`)) || identical(pipe, quote(`%<>%`)) || identical(pipe, quote(`%$%`)) } 

if this does not return as true, the function completes the return of a list that does not have a channel type and the right side of the argument that causes problems. When scoping, a la magrittr::'%>%' , the first part of the call includes an explicit scope definition, and therefore it does not perform these hard coded checks.

+6
source

All Articles