[Rd] dealing with empty actual arguments matched by '...' formals

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sat Apr 16 16:42:25 CEST 2005


Duncan Murdoch <murdoch at math.aau.dk> writes:

> Tony Plate wrote:
> > I'm trying to write some functions to deal with empty actual
> > arguments that are picked up by '...' formals.  Such actual
> > arguments are common (and very useful) in calls to subsetting
> > functions, e.g., x[1:2,].  It seems that R and S-PLUS treat these
> > arguments differently: in S-PLUS list(...) will return a list
> > containing just the non-empty arguments, whereas in R list(...)
> > stops with an error:
> >  > # In R:
> >  > f <- function(x, ...) list(...)
> >  > f(1,2)
> > [[1]]
> > [1] 2
> >  > f(1,2,)
> > Error in f(1, 2, ) : argument is missing, with no default
> >  >
> > So it seems that quite different methods must be used in S-PLUS and
> > R to detect and process the arguments of a function that can have
> > empty arguments matched to '...'.
> 
> Can you give an example where it's useful to do this, i.e. to have a
> call like f(1,2,)?  I've never used that construction as far as I can
> recall.

The standard case is indexing, as Tony mentions. 

The whole thing is somewhat tricky because at least some of R's
semantics are deliberately different from S. E.g.

> f <- function(i) g(i)
> g <- function(i) missing(i)
> f()
[1] TRUE

Same thing in S gives FALSE. S looks at the call to g whereas R looks
at the value. This works by passing a "magic bullet" which is
implemented as the "empty name", as you can get to see by doing
something like

> f <- function(...) match.call(expand.dots=FALSE)$...
> l <- f(1,,2)
> eval(l[[2]])
Error in eval(expr, envir, enclos) : Argument is missing, with no default
> mode(l[[2]])
[1] "name"
> as.character(l[[2]])
[1] ""

One side effect of R's way of doing things is that a call to
list(i,j,k) with k missing is hard to tell from list(i,j,). However,
list() must be doing that somehow... I'm not sure it is a good thing, but
it may have been necessary for S compatibility.

I think that what Tony was up to might be doable through variations on
the match.call() scheme above.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907



More information about the R-devel mailing list