3

R does not complain when one runs:

library(magrittr)
f <- function(x){x} %>% f

(but running f(1) throws a C stack error).

However, isn't it equivalent to:

f <- f(function(x){x})

which throws the error f not found?

Why does the first command not throw an error?

Boann
  • 48,794
  • 16
  • 117
  • 146
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225

3 Answers3

4

function binds less strongly than the infix operator %>%. This means that your code is equivalent to

f <- function(x) ({x} %>% f)

And not to

f <- (function(x) {x}) %>% f

The latter does indeed raise the error you’re expecting.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
3

This is equivalent to :

f <- function(x) f(x)

and results in an infinite recursive call

Waldi
  • 39,242
  • 6
  • 30
  • 78
2

Ah I understand:

> body(f)
{
    x
} %>% f

In fact this is equivalent to f <- function(x){{x} %>% f}.

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225