Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valid expressions to the quote function

Tags:

r

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?

like image 677
Ali Avatar asked Oct 11 '12 21:10

Ali


People also ask

What is the function of quote?

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.

What is use of quote () function in R?

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.)

What is the function of double quotes?

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.


1 Answers

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.

like image 196
Joshua Ulrich Avatar answered Oct 02 '22 20:10

Joshua Ulrich