As an intellectual exercise it can be reasonable to discuss these ways to use a 
pipe even in places where it may not have been seen as something anyone would 
even try to use it.

In actual code, it is often better to not make overly cute constructions that 
others (or yourself a month later) will not understand.

Pipes were not part of R originally and the versions created over the years 
have included many kinds of functionality that is not in the new official pipe 
and that might allow functionality such as this. In some ways, the tidyverse 
evolved so that they did not require this game as their normal method of 
changing the names of columns works inline by using verbs such as rename(). 
Other things you can often do in-line is to reorder the columns or apply 
changes selectively to columns whose names or contents match your patterns. 

If you now wanted to use base R to make similar changes in a pipeline, the 
result may be that you end up reinventing extensions such as functions that do 
what you want in a pipeline, OR you realize that many things done in just base 
R should continue being done in more discrete lumps rather than one huge 
pipeline.

There may well already be one or more packages outside the tidyverse that 
provide such extensions but I am not so sure that using them will be as easy or 
convenient or readable until and unless they are as well-known. I note that 
even in the tidyverse, many things are often better done, especially while 
testing the code, without really long pipes so that it is easier to modify and 
rearrange things or see intermediate values. Similar arguments apply to 
something like using ggplot() with its own sort-of piping where it may make 
sense to use repeated invocations of "p <- p + function(args)" so each can 
clearly be documented with comments and sometimes steps can selectively be 
commented out or moved later in the "pipeline" if it seems the changes by one 
step are interfering with a later step by re-setting internal variables. Of 
course, if the code is completely done and not expected to change, you can 
always switch to piping if that is what you want.

Programming languages can have many purposes including things like efficiency 
or compact representations or making it harder to make mistakes but a major 
advantage of some is that the programs be READ easily without having to consult 
gurus or try to debug. Pipes can both be helpful in this regard or be 
absolutely mysterious. Using "_" in any way imaginable as a placeholder is 
convenient and allowing a default of it being a replacement for a first 
argument without specifying it is nice. But you can imagine an implementation 
where you constantly put in ".placeHolder." as clearer.


-----Original Message-----
From: R-help <r-help-boun...@r-project.org> On Behalf Of Deepayan Sarkar
Sent: Sunday, July 21, 2024 1:08 AM
To: Bert Gunter <bgunter.4...@gmail.com>
Cc: R-help <R-help@r-project.org>
Subject: Re: [R] [External] Using the pipe, |>, syntax with "names<-"

The main challenge in Bert's original problem is that `[` and `[<-` cannot
be called in a pipeline. The obvious solution is to define named versions,
e.g.:

elt <- `[`
`elt<-` <- `[<-`

Then,

> z <- data.frame(a = 1:3, b = letters[1:3])
> z |> names() |> elt(2)
[1] "b"
> z |> names() |> elt(2) <- "foo"
> z
  a foo
1 1   a
2 2   b
3 3   c

You could actually also do (using a similar function already defined in
methods)

z |> names() |> el(2) <- "bar"

Iris's _ trick is of course a nice alternative; and this example in ?pipeOp
already covers it:

# using the placeholder as the head of an extraction chain:
mtcars |> subset(cyl == 4) |> lm(formula = mpg ~ disp) |> _$coef[[2]]

While the replacement question is a nice exercise, I am not sure about the
value of emphasizing that you can use pipes to do complex assignments.
Doesn't that defeat the whole purpose of piping? For one thing, it will
necessarily terminate the pipe. Also, it will not work if the starting
value is not a variable. E.g.,

> data.frame(a = 1:3, b = letters[1:3]) |> names() |> _[2] <- "bar"
Error in names(data.frame(a = 1:3, b = letters[1:3]))[2] <- "bar" :
  target of assignment expands to non-language object

Duncan's rename() approach, which will just change the column name and
return the modified object, seems more useful as part of a pipeline.

Best,
-Deepayan

On Sun, 21 Jul 2024 at 04:46, Bert Gunter <bgunter.4...@gmail.com> wrote:

> I second Rich's excellent suggestion.
>
> As with all elegant solutions, Iris's clicked on the wee light bulb in
> my brain, and I realized that a slightly more verbose, but perhaps
> more enlightening, alternative may be:
>
> z |>  attr("names") |> _[2] <- "foo"
>
> However, I would add this as an example *only with* Iris's solution.
> Hers should be shown whether or not the above is.
>
> Cheers,
> Bert
>
> On Sat, Jul 20, 2024 at 3:35 PM Richard M. Heiberger <r...@temple.edu>
> wrote:
> >
> > I think Iris's solution should be added to the help file: ?|>
> > there are no examples there now that show assignment or replacement
> using the "_"
> >
> > > On Jul 20, 2024, at 18:21, Duncan Murdoch <murdoch.dun...@gmail.com>
> wrote:
> > >
> > > On 2024-07-20 6:02 p.m., Iris Simmons wrote:
> > >> z <- data.frame(a = 1:3, b = letters[1:3])
> > >> z |> names() |> _[2] <- "foo"
> > >> z
> > >
> > > That's a great suggestion!
> > >
> > > Duncan Murdoch
> > >
> > > ______________________________________________
> > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide
> http://www.r-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> >
>
> ______________________________________________
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to