1

Here are the rules for what identifiers can be named in R. It says:

Identifiers consist of a sequence of letters, digits, the period (‘.’) and the underscore. They must not start with a digit or an underscore, or with a period followed by a digit.

But it seems possible to assign variables that do not meet these rules simply by using quotes around the illegal identifier name

E.g. Using an illegal character in an identifier name (in this case, a space)

`sdf sdf` <- 2
`sdf sdf`
# [1] 2

A question mark also works

`sdf?sdf` <- 2
`sdf?sdf`
# [1] 2

Varibles assigned in this way seem to work

`_sdfk` <- 2
`_sdfk` * 9
# [1] 18

It even works for functions

"my funct" <- function(x) { x * 2 }

"my funct"(3)
# [1] 6

According to the rules, these identifiers should not be possible.

Why is R allowing this?

stevec
  • 41,291
  • 27
  • 223
  • 311
  • 3
    See `?Quotes`: "Almost always, other names [than identifiers] 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)" – Henrik May 12 '19 at 14:47
  • @Henrik that explains it. It seems strange that `?Quotes` gives this important information, as do some other sources [e.g.](http://web.mit.edu/r/current/lib/R/library/base/html/Quotes.html) but the [CRAN docs](https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Identifiers) don't – stevec May 12 '19 at 14:56
  • You didn't read the document you cited. It says "Notice also that objects can have names that are not identifiers" and goes on to give examples that don't follow the rules. By the way, that's a base R document; CRAN distributes it, but it is not a "CRAN doc". – user2554330 May 12 '19 at 17:36

0 Answers0