15

I found out that some expressions can only be piped if inside curly brackets (braces, { }), for instance:

library(dplyr)
3 %>% {3 + .}
3 %>% {ifelse(. < 2, TRUE, FALSE)}

What are the rules behind the use of curly brackets when piping? When should we use it and with which expressions?

Henrik
  • 65,555
  • 14
  • 143
  • 159
mat
  • 2,412
  • 5
  • 31
  • 69
  • 4
    The curly brace appears to suppress the pipe from using the first argument in a function. http://qiita.com/aakansh9/items/c2c4d3f653162778b757 So to answer your question, whenever you want the passing of first argument to a function suppressed? :) – Roman Luštrik Mar 06 '17 at 10:55
  • 2
    This is quite clearly documented. See `?"%>%"` under _Using the dot for secondary purposes_. Also see [here](http://stackoverflow.com/questions/42385010/map-for-nested-data-frames/42386886#42386886). – Axeman Mar 06 '17 at 11:00
  • @Axemanthank you, wans't aware of `?"%>%"` – mat Mar 06 '17 at 11:18
  • 2
    @Axeman and Roman, This is a fairly common question, but we can't use it as a dupe target because it doesn't have a 'real' answer. Would either of you mind to compile your comments as an answer? Cheers. – Henrik Jan 04 '18 at 20:32
  • 2
    @Roman Luštrik Couldn't ping both you and Axeman in the post above, so here we go. I know the `{}` topic has been elaborated in other posts, but I reckon this question is most to the point. – Henrik Jan 04 '18 at 20:32
  • @Henrik, I think I basically wrote my answer in the link above. Can one link to that, perhaps? – Axeman Jan 04 '18 at 22:59
  • 1
    @Axeman then maybe hammer as dupe? – zx8754 Jan 04 '18 at 23:22

0 Answers0