Reputation: 8377
Please someone could explain why there is a difference in the behaviour of the last line of code, when I call a function using its namespace in a pipe, and what the error message actually means:
library(magrittr)
1:5 %>% cumsum
#### [1] 1 3 6 10 15
1:5 %>% cumsum()
#### [1] 1 3 6 10 15
1:5 %>% base::cumsum()
#### [1] 1 3 6 10 15
1:5 %>% (base::cumsum)
#### [1] 1 3 6 10 15
1:5 %>% base::cumsum
#### Error in .::base : unused argument (cumsum)
I genuinely thought that it would work since operator precedence rules state that the highest priority is for the namespace operator ::
, far away from special operators.
Thanks.
Upvotes: 2
Views: 130
Reputation: 1593
I think it is because it calls the description of the function, base::cumsum
returns function (x) .Primitive("cumsum")
which does not take any argument, which is what the error says. Once you add the ()
as you see a line above, it calls the function with the 1:5
argument.
Even better example is with your own function.
foo<-function(){cat("hello")}
then returns its code if called without argument:
> foo
function(){cat("hello")}
So it is similar with base::cumsum
, but I guess it is somehow protected or just programmed that is gives you that description.
Upvotes: 3