Tony Plate
2005-Apr-15 17:32 UTC
[Rd] dealing with empty actual arguments matched by '...' formals
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 '...'. 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. -- Tony Plate # 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)) } i <- 1:7 f.R(1,1:2,i) # Try to confirm that f.R evaluates its argument in the correct environment (function() {i<-1:2; f.R(1,1:2,i)})() 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,,,,)
Duncan Murdoch
2005-Apr-16 10:59 UTC
[Rd] dealing with empty actual arguments matched by '...' formals
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. Duncan Murdoch
Seemingly Similar Threads
- Error formal argument "softmax" matched by multiple actual arguments
- get(name, envir=envir) : formal argument "envir" matched by multiple actual arguments
- formal argument "axes" matched by multiple actual arguments
- formal argument "envir" matched by multiple actual arguments
- Argument "nomatch" matched by multiple actual arguments ... %in% -> match?!?