Search code examples
rlistconcatenationexpression

How can I make c return an expression?


The documentation for c claims that:

"The output type is determined from the highest type of the components in the hierarchy NULL < raw < logical < integer < double < complex < character < list < expression."

But when I feed it an expression, what I get is quite clearly a list:

> c(1,quote(5+2),3)
[[1]]
[1] 1

[[2]]
5 + 2

[[3]]
[1] 3

> typeof(c(1,quote(5+2),3))
[1] "list"
> is.list(c(1,quote(5+2),3))
[1] TRUE

So what input is it supposed to return an expression for? I tried to feed it a function, but that seems to fall under the "whereas non-vector components (such names and calls) are treated as one-element lists" rule and therefore gives me a list.


Solution

  • quote() doesn't return an expression, it returns a variety of different types. In your case it returns something of type "language" (which is actually an unevaluated function call). Use expression(5+2) to get an expression, which is language (or other things) wrapped in a specially marked list.

    For example,

    > c(1,expression(5+2),3)
    expression(1, 5 + 2, 3)