Skip to content

Commit 587244f

Browse files
author
luke
committed
Add support for using _ placeholder as a named argument on pipe RHS.
git-svn-id: https://svn.r-project.org/R/trunk@81858 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 16bcf59 commit 587244f

File tree

4 files changed

+793
-726
lines changed

4 files changed

+793
-726
lines changed

doc/NEWS.Rd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@
224224
225225
\item \code{iconv} now allows \code{sub = "c99"} to use C99-style
226226
escapes for UTF-8 inputs which cannot be converted to encoding \code{to}.
227+
228+
\item In a forward pipe \code{|>} expression it is now possible to
229+
use a named argument with the placeholder \code{_} in
230+
the \code{rhs} call to specify where the \code{lhs} is to be
231+
inserted. The placeholder can only appear once on the \code{rhs}.
232+
227233
}
228234
}
229235

src/library/base/man/pipeOp.Rd

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@
2121
A pipe expression passes, or pipes, the result of the left-hand side
2222
expression \code{lhs} to the right-hand side expression \code{rhs}.
2323

24-
%If the \code{rhs} expression is a call, then the
2524
The \code{lhs} is
2625
inserted as the first argument in the call. So \code{x |> f(y)} is
2726
interpreted as \code{f(x, y)}.
2827

2928
To avoid ambiguities, functions in \code{rhs} calls may not be
3029
syntactically special, such as \code{+} or \code{if}.
3130

31+
It is also possible to use a named argument with the placeholder
32+
\code{_} in the \code{rhs} call to specify where the \code{lhs} is to
33+
be inserted. The placeholder can only appear once on the \code{rhs}.
34+
3235
Pipe notation allows a nested sequence of calls to be written in a way
3336
that may make the sequence of processing steps easier to follow.
3437

@@ -54,10 +57,13 @@ mtcars |> head() # same as head(mtcars)
5457
mtcars |> head(2) # same as head(mtcars, 2)
5558
mtcars |> subset(cyl == 4) |> nrow() # same as nrow(subset(mtcars, cyl == 4))
5659

57-
# passing the lhs into an argument other than the first, either:
60+
# to pass the lhs into an argument other than the first, either
61+
# use the _ placeholder with a named argument:
62+
mtcars |> subset(cyl == 4) |> lm(mpg ~ disp, data = _)
63+
# or use an anonymous function:
5864
mtcars |> subset(cyl == 4) |> (function(d) lm(mpg ~ disp, data = d))()
5965
mtcars |> subset(cyl == 4) |> (\(d) lm(mpg ~ disp, data = d))()
60-
# or explicitly naming the argument(s) before the "one":
66+
# or explicitly name the argument(s) before the "one":
6167
mtcars |> subset(cyl == 4) |> lm(formula = mpg ~ disp)
6268

6369
# the pipe operator is implemented as a syntax transformation:

0 commit comments

Comments
 (0)