What do backticks do in R?

2019-01-11 10:59发布

I'm trying to understand what backticks do in R.

From what I can tell, this is not explained in the ?Quotes documentation page for R.

For example, at the R console:

"[["
# [1] "[["
`[[`
# .Primitive("[[")

It seem to be returning the equivalent to:

get("[[")

3条回答
看我几分像从前
2楼-- · 2019-01-11 11:37

They are equivalent to verbatim. For example... try this:

df <- data.frame(20a=c(1,2),b=c(3,4))

gives error

df <- data.frame(`20a`=c(1,2),b=c(3,4))

doesn't give error

查看更多
Bombasti
3楼-- · 2019-01-11 11:42

A pair of backticks is a way to refer to names or combinations of symbols that are otherwise reserved or illegal. Reserved are words like if are part of the language, while illegal includes non-syntactic combinations like c a t. These two categories, reserved and illegal, are referred to in R documentation as non-syntactic names.

Thus,

`c a t` <- 1 # is valid R

and

> `+` # is equivalent to typing in a syntactic function name
function (e1, e2)  .Primitive("+")

As a commenter mentioned, ?Quotes does contain some information on the backtick, under Names and Identifiers:

Identifiers consist of a sequence of letters, digits, the period
 (‘.’) and the underscore.  They must not start with a digit nor
 underscore, nor with a period followed by a digit.  Reserved words
 are not valid identifiers.

 The definition of a _letter_ depends on the current locale, but
 only ASCII digits are considered to be digits.     

 Such identifiers are also known as _syntactic names_ and may be
 used directly in R code.  Almost always, other names can be used
 provided they are quoted.  The preferred quote is the backtick
 (‘`’), and ‘deparse’ will normally use it, but under many
 circumstances single or double quotes can be used (as a character
 constant will often be converted to a name).  One place where
 backticks may be essential is to delimit variable names in
 formulae: see ‘formula’.

This prose is a little hard to parse. What it means is that for R to parse a token as a name, it must be 1) a sequence of letters digits, the period and underscores, that 2) is not a reserved word in the language. Otherwise, to be parsed as a name, backticks must be used.

Also check out ?Reserved:

 Reserved words outside quotes are always parsed to be references
     to the objects linked to in the ‘Description’, and hence they are
     not allowed as syntactic names (see ‘make.names’).  They *are*
     allowed as non-syntactic names, e.g. inside backtick quotes.

In addition, Advanced R has some examples of how backticks are used in expressions, environments, and functions.

查看更多
再贱就再见
4楼-- · 2019-01-11 11:51

Here is an incomplete answer using improper vocabulary: backticks can indicate to R that you are using a function in a non-standard way. For instance, here is a use of [[, the list subsetting function:

temp <- list("a"=1:10, "b"=rnorm(5))

extract element one, the usual way

temp[[1]]

extract element one using the [[ function

`[[`(temp,1)
查看更多
登录 后发表回答