Closed balwierz closed 5 years ago
The behavior you observe is as expected.
The rule is that an expression like
x %>% f(<STUFF>)
is equivalent to
. <- x; f(<STUFF>)
whenever .
appears as an argument of f(<STUFF>)
, otherwise
. <- x; f(., <STUFF>)
whenever .
does not appear as an argument of f(<STUFF>)
. (The assignment . <- x
happens in a temporary child environment. You can think of it as happening in a call to local()
.)
Thus x %>% .[1]
has the same effect as . <- x; .[1]
, because the RHS .[1]
is actually the call `[`(., 1)
(first case), while x %>% c(.[1])
has the same effect as . <- x; c(., .[1])
(second case).
Incidentally, x %>% .[1]
(first case) is the same as x %>% `[`(1)
(second case).
Thanks for the explanation
Another way to put it is that
c("a", "b", "c") %>% .[1]
translates to
`[`(c("a", "b", "c"), 1)
and
c("a", "b", "c") %>% c(.[1])
translates to
c(c("a", "b", "c") , c("a", "b", "c")[1])
the above is not actually what happens, so the lhs expression is only evaluated once.
This is correct:
This is unexpected:
I don't uderstand what has happened here. If the pipe is evaluated first,
c("a", "b", "c") %>% c
gives a,b,c, but thenc("a", "b", "c") (.[1])
should be syntax error. If . is substituted then it isc("a", "b", "c") c("a", "b", "c")[1]
, which is still a syntax error.Yes,
c("a", "b", "c") %>% {c(.[1])}
works fine. IMHO,c("a", "b", "c") %>% c(.[1])
should be either the expected behaviour or a syntax error with a warning to use parentheses. Otherwise it can cause a lot of trouble.