What Do Backticks Do in R

What do backticks do 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. 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.

in R what is this bracket `[` with the back ticks

A function in R is invoked using fun(arg1, arg2, ...) but certain functions can be invoked using different syntax. When we write BOD[1, 2] what we are really doing is invoking the [ function on the arguments BOD, 1 and 2 and this can be alternately written as a normal function invocation. Because [ uses a special character that is not normally allowed in object names we must surround it with backticks to tell R to regard it as a name. It can also be specified as a constant string. Thus these are all the same:

BOD[1, 2]
`[`(BOD, 1, 2) # same
"["(BOD, 1, 2) # same
'['(BOD, 1, 2) # same

examples

Here are other examples of this:

1 + 2
`+`(1, 2) # same

3 %in% 2:4
`%in%`(3, 2:4) # same

if (2 > 3) 4 else 5
`if`(2 > 3, 4, 5) # same

getAnywhere

We can find the code of a function using getAnywhere like this:

getAnywhere(`[`)

but in this case it is just a primitive so we get:

A single object matching ‘[’ was found
It was found in the following places
package:base
namespace:base
with value

.Primitive("[")

Actually, in this case what [ does when the first argument is a data frame is to invoke [.data.frame and that one has R source so we do this to see its source:

getAnywhere(`[.data.frame`)

In some cases getAnywhere finds several occurrences of a name. In that case it will tell you where it found each and to get the ith use getAnywhere(...)[i] where ... is the name you are looking for.

Difference between backticks and quotes in aes function in ggplot

Back ticks are the standard way of denoting a non-standard variable name in R. Quotes are used to indicate a string. Example:

`bad name` = 1
`bad name`
# [1] 1

This doesn't work with quotes.

"bad name" = 1
"bad name"
# [1] "bad name"

Generally, you shouldn't use these strange, non-standard names. But, if you ever have to, that's the way to do it. You can do pretty much anything,

`really-bad^name+violating*all()/[kinds] <- of == rules&!|` = 1
# works just fine

but that doesn't mean you should.


When it comes to ggplot, if you did

ggplot(mtcars, aes(x = wt, y = 1)) + geom_point()

you would expect that all the y-values would be 1. And you'd be right!

With a quoted string it's just the same:

ggplot(mtcars, aes(x = wt, y = "mpg")) + geom_point()

except instead of a numeric as in the y = 1 case above, you've given it a character - which is implicitly converted to a factor (with only one level) for a discrete y scale (with only one value). It doesn't matter if there's a column named "mpg" or not, because you've just passed aes() a value. ggplot doesn't look for a column named mpg like it doesn't look for a column named 1 in the first example.

With back ticks, you give ggplot something R recognizes as an object name, not just a value like 1 or "some string". So ggplot does go looking for a column with that name.

# both of these work
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
ggplot(mtcars, aes(x = wt, y = `mpg`)) + geom_point()

While back ticks do work, and setting constants inside aes() usually does work, neither of these are very recommended. The preferred way to set constants is to set constants outside aes(). This is the only way to guarantee everything will work nicely in more complicated plots. Facets, in particular, often have errors or don't produce expected results if you try to do weird stuff inside aes() (especially transformations).

# better than above, set a constant outside of `aes()`
# Here I set y as a constant which is a bit unusual
ggplot(mtcars, aes(x = wt)) + geom_point(y = 1)
# aesthetics that are more commonly set to constants are
# size, color, fill, etc.

For non-standard column names, aes_string() works well, and then it expects the aesthetic mappings to be quoted column names. This also is a good way to do things if you are writing a function that creates ggplots and needs to take column names as arguments.

ggplot(mtcars, aes_string(x = "wt", y = "mpg")) + geom_point()
# or, in a variable
my_y_column = "mpg"
ggplot(mtcars, aes_string(x = "wt", y = my_y_column)) + geom_point()

One more nice example, beginning to look under-the-hood, thanks to @TheTime:

Eventually, ggplot needs to evaluate everything, which will be done with eval. Consider the following:

a <- 1

eval(parse(text="a"))
# [1] 1

eval(parse(text='"a"'))
# [1] "a"

eval(parse(text="`a`"))
# [1] 1

Avoiding backtick characters with dplyr

You can use as.name() with select_():

colName <- "__ID"
df <- data.frame(`__ID` = c(1,2,3), `123` = c(4,5,6), check.names = FALSE)
select_(df, as.name(colName))

What does the backtick - ` - do in a command line invocation specifically with regards to Git commands?

Bash takes the content of the backticks and runs another bash process with that as a command.
Another (prefered) way is $( ... ).

To include backticks, use single quotes for your string, not " but '.

What is the difference between backticks (``) & double quotes () in golang?

In quotes "" you need to escape new lines, tabs and other characters that do not need to be escaped in backticks ``. If you put a line break in a backtick string, it is interpreted as a '\n' character, see https://golang.org/ref/spec#String_literals

Thus, if you say \n in a backtick string, it will be interpreted as the literal backslash and character n.

a := "\n" // This is one character, a line break.
b := `\n` // These are two characters, backslash followed by letter n.

backticks in variable name

All your attempts end in various errors because ?Quotes says that you can only escape the characters listed in the (included) table; and "`" is not in that table.

You could use the hex, octal, etc. representation of "`" though:

> `a\x60b`  # hex
[1] 5
> `a\140b` # octal
[1] 5


Related Topics



Leave a reply



Submit