What do backticks do in R?

Megatron picture Megatron · Mar 25, 2016 · Viewed 13k times · Source

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("[[")

Answer

jaimedash picture jaimedash · Mar 26, 2016

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.