From jombart at biomserv.univ-lyon1.fr  Fri May 23 17:20:14 2008
From: jombart at biomserv.univ-lyon1.fr (Thibaut Jombart)
Date: Fri, 23 May 2008 17:20:14 +0200
Subject: [Rd] seeing an S4 method, not using it
Message-ID: <4836E0AE.9070609@biomserv.univ-lyon1.fr>

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.

-- 
######################################
Thibaut JOMBART
CNRS UMR 5558 - Laboratoire de Biom?trie et Biologie Evolutive
Universite Lyon 1
43 bd du 11 novembre 1918
69622 Villeurbanne Cedex
T?l. : 04.72.43.29.35
Fax : 04.72.43.13.88
jombart at biomserv.univ-lyon1.fr
http://biomserv.univ-lyon1.fr/%7Ejombart/
http://adegenet.r-forge.r-project.org/



[Rd] seeing an S4 method, not using it
Thibaut Jombart