[Rd] S4 method dispatch

From: Edzer Pebesma <edzer.pebesma_at_uni-muenster.de>
Date: Wed, 14 Sep 2011 23:00:38 +0200


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)



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


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"
[1] "sp"
> fullgrid(g)

ink3">[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@wwu.de

R-devel_at_r-project.org mailing list
Received on Thu 15 Sep 2011 - 11:34:18 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 18 Sep 2011 - 21:20:32 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive