Dirk Eddelbuettel
2021-Jan-12 20:00 UTC
[Rd] [External] brief update on the pipe operator in R-devel
On 12 January 2021 at 20:38, I?aki Ucar wrote: | On Tue, 12 Jan 2021 at 20:23, <luke-tierney at uiowa.edu> wrote: | > | > After some discussions we've settled on a syntax of the form | > | > mtcars |> subset(cyl == 4) |> d => lm(mpg ~ disp, data = d) | > | > to handle cases where the pipe lhs needs to be passed to an argument | > other than the first of the function called on the rhs. This seems a | > to be a reasonable balance between making these non-standard cases | > easy to see but still easy to write. This is now committed to R-devel. | | Interesting. Is the use of "d =>" restricted to pipelines? In other | words, I think that it shouldn't be equivalent to "function(d)", i.e., | that this: | | x <- d => lm(mpg ~ disp, data = d) | | shouldn't work. Looks like your wish was already granted: > mtcars |> subset(cyl == 4) |> d => lm(mpg ~ disp, data = d) Call: lm(formula = mpg ~ disp, data = subset(mtcars, cyl == 4)) Coefficients: (Intercept) disp 40.872 -0.135 > d => lm(mpg ~ disp, data = d) Error in `=>`(d, lm(mpg ~ disp, data = d)) : could not find function "=>" > x <- d => lm(mpg ~ disp, data = d) Error in `=>`(d, lm(mpg ~ disp, data = d)) : could not find function "=>" > Dirk -- https://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
Bill Dunlap
2021-Jan-12 20:52 UTC
[Rd] [External] brief update on the pipe operator in R-devel
'=>' can be defined as a function. E.g., it could be the logical "implies" function: > `=>` <- function(x, y) !x | y > TRUE => FALSE [1] FALSE > FALSE => TRUE [1] TRUE It might be nice then to have deparse() display it as an infix operator instead of the current prefix: > deparse(quote(p => q)) [1] "`=>`(p, q)" There was a user who recently wrote asking for an infix operator like -> or => that would deparse nicely for use in some sort of model specification. When used with |>, the parser will turn the |> and => into an ordinary looking function call so deparsing is irrelevant. > deparse(quote(x |> tmp => f(7,arg2=tmp))) [1] "f(7, arg2 = x)" -Bill On Tue, Jan 12, 2021 at 12:01 PM Dirk Eddelbuettel <edd at debian.org> wrote:> > On 12 January 2021 at 20:38, I?aki Ucar wrote: > | On Tue, 12 Jan 2021 at 20:23, <luke-tierney at uiowa.edu> wrote: > | > > | > After some discussions we've settled on a syntax of the form > | > > | > mtcars |> subset(cyl == 4) |> d => lm(mpg ~ disp, data = d) > | > > | > to handle cases where the pipe lhs needs to be passed to an argument > | > other than the first of the function called on the rhs. This seems a > | > to be a reasonable balance between making these non-standard cases > | > easy to see but still easy to write. This is now committed to R-devel. > | > | Interesting. Is the use of "d =>" restricted to pipelines? In other > | words, I think that it shouldn't be equivalent to "function(d)", i.e., > | that this: > | > | x <- d => lm(mpg ~ disp, data = d) > | > | shouldn't work. > > Looks like your wish was already granted: > > > mtcars |> subset(cyl == 4) |> d => lm(mpg ~ disp, data = d) > > Call: > lm(formula = mpg ~ disp, data = subset(mtcars, cyl == 4)) > > Coefficients: > (Intercept) disp > 40.872 -0.135 > > > d => lm(mpg ~ disp, data = d) > Error in `=>`(d, lm(mpg ~ disp, data = d)) : could not find function "=>" > > x <- d => lm(mpg ~ disp, data = d) > Error in `=>`(d, lm(mpg ~ disp, data = d)) : could not find function "=>" > > > > Dirk > > -- > https://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >[[alternative HTML version deleted]]