> # In R:
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:
> 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 '...'.
In R, the only way I could find to get the non-empty arguments in the presence of empty arguments was to call eval() on particular components of match.call() (as in the function f.R() below). Is there a better way?
I've appended some example functions and test calls in case anyone wants to play with this and suggest possible alternative methods.
# R function to process empty arguments
f.R <- function(x, ...) {
dotargs <- match.call(expand.dots=F)$...
arg.missing <- sapply(dotargs,
function(a) is.name(a) && as.character(a)=="")
args <- vector("list", length(arg.missing))
i <- 3 # check that args are being eval'd in the right env
args[!arg.missing] <- lapply(dotargs[!arg.missing],
eval, sys.parent())
data.frame(missing=arg.missing,
length=if (length(args)) sapply(args, length)
else numeric(0))
f.R(1) f.R(1,,,) f.R(1,,2:4,) f.R(1,numeric(0),2:4,) f.R(1,NULL,2:4,) f.R(1,NULL,2:4,,,,)
# Example of an S-PLUS function that can process empty anonymous arguments f.S <- function(x, ...) {
dotargs <- match.call(expand.dots=F)$...[-1]
arg.missing <- if (length(dotargs)) sapply(dotargs, mode)=="missing"
else logical(0)
args <- list(...)
args <- args[replace(cumsum(!arg.missing), arg.missing,
length(args)+1)]
data.frame(missing=arg.missing,
length=if (length(args)) sapply(args, length)
else numeric(0))
f.S(1) f.S(1,,,) f.S(1,,2:4,) f.S(1,numeric(0),2:4,) f.S(1,NULL,2:4,) f.S(1,NULL,2:4,,,,) ______________________________________________R-devel@stat.math.ethz.ch mailing list
This archive was generated by hypermail 2.1.8 : Mon 20 Feb 2006 - 03:21:03 GMT