[Rd] [External] Re: should base R have a piping operator ?

Lionel Henry ||one| @end|ng |rom r@tud|o@com
Mon Oct 7 17:22:38 CEST 2019



> On 7 Oct 2019, at 17:04, Tierney, Luke <luke-tierney using uiowa.edu> wrote:
> 
>  Think about what happens if an
> argument in a pipe stage contains a pipe. (Not completely
> unreasonable, e.g. for a left_join).

It should work exactly as it does in a local environment.

```
`%foo%` <- function(x, y) {
  env <- parent.frame()

  # Use `:=` to avoid partial matching on .env/.frame
  rlang::scoped_bindings(. := x, .env = env)

  eval(substitute(y), env)
}

"A" %foo% {
  print(.)
  "B" %foo% print(.)
  print(.)
}
#> [1] "A"
#> [1] "B"
#> [1] "A"

print(.)
#> Error in print(.) : object '.' not found

```

The advantage is that side effects (such as assigning variables or calling
`return()`) will occur in the expected environment. I don't see it causing
problems except in artificial cases. Am I missing something?

I agree that restraining the pipe to a single placeholder (to avoid
double evaluation) would be a good design too.

I can't access https://gitlab.com/luke-tierney/pipes, it appears to be 404.

Best,
Lionel



More information about the R-devel mailing list