Skip to content

Native pipe: Workaround for piping to syntactically special expression, e.g. for, if, ... #179

Description

@HenrikBengtsson

Issue

We have:

> e1 <- quote(for (x in 42) x)
> str(e1)
 language for (x in 42) x

but we piping does not work here:

> e2 <- for (x in 42) x |> quote()
> str(e2)
 NULL

Troubleshooting

This is because:

> e2 <- for (x in 42) x |> quote()

is equivalent to:

> e2 <- for (x in 42) quote(x)

This might be what help("|>") refers to when it says:

To avoid ambiguities, functions in rhs calls may not be syntactically special, such as + or if.

Solution

The only way we can quote the full expression is if we wrap it in { ... } or ( ... ), e.g.

> e3 <- { for (x in 42) x } |> quote()
> str(e3)
 language for (x in 42) x
 - attr(*, "srcref")=List of 2
  ..$ : 'srcref' int [1:8] 1 7 1 7 7 7 1 1
  .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x6423c5c86a78> 
  ..$ : 'srcref' int [1:8] 1 9 1 23 9 23 1 1
  .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x6423c5c86a78> 
 - attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x6423c5c86a78> 
 - attr(*, "wholeSrcref")= 'srcref' int [1:8] 1 0 1 25 0 25 1 1
  ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x6423c5c86a78> 

and

> e4 <- ( for (x in 42) x ) |> quote()
> str(e4)
 language, mode "(": (for (x in 42) x)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions