I am wondering how to use the base |> pipe in place of tidyverse's %>% in a situation where there are conditional elements of a dplyr chain. When I try to following code with |>, I get an error:
Error: function '{' not supported in RHS call of a pipe
Example with Tidyverse
library(tidyverse)
condition = FALSE
mtcars %>%
{ if(condition == TRUE)
mutate(., mpg = mpg*1000)
else . }
Example with base R pipe (causes error):
mtcars |>
{ if(condition == TRUE)
mutate(., mpg = mpg*1000)
else . }
Call it via an unnamed function. This is needed as you are using the placeholder on two positions.
mtcars |>
{\(.) if(condition == TRUE)
mutate(., mpg = mpg*1000)
else . }()
Have also a look at What are the differences between R's new native pipe |> and the magrittr pipe %>%?.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With