语法名称不使用字符串引号的原因是什么?

GKi*_*GKi 17 r

我对语法名称函数名称使用了双引号,并得到了使用反引号的注释。在这里,我得到一条评论,将函数名称作为字符串传递给match.fun(以及*apply函数 or do.call)是完全可以的。

A <- matrix(1:4, 2)
B <- matrix(4:1, 2)
apply(A, 2, `*`, B)  # Works: backtick quotes
apply(A, 2, "*", B)  # Works: double quotes
apply(A, 2, '*', B)  # Works: single quotes
# apply(A, 2, *, B)  # Error: unexpected '*' in "apply(A, 2, *"

`%x%` <- function(lhs, rhs) lhs * rhs  # Works: backtick quotes
"%x%" <- function(lhs, rhs) lhs * rhs  # Works: double quotes
'%x%' <- function(lhs, rhs) lhs * rhs  # Works: single quotes
# %x% <- function(lhs, rhs) lhs * rhs  # Error: unexpected SPECIAL in "%x%"
Run Code Online (Sandbox Code Playgroud)

我想知道使用单引号或双引号作为函数名称而不是反引号有什么缺点?在什么情况下应该使用什么报价类型?'"`

nic*_*ola 18

It has to be stressed that

`*`
Run Code Online (Sandbox Code Playgroud)

and

"*"
Run Code Online (Sandbox Code Playgroud)

by themselves are two different objects: the former is a function, while the latter is just a character vector. Whether using one or the other makes any difference, it depends on the use case. Since in most cases when you pass a function to another function, usually match.fun is invoked (and this happens to *apply, do.call and basically any base function that accepts functions as arguments), passing either object does not make any difference. However, if you use some function from external packages or other sources, you cannot be sure that a call to match.fun is performed. For example, say that you have this function:

ex_fun<-function(a, b, FUN) {
    return(FUN(a, b))
}
Run Code Online (Sandbox Code Playgroud)

This works:

ex_fun(1, 3, `*`)
#[1] 3
Run Code Online (Sandbox Code Playgroud)

This does not:

ex_fun(1, 3, "*")
#Error in FUN(a, b) : could not find function "FUN"
Run Code Online (Sandbox Code Playgroud)

Other point: everything in R is a function, even assignment. So when you use something like:

var <- value
Run Code Online (Sandbox Code Playgroud)

The above instruction is transformed by the parser as:

`<-`(var, value)
Run Code Online (Sandbox Code Playgroud)

In this function, the parser allows var to be quoted, as for documentation, so this syntax is valid:

"foo" <- 3 * 3
foo
#[1] 9
Run Code Online (Sandbox Code Playgroud)

But as before, foo and "foo" remain different objects.

match.fun调用函数时会发生另一种隐式用法。当我们将符号视为函数时,表达式的计算会查找函数而不是通用对象。例如:

a <- 4
a(2)
#Error in a(2) : could not find function "a"
Run Code Online (Sandbox Code Playgroud)

错误消息很明确:这不是因为它a不是函数对象,而是因为模式 function name 的对象a不存在。例如,我们可以声明像基函数一样命名的对象,并且解析器在调用时仍然会调用该函数:

log <- 7
log(2) 
#[1] 0.6931472
log
#[1] 7
Run Code Online (Sandbox Code Playgroud)

当解析器知道我们正在调用一个函数时,它会调用match.fun. 这有效:

"*"(3, 4)
#[1] 12
Run Code Online (Sandbox Code Playgroud)

当然,这并不:

FUN <- "*"
FUN(3, 4)
#Error in FUN(3, 4) : could not find function "FUN"
Run Code Online (Sandbox Code Playgroud)

  • “对 var 执行对 as.symbol 的隐式调用,因此此语法是有效的”。我相信这是不正确的。这些只是解析器接受的两种语法。在这两种情况下都不会创建符号对象 (2认同)
  • “前者是一个函数”不,不是。它只是一个名称(一个符号)。一个函数恰好绑定到该名称。 (2认同)

ben*_*n23 14

来自Hadley Wickham 的《Advanced R》第 2.2.1 节

\n
\n

您还可以"_abc" <- 1使用单引号或双引号(例如)而不是反引号创建非语法绑定,但您不应该\xe2\x80\x99t,因为\xe2\x80\x99 必须使用不同的语法来检索值。在赋值箭头左侧使用字符串的能力是一个历史产物,在 R 支持反引号之前使用。

\n
\n

并来自?Quotes(我的大胆):

\n
\n

标识符由一系列字母、数字、句点 (.) 和下划线组成。它们不能以数字或下划线开头,也不能以句点后跟数字开头。保留字不是有效的标识符。
\n...
\n此类标识符也称为语法名称,可以直接在 R 代码中使用。几乎总是可以使用其他名称,只要它们被引用即可。首选引号是反引号(\xe2\x80\x98\xe2\x81\xa0`\xe2\x81\xa0\xe2\x80\x99),deparse 通常会使用它,但在很多情况下单引号或双引号都可以被使用(作为字符常量通常会被转换为名称)。反引号可能必不可少的地方之一是在公式中分隔变量名称:请参阅公式。

\n
\n