[R] match.call(), S4

Heywood, Giles Giles.Heywood at CommerzbankIB.com
Wed Mar 3 13:06:46 CET 2004


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.

--------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}}




More information about the R-help mailing list