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

2005-04-16 Thread Duncan Murdoch
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
__
R-devel@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


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

2005-04-16 Thread Peter Dalgaard
Duncan Murdoch [EMAIL PROTECTED] 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
~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907

__
R-devel@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


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

2005-04-15 Thread Tony Plate
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)
__
R-devel@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel