[R] nargs() inside "[.myclass"

David James dj at research.bell-labs.com
Tue Jun 27 05:06:42 CEST 2000


[rgentlem at stat.auckland.ac.nz]
> > "Vadim Kutsyy" <vadim at kutsyy.com> writes:
> > 
> > > > This does seem to work:
> > > > 
> > > > > "[.myclass"<-function(x,...)
> > > > > {n<-nargs();if(n==2&&missing(..1))n<-1;print(n-1)}
> > > > >  x[]
> > > > [1] 0
> > > > >  x[1]
> > > > [1] 1
> > > > >  x[1,2]
> > > > [1] 2
> > > 
> > > 
> > > It does, thank you.  could you explain what ..1 stands for?
> > 
> > It's the first of the "..." arguments.
> > 
> > Some of the things that surround the handling of those are barely
> > understood by the maintainers. I've certainly been surprised by having
> > "..1" pop up in match.call() output once in a while. I'm not convinced
> > that the [] case isn't a bug. 
> > 
> > We seem to have managed to code the entire base library without
> > referring to ..n arguments a single time, but your example looks like
> > it requires it, unless the current behavior is a bug.
> 
>   I don't think that is true. It is needed for the generic functions
>   at least at an internal level. Also, I expect that the dispatching
>   needs a bit of a tidy-up for to handle this; my guess is that nargs
>   is settled on at time of dispatch and possibly not adjusted once the
>   method is selected (but I could be wrong and don't have time this week
>   to do anything about it).

I'm not sure this is a bug, but is is consistent with S. 

If we invoke the function ourselves we get the same behaviour:

# R 1.0.0
> get("[.myclass")
function(x, ...) print(nargs()-1)
> "[.myclass"(x)
[1] 0
> "[.myclass"(x,)
[1] 1
> "[.myclass"(x,1)
[1] 1
> "[.myclass"(x,1,2)
[1] 2

Clearly the generic function will create a call with the "x"
argument plus "..." -- this means that

  x[]    <------>  "[.myclass"(x,)    two args, 2nd arg missing
  x[1]   <------>  "[.myclass"(x,1)   two args, no missing
  x[1,2] <------>  "[.myclass"(x,1,2) three args, no missing

The question now becomes whether R/S should dispatch "x.myclass"(x)
for x[] or "x.myclass(x,) as it's currently done.  

BTW see p.476 in Appendix A of Chambers and Hastie (ed.) "Statistical 
Models in S" for an example just like the problem Vadim is trying to
solve.

> > 
> > 
> > Apropos: Here's a little exercise:
> > 
> > > f<-function(...)nargs()
> > > f(1)
> > [1] 1
> > > f()
> > [1] 0
> > > f(,)
> > [1] 2
> > > f(,,)
> > [1] 3
> > 
> > Now, how do you call f with exactly *one* missing argument?
> > 
> > 
> > -- 
> >    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
> > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> > Send "info", "help", or "[un]subscribe"
> > (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 
> -- 
> +-------------------------------------------------------------------------+
> | Robert Gentleman              phone : (64-9) 3737-599 ext 3946          | 
> | Senior Lecturer               fax :   (64-9) 3737-018                   |
> | Department of Statistics      office : Room 206 Maths/Physics           |
> | University of Auckland	email : rgentlem at stat.auckland.ac.nz      |
> +-------------------------------------------------------------------------+
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

David A James                        Phone: (908) 582-3082
Bell Labs, Lucent Technologies       Fax:   (908) 582-3340
600 Mountain Ave                     Email: dj at bell-labs.com
Murray Hill, NJ 07974

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list