According to the manual of quote(expr)
:
expr: any syntactically valid R expression
While quote(x==y)
returns a call x==y
successfully, quote(x=y)
fails:
Error in quote(x = y) : supplied argument name 'x' does not match 'expr'
Both x=y
and x==y
are syntactically valid R expressions, aren't they? Why quote()
fails on x=y
?
The primary function of quotation marks is to set off and represent exact language (either spoken or written) that has come from somebody else. The quotation mark is also used to designate speech acts in fiction and sometimes poetry.
quote() returns an expression: an object that represents an action that can be performed by R. (Unfortunately expression() does not return an expression in this sense. Instead, it returns something more like a list of expressions. See parsing and deparsing for more details.)
Double quotation marks are used for direct quotations and titles of compositions such as books, plays, movies, songs, lectures and TV shows. They also can be used to indicate irony and introduce an unfamiliar term or nickname. Single quotation marks are used for a quote within a quote.
As it says in ?"="
:
The operator ‘<-’ can be used anywhere, whereas the operator ‘=’ is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions.
Using =
in an argument to quote
is not using it at the top level, so you have to put it in braces or parentheses, but you still have to be careful how you evaluate this expression, since the rules above will still apply.
quote({x=y})
quote((x=y))
To address a comment:
As Gavin Simpson said: basically, the "top level" is when you type or run the code at the prompt and is not within a function call. Take z = quote(expr=x)
for example. z = quote(...)
is evaluated at the top level, but expr=x
is not because it's inside a function call.
In quote(expr=x)
, =
is being used to assign the value of x
to the function argument expr
; so you're no longer working at the top-level, you're constructing a function argument list (pairlist). The reason quote(x=y)
fails is because quote
doesn't have an x
argument.
The top level context is described briefly in R Internals, in Section 1.4, Contexts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With