|> vs %>% Why does one work but not the other?

I have been happily using the |> version of pipes since it was made a part of "base" r. However, I just encountered a situation where it did not work and I had to revert back to the %>% pipe code. I don't know why one would work and one wouldn't and I'm curious how I am properly supposed to pass a dataframe to ggplot within pipes.

Code that DIDN'T work:

dataframe |> ggplot( aes(x = x_variable, y = y_variable) ) + geom_col(data = . |> filter(y_variable >= 0), fill = "red") + geom_col(data = . |> filter(y_variable <= 0), fill = "blue")

Code that DID work:
dataframe |> ggplot( aes(x = x_variable, y = y_variable) ) + geom_col(data = . %>% filter(y_variable >= 0), fill = "red") + geom_col(data = . %>% filter(y_variable <= 0), fill = "blue")

There are several differences between the inner workings of |> and %>%, as described here (in particular, %>% is more flexible), and it looks like you're running into one of them.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.