[R] match.call(), S4

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Mar 3 16:30:29 CET 2004


On Wed, 3 Mar 2004, Heywood, Giles wrote:

> I get different results from match.call(), according to whether a function
> is dispatched via S3 or S4.  Specifically, when I use S4 dispatch in the
> following example, the match.call() result is of length 1 less than I
> expect.  I need to add an extra comma to get the same results as in the S3
> method.

I am confused by your puzzlement, and don't believe this has anything to
do with S4 vs S3.  Consider

> tmp <- function(i, j, k) match.call()
> tmp(1)
tmp(i = 1)
> tmp(1,)
tmp(i = 1)
> tmp2 <- function(x, ...) match.call()
> tmp2(1)
tmp2(x = 1)
> tmp2(1, )
tmp2(x = 1,)
> tmp2(1,,2)
tmp2(x = 1, , 2)

so why are you surprised?  `...' is not handled the same way as other 
arguments, and your S3 and S4 methods have different formal argument 
lists.

Take a look at [.data.frame for the sort of code you need to write to 
handle this at R level.


> --------example
> 
> setClass("foo",representation("matrix"))
> setMethod("[", c("foo","ANY"),  function(x, i, j, drop, ...)
>     {
>     mc <- match.call()
>     print(mc)
>     xmat <- x at .Data
>     mc[[2]] <- as.name("xmat")
>     return(eval(mc))
>     }) 
> "[.bar" <- function(x, ...)
>     {
>     mc <- match.call()
>     print(mc)
>     xmat <- unclass(x)
>     mc[[1]] <- as.name("[")
>     mc[[2]] <- as.name("xmat")
>     return(eval(mc))
>     }
> 
> ff <- new("foo",matrix(1:6,2,3))
> bb <- structure(matrix(1:6,2,3),class="bar")
> 
> all.equal(bb[1],ff[1,])		#did not expect to need the extra comma in
> ff[1,]
> all.equal(bb[1,],ff[1,,])
> 
> 
> ------output
> 
> 
> > all.equal(ff[1,],bb[1])
> ff[i = 1]
> [1] "length of mc is  3"
> "[.bar"(x = bb, 1)
> [1] "length of mc is  3"
> [1] TRUE
> > all.equal(bb[1,],ff[1,,])
> "[.bar"(x = bb, 1, )
> [1] "length of mc is  4"
> ff[i = 1, ]
> [1] "length of mc is  4"
> [1] TRUE
> 
> ------final comment
> 
> This is a minimal example.  I am really trying to invoke the method of the
> superclass (matrix) after modifying the range arguments in the extract
> method.  Perhaps there's a better way to do the whole kaboodle?
> 
> Thanks
> 
> - Giles
>          _              
> platform i386-pc-mingw32
> arch     i386           
> os       mingw32        
> system   i386, mingw32  
> status                  
> major    1              
> minor    8.1            
> year     2003           
> month    11             
> day      21             
> language R              
> 
> 
> ********************************************************************** 
> This is a commercial communication from Commerzbank AG.\ \ T...{{dropped}}
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list