Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not possible to use special ( ` ) functions with base pipe in R

Tags:

r

pipe

With the magrittr pipe, one could use backticks ( ` ) to include special functions1 in the pipe chain. For example:

library(magrittr)
c(7, 6, 5) %>% sort() %>% `[`(1)
#> [1] 5

# and

3 %>% `-`(4)
#> [1] -1

are the same as

v <- c(7, 6, 5)
v <- sort(v)
v[1]
#> [1] 5

# and

3 - 4 
#> [1] -1

However, the native R pipe |> does not (yet?) allow to do that. For example:

c(7, 6, 5) |> sort() |> `[`(1)
#> Error: function '[' not supported in RHS call of a pipe

# and

3 |> `-`(4)
#> Error: function '-' not supported in RHS call of a pipe

Is there a reason why this has not been technically implemented (in case, what is the reason and are there plans to change this?) and are there any workarounds that are not too tortuous?


1 I don't know how to correctly refer to those functions (behind operators such as + or []) that need backticks to be called in their standard function form. Please edit where I say "special functions" if a more appropriate expression exists.

like image 521
Matteo Avatar asked Dec 05 '22 08:12

Matteo


1 Answers

The help file for the native pipe operator |> states:

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

So it is not that the [ function is not implemented in the pipe, but rather that the [ symbol has been specifically prevented from being used in the pipe to prevent parsing ambiguities. There is certainly a case for arguing that the syntax '['(op1, op2) or '-'(op1, op2) could be confusing for new users, and results in code that is less clean and idiomatic. There may be more to it than that - perhaps some edge cases where there would be genuine parsing ambiguity, but I can't think of any, as long as backticks are used around special symbols.

Anyway, this means you can define a function as an alias for [ (a bit like magrittr's extract), for example:

obtain <- `[`

c(7, 6, 5) |> sort() |> obtain(1)
#> [1] 5

takeaway <- `-`

c(7, 6, 5) |> sort() |> takeaway(1)
#> [1] 4 5 6
like image 181
Allan Cameron Avatar answered Jan 10 '23 07:01

Allan Cameron