tidyverse / purrr

A functional programming toolkit for R
https://purrr.tidyverse.org/
Other
1.28k stars 272 forks source link

Deprecation of when: can documentation show the equivalent code to previous `when()` use? #1059

Closed MatthieuStigler closed 1 year ago

MatthieuStigler commented 1 year ago

A convenient use of when() was as a conditional pipe, as was shown in the example, and as was advocated on Stack (R Conditional evaluation when using the pipe operator %>%).

Unfortunately, with the deprecation of when() in purrr 1.0.0, while the documentation mentions that one should use if instead, the replacement example does does not correspond to the previous example, where when was used as conditional pipe? Could the documentation seek to provide an equivalent code suggestion?

Thanks!

steffen-stell commented 1 year ago

Can you provide an example where this does not work? The example in the answer advocating the use of when() is:

1:3 %>% 
  purrr::when(sum(.) < 25 ~ sum(.), ~0)

and can easily be replaced with

1:3 %>%
  {if (sum(.) < 25) sum(.) else 0}
MatthieuStigler commented 1 year ago

thanks @steffen-stell My point was not about code "working" but the recommended alternative/best practice being documented. Unfortunately, the current documentation does not provide an equivalent code example for what I believe was a common use of when

hadley commented 1 year ago

I don't think there currently is a good alternative for that very specific use case. In my opinion, once you start adding branching to a pipe, you're starting to make a pipe that's going to be hard for other people to understand.