[Rd] seeing an S4 method, not using it

Martin Morgan mtmorgan at fhcrc.org
Fri May 23 20:05:21 CEST 2008


Hi Thibaut --

The short answer seems to be that this is fixed in the devel 
implementation of S4 (at least, my effort at reproducing this was 
successful in 2.7 but not R version 2.8.0 Under development (unstable) 
(2008-05-22 r45762)).

I think it is like this

http://tolstoy.newcastle.edu.au/R/e2/devel/07/09/4469.html

My understanding of the problem is that when the object is read in the 
necessary package is 'load'ed but not 'attach'ed -- R knows about the 
class definition, etc, but methods and functions from the package are 
not on the search path, i.e., after reading your data object, the result 
of search() does not contain the package.

When you 'show' your object, S4 tries to find an appropriate method, but 
does not. So it uses the default, and makes a note that your object 
should be printed with that. After printing, showMethods('show') will 
indicate that your object has a 'show' method inherited from 'ANY'. 
Unfortunately, if you now load the package, nothing good happens -- the 
appropriate show method is available, but S4 thinks that it already 
knows a method for your object (the 'ANY' method) so does not find your 
version.

Confusing, isn't it?

Martin

Thibaut Jombart wrote:
> Dear list,
> 
> here is a problem I met when trying to use a method for an S4 object, 
> without loading the package in which the method was defined. I do not 
> know if this is a bug, or a mistake of mine. Normally, I think the 
> package in which the appropriate method is defined is loaded 
> automatically when calling the method. The problem is that if the 
> package is indeed loaded automatically, the appropriate method is not used.
> 
> An example using the package pixmap (but I obtained similar results with 
> S4 objects from adegenet and phylobase packages):
> ##### in one R session
>  > library(pixmap)
>  > foo <- new("pixmap")
> 
> ## here the object is printed with the appropriate show method.
>  > foo
> Pixmap image
> Type : pixmap
> Size : 0x0
> Resolution : 0x0
> Bounding box : 0 0 0 0
> 
>  > setClass("myClass", contains="pixmap") # used later
> [1] "myClass"
>  > foo2 <- new("myClass")
> ## show is ok too
>  > foo2
> Pixmap image
> Type : myClass
> Size : 0x0
> Resolution : 0x0
> Bounding box : 0 0 0 0
> 
> 
> save.image()
> 
> 
> ##### after closing R and starting a new session, loading the .RData
> ##### (pixmap is not loaded)
>  > foo
> Loading required package: pixmap ## <-- pixmap loaded automatically
> An object of class “pixmap”
> Slot "size":
> [1] 0 0
> 
> Slot "cellres":
> [1] 0 0
> 
> Slot "bbox":
> [1] 0 0 0 0
> 
> Slot "bbcent":
> logical(0)
> 
> 
> ## the object is not printed correctly. Yet:
>  > getMethod("show","pixmap")
> Method Definition:
> 
> function (object)
> {
> cat("Pixmap image\n")
> cat(" Type :", class(object), "\n")
> cat(" Size :", paste(object at size, collapse = "x"),
> "\n")
> cat(" Resolution :", paste(object at cellres, collapse = "x"),
> "\n")
> cat(" Bounding box :", object at bbox, "\n")
> if (is(object, "pixmapIndexed"))
> cat(" Nr. of colors :", length(unique(as(object at index,
> "vector"))), "of", length(object at col), "\n")
> cat("\n")
> }
> <environment: 0x89b0e38>
> 
> Signatures:
> object
> target "pixmap"
> defined "pixmap"
> 
> 
> More surprisingly, the printing of foo2 is correct:
>  > foo2
> Pixmap image
> Type : myClass
> Size : 0x0
> Resolution : 0x0
> Bounding box : 0 0 0 0
> 
> 
> 
> Plus, detaching the package pixmap and re-loading it did not help.
> I tried to trace the show method to get an idea of what was going on, 
> but 'tracing' the problem, solved the problem (!). I used:
>  > trace("show",browser, signature="pixmap")
>  > foo
> Tracing structure(function (object) .... on entry
> Called from: eval(expr, envir, enclos)
> Browse[1]>
> Pixmap image
> Type : pixmap
> Size : 0x0
> Resolution : 0x0
> Bounding box : 0 0 0 0
>  > tracingState(FALSE)
>  > foo
> Pixmap image
> Type : pixmap
> Size : 0x0
> Resolution : 0x0
> Bounding box : 0 0 0 0
> 
> 
> Running debug on trace, I found that the correct show method was used 
> after this command line:
> methods::.TraceWithMethods("show", browser, signature = "pixmap", where 
> = .GlobalEnv)
> 
> But I admit I did not go much further in .TraceWithMethods.
> 
> I did not find related topics among known bugs (S4methods section), yet 
> I found this possibly related topic:
> http://tolstoy.newcastle.edu.au/R/e2/devel/07/05/3111.html
> but this did not give me an answer.
> 
> Is this a normal behaviour?
> 
> Here is my sessionInfo (second session):
>  > sessionInfo()
> R version 2.7.0 (2008-04-22)
> i686-pc-linux-gnu
> 
> locale:
> LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=C;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C 
> 
> 
> attached base packages:
> [1] datasets utils stats graphics grDevices methods base
> 
> other attached packages:
> [1] pixmap_0.4-7 ade4_1.4-8 MASS_7.2-42
> 
> loaded via a namespace (and not attached):
> [1] tools_2.7.0
> 
> My OS is an Ubuntu 8.04 for 32bits systems.
> 
> 
> Thanks in advance.
> 
> Best regards,
> 
> Thibaut.
> 


-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M2 B169
Phone: (206) 667-2793



More information about the R-devel mailing list