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.