Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing "->" assignment operator in R

My question is about parsing expressions in R language. Let me jump right into an example:

fun_text <- c("
0 -> var
f1 <- function()
{
    0 -> sum_var
    sum_var2 = 0
    sum_var3 <- 0
}

(function()
{
    0 -> sum_var
    sum_var2 = 0
    sum_var3 <- 0
})->f2

f3 = function(x)
{
  0 -> sum_var
  sum_var2 = 0
  sum_var3 <- 0
}

")

fun_tree <- parse(text=fun_text)
fun_tree 
fun_tree[[1]]
fun_tree[[2]]
fun_tree[[3]]
fun_tree[[4]]

After that, we obtain those results:

expression(0 -> var, f1 <- function()
{
    0 -> sum_var
    sum_var2 = 0
    sum_var3 <- 0
}, (function()
{
    0 -> sum_var
    sum_var2 = 0
    sum_var3 <- 0
})->f2, f3 = function(x)
{
    0 -> sum_var
    sum_var2 = 0
    sum_var3 <- 0
})

and

var <- 0

and

f1 <- function() {
    sum_var <- 0
    sum_var2 = 0
    sum_var3 <- 0
}

and

f2 <- (function() {
    sum_var <- 0
    sum_var2 = 0
    sum_var3 <- 0
})

and

f3 = function(x) {
    sum_var <- 0
    sum_var2 = 0
    sum_var3 <- 0
}

As you can see, all "->" assignment operators are changed to "<-", but not in the first example ("fun_tree" only). My question is: why is that? and can I be sure that I always get "<-" operator in syntax tree, so I can do not bother myself in implementing "->" case?

like image 576
bartoszukm Avatar asked Apr 26 '14 10:04

bartoszukm


1 Answers

can I be sure that I always get "<-" operator in syntax tree

Let’s see …

> quote(b -> a)
a <- b
> identical(quote(b -> a), quote(a <- b))
[1] TRUE

So yes, the -> assignment is always parsed as <- (the same is not true when invoking -> as a function name!1).

Your first display is the other way round because of parse’s keep.source argument:

> parse(text = 'b -> a')
expression(b -> a)
> parse(text = 'b -> a', keep.source = FALSE)
expression(a <- b)

1 Invoking <- as a function is the same as using it as an operator:

> quote(`<-`(a, b))
a <- b
> identical(quote(a <- b), quote(`<-`(a, b)))
[1] TRUE

However, there is no -> function (although you can define one), and writing b -> a never calls a -> function, it always gets parsed as a <- b, which, in turn, invokes the <- function or primitive.

like image 128
Konrad Rudolph Avatar answered Oct 19 '22 01:10

Konrad Rudolph