moodymudskipper / nakedpipe

Pipe Into a Sequence of Calls Without Repeating the Pipe Symbol.
69 stars 7 forks source link

ggplot2 and piping into binary ops #2

Closed moodymudskipper closed 4 years ago

moodymudskipper commented 4 years ago

We can already do this, which is an improvement over magrittr

library(nakedpipe)
library(ggplot2)
cars %..% {
  head(.)
  ggplot(., aes(speed, dist)) +
    geom_point()
  plotly::ggplotly(.)
}

However this won't work if we replace %..% by %.%, and obviously likewise if we don't give the dots :

cars %.% {
  head()
  ggplot(aes(speed, dist)) +
    geom_point()
  plotly::ggplotly()
}

Because we'd be piping into +.

But + already had 2 arguments here, so it'd make no sense to pipe to it, thus what we can do without breaking code is to pipe to the lhs (recursively) when piping on a binary operator.

:: and ::: are exceptions to that rule, they only accept symbols on the lhs and use NSE so piping to the lhs doesn't make sense, In that case we'll pipe to the rhs, which was an improvement implemented in fastpipe, and a FR by many. Not sure how to treat $ though!

Piping to unary + should still work as this already work and is quite cool :

cars %.% {
  head()
  ggplot(aes(speed, dist))
  +geom_point()
  plotly::ggplotly()
}
moodymudskipper commented 4 years ago

We cannot do this recursive piping efficiently because testing all symbols takes time, and moreover there's no builtin tool to know if %foo% is infix, we have to deparse and regex.

That's good enough that we can use {} and be explicit, and with ggplot we can just go to the nex tine anyway

github-actions[bot] commented 2 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.