[Rd] S4 method dispatch

Edzer Pebesma edzer.pebesma at uni-muenster.de
Wed Sep 14 23:00:38 CEST 2011


List,

In order to get rid of some old, unreadable S3 code in package sp, I'm
trying to rewrite things using S4 methods. Somewhere I fail, and I
cannot sort out why. In order to isolate the problem, I created two
functions, doNothing<- and dosth, and both should do nothing. The issue
is that in most cases they do nothing, but in some cases dosth(obj)
changes the class of obj and breaks with the error. I couldn't find a
pattern when this happens, but have a few cases where it consistently
breaks. Here's the code snippet:

setGeneric("doNothing<-", function(object, value)
    standardGeneric("doNothing<-"))

setReplaceMethod("doNothing",
    signature(object = "Spatial", value = "ANY"),
    function(object, value) object)

dosth = function(obj) {
    cl1 = class(obj)
    doNothing(obj) = TRUE
    cl2 = class(obj)
    if (!identical(cl1, cl2)) {
        print(paste(cl1, cl2))
        stopifnot(identical(cl1, cl2))
    }
    obj
}

When things go wrong, dosth and doNothing are called with a subclass of
Spatial, e.g. an object of class SpatialGrid, but when this gets in
doNothing, the object is suddenly of class Spatial, and is then returned
as an object of class Spatial, which should never happen.

For instance, I have a case where consistently

setMethod("fullgrid", c("Spatial"),
    function(obj) { is(obj, "SpatialGrid") })
> class(g)
[1] "SpatialGrid"
attr(,"package")
[1] "sp"
> fullgrid(g)
[1] FALSE

is obviously false, but in other cases it works fine.

When I change the signature of doNothing to signature(object = "ANY",
value = "ANY"), the problem disappears.

I tried to make a self-contained example that reproduced the issue, but
could only get something that worked as expected.

I would appreciate any help or suggestions.
-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics      e.pebesma at wwu.de



More information about the R-devel mailing list