Direct arrow operator not found

I'm confused about nature ->

, I can't get it defined as much as I can with other operators and it doesn't behave like <-

. See below:

print(`<-`) # .Primitive("<-")
print(`->`) # Error in print(`->`) : object '->' not found

      

Also, I cannot grab it, although R won't trigger any errors if I try:

`->` = `+`  # attempting to hijack `->`, no error
print(`->`) # function (e1, e2)  .Primitive("+"), seems like it worked
1 -> 3      # Error in 3 <- 1 : invalid (do_set) left-hand side to assignment
1 -> test1
print(test1) # 1, hijacking failed
`->`(1,3)   # 4, this works

      

With <-

(or whatever operator I've tried), I can do this:

`<-` = `+`   
print(`<-`)
1 <- 3      # 4
1 <- test2  # Error: object 'test2' not found

rm(list=ls()) # back to sanity

      

So what's going on?

+3


source to share


2 answers


> e <- quote(42 -> x)
> e
x <- 42

      



There is only one assignment operator in R: <-

(well, two: there =

, but let's not complicate things). The parser interprets the character " ->

" as a destination and creates an expression as if it were used <-

.

+4


source


More comment than answer, but it may have been a long time.

It seems to ->

be being parsed by a parser that detects the left and right sides of the job and then calls <-

. After your hacks:

`<-` = `+`
1 -> 3
#[1] 4

      



See what happened? The operator has ->

little to no time to act, since the parser doesn't resolve it unless you explicitly call it:

`->` = `+`
`->`(5,6)
#[1] 11

      

+2


source







All Articles