2

I wanted to use "help in R" in order to see some information about some commands such as "for", "if", "while", "repeat" etc. But there is no information in "R help" regarding such commands. I would like to know why?

I use "R help" for the above commands like below:

?for
?while
?if
?repeat
Rojer
  • 335
  • 2
  • 9
  • 2
    `?"while"` or `?"if"` works though. – Ronak Shah Jun 29 '20 at 09:53
  • 1
    @RonakShah Please, please, *please* do not use regular quotes, which the R documentation discourages, around identifiers. Use backticks. Yes, I’m aware that it’s a pain to format these in comments, but still. Using quotes here is suuuuper confusing. – Konrad Rudolph Jun 29 '20 at 09:55
  • Please, please do read basic documentation: [An Introduction to R: Getting help with functions and features](https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Getting-help): "For a feature specified by special characters, the argument must be enclosed in double or single quotes, making it a “character string”: This is also necessary for a few words with syntactic meaning including `if`, `for` and `function`." See also the FAQ [How to get help in R?](https://stackoverflow.com/questions/15289995/how-to-get-help-in-r) – Henrik Jun 29 '20 at 10:22
  • @KonradRudolph FWIW, it seems like [the docs](https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Getting-help) does not explicitly encourage backticks or discourage regular quotes. – Henrik Jun 29 '20 at 10:38
  • @Henrik The documentation says “The preferred quote [for identifiers] is the backtick (`)”. For what it’s worth, I would phrase this much stronger: the fact that regular quotes work here at all *is a mistake in a language* that should be deprecated and then removed in future versions of R. – Konrad Rudolph Jun 29 '20 at 12:41
  • @KonradRudolph I agree with the argument on backticks from `Quotes`, and was therefore a bit surprised about the wording in the "_Getting help docs_" that I pointed to. Cheers – Henrik Jun 29 '20 at 13:02

1 Answers1

1

R requires that keywords are used in syntactically valid form. The way R works, it expects that if for instance is followed by an expression in parentheses, and a body. ?if is not valid R syntax.

Conversely, ? is an operator that expects an identifier after it.

To make it valid, you should quote the if identifier in backticks. That way, R parses the expression as ? followed by an identifier, rather than ? followed by an incomplete if expression:

?`if`

Backtick-quoting is R’s way of saying: “hey, that thing between backticks is a valid identifier, even if it totally doesn’t look like one”. You could (but generally shouldn’t!) totally use it to use wonky variable names:

`name with spaces` = 2
message(`name with spaces` + 5)
# 7

This feature is more useful when applied to column names of externally imported data (which sometimes contains spaces or other invalid identifier characters), or when defining operators.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214