Here's a sketch of why ...()
works the way it does. I'll fill in with more details and references later, but this touches on the key points.
Before performing substitution on any of its components, substitute()
first parses an R statement.
...()
parses to a call object, whereas ...
parses to a name object.
...
is a special object, intended only to be used in function calls. As a consequence, the C code that implements substitution takes special measures to handle ...
when it is found in a call object. Similar precautions are not taken when ...
occurs as a symbol. (The relevant code is in the functions do_substitute
, substitute
, and substituteList
(especially the latter two) in R_SRCDIR/src/main/coerce.c
.)
So, the role of the ()
in ...()
is to cause the statement to be parsed as a call (aka language) object, so that substitution will return the fully expanded value of the dots. It may seem surprising that ...
gets substituted for even when it's on the outside of the ()
, but: (a) calls are stored internally as list-like objects and (b) the relevant C code seems to make no distinction between the first element of that list and the subsequent ones.
Just a side note: for examining behavior of substitute
or the classes of various objects, I find it useful to set up a little sandbox, like this:
f <- function(...) browser()
f(a = 4, 77, B = "char")
## Then play around within the browser
class(quote(...)) ## quote() parses without substituting
class(quote(...()))
substitute({...})
substitute(...(..., X, ...))
substitute(2 <- (makes * list(no - sense))(...))
substitute(...)
only returns the first element of the of...
. (Note thattest <- function(...) substitute({...}); test(T, F)
will return them both, in a fashion.substitute
,substituteList
anddo_substitute
. They do deal with ellipses in a special way....()
creates a function for element of...
....
is a pairlist and the first argument ofsubstitute()
is supposed to be an expression. In an expression(
can be used to simply group things. As allsubstitute
is doing is returning the parse tree for...
, I wonder if the()
is just a trick of the parser or similar to get it to group the...
somehow. As Bill's email shows, there isn't any function creation as that would show up in the resulting parse tree would it not?substitute(...[])
, too...