Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do backticks do in R?

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("[[") 
like image 905
Megatron Avatar asked Mar 25 '16 13:03

Megatron


People also ask

What does backtick mean in R?

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 .

What is the backtick used for?

The backtick ` is a typographical mark used mainly in computing. It is also known as backquote, grave, or grave accent. The character was designed for typewriters to add a grave accent to a (lower-case) base letter, by overtyping it atop that letter.

How do you add a backtick in R?

As a bonus, you can also set the functions of an Addin to custom keyboard shortcuts and quickly access them from the RStudio command palette (just hit Shift + Cmd + P , or Shift + Ctrl + P , then type the word 'backtick').


2 Answers

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.

like image 62
jaimedash Avatar answered Sep 20 '22 05:09

jaimedash


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

like image 36
Amit Kohli Avatar answered Sep 23 '22 05:09

Amit Kohli