简体   繁体   English

在R中解析“ - >”赋值运算符

[英]Parsing “->” assignment operator in R

My question is about parsing expressions in R language. 我的问题是在R语言中解析表达式。 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). 如您所见,所有“ - >”赋值运算符都更改为“< - ”,但不是第一个示例(仅限“fun_tree”)。 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? 我能确定我总是在语法树中得到“< - ”运算符,所以我不能打扰自己实现“ - >”的情况吗?

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 ). 所以是的, ->赋值总是被解析为<- (在调用时一样->作为函数名! 1 )。

Your first display is the other way round because of parse 's keep.source argument : 由于parsekeep.source参数,你的第一个显示是keep.source

> 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: 1调用<-作为函数与将其用作运算符相同:

> 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. 但是,没有->函数(虽然你可以定义一个),并且写b -> a从不调用->函数,它总是被解析为a <- b ,然后调用<-函数或原始。

This is slightly off topic, but I think that a left-to-right assignment operator can be used in cases beyond fast command-line typing. 这稍微偏离主题,但我认为在快速命令行输入之外的情况下可以使用从左到右的赋值运算符。 So, here's one way to define such an operator: 所以,这是定义这样一个运算符的一种方法:

"%op%"<- function(a,b) eval(substitute(b<-a), parent.frame())

I am using this to avoid duplicating R-code when storing/loading a set of matrices and vectors to/from a vector ( Error: object '->' not found in R ). 我正在使用它来避免在向/从向量存储/加载一组矩阵和向量时重复R代码( 错误:在R中找不到对象' - >' )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM