Dear All,
maybe this is something obvious, I seem to be incapable of
understanding how S4 works.
So, in package 'A' I defined a "summary" method for my class:
setMethod("summary", signature(object="ListHyperGResult"),
function(object, pvalue=pvalueCutoff(object), categorySize=NULL) {
"whatever"
})
"ListHyperGResult" has a subclass, "GOListHyperGResult":
setClass("GOListHyperGResult",
representation=representation(conditional="logical"),
contains="ListHyperGResult",
prototype=prototype(testname="GO"))
The summary method is exported in the NAMESPACE:
exportMethods("summary")
Package 'B' depends on package 'A', this is stated in the
'DESCRIPTION' file. If I call 'summary' on a
'GOListHyperGResult' in
package B, then the default summary method is called instead of the
correct one, despite that I have
Browse[1]> showMethods("summary")
Function: summary (package base)
object="AnnDbBimap"
object="ANY"
object="Bimap"
object="DBIObject"
object="HyperGResultBase"
object="KEGGHyperGResult"
object="LinearMResultBase"
object="ListHyperGResult"
object="PFAMHyperGResult"
object="SQLiteConnection"
object="SQLiteDriver"
object="SQLiteResult"
Browse[1]> class(gos[[1]])
[1] "GOListHyperGResult"
But I still get:
Browse[1]> is(gos[[1]], "ListHyperGResult")
[1] TRUE
Browse[1]> summary(gos[[1]])
Length Class Mode
1 GOListHyperGResult S4
What am I doing wrong?
> sessionInfo()
R version 2.9.0 (2009-04-17)
x86_64-redhat-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] stats graphics grDevices utils datasets methods base
other attached packages:
[1] hgu95av2.db_2.2.12 ALL_1.4.4 ExpressionView_0.1
[4] caTools_1.9 bitops_1.0-4.1 KEGG.db_2.2.5
[7] GO.db_2.2.5 RSQLite_0.7-1 DBI_0.2-4
[10] eisa_0.1 genefilter_1.24.2 Category_2.10.0
[13] AnnotationDbi_1.6.0 Biobase_2.4.1 isa2_0.1
loaded via a namespace (and not attached):
[1] annotate_1.22.0 graph_1.22.2 GSEABase_1.6.0 RBGL_1.20.0
[5] splines_2.9.0 survival_2.35-4 tools_2.9.0 XML_2.6-0
[9] xtable_1.5-5>
Thanks,
Gabor
--
Gabor Csardi <Gabor.Csardi at unil.ch> UNIL DGM
Martin Morgan
2009-Sep-17 15:59 UTC
[R] Why S4 method is not visible from another package?
G?bor Cs?rdi wrote:> Dear All, > > maybe this is something obvious, I seem to be incapable of > understanding how S4 works. > > So, in package 'A' I defined a "summary" method for my class: > > setMethod("summary", signature(object="ListHyperGResult"), > function(object, pvalue=pvalueCutoff(object), categorySize=NULL) { > "whatever" > }) > > "ListHyperGResult" has a subclass, "GOListHyperGResult": > > setClass("GOListHyperGResult", > representation=representation(conditional="logical"), > contains="ListHyperGResult", > prototype=prototype(testname="GO")) > > The summary method is exported in the NAMESPACE: > > exportMethods("summary") > > Package 'B' depends on package 'A', this is stated in the > 'DESCRIPTION' file. If I call 'summary' on a 'GOListHyperGResult' inHi Gabor It is not S4 alone, but S4 + name spaces that are giving you problems. You probably want to Import: A rather than depends, and importFrom(A, summary). As it stands, inside the B name space, you find base::summary, whereas you've defined a method on summary that has been promoted to a generic in one of the packages that A imports (probably AnnotationDbi). This is a little bit of a guess; at some level it might seem more appropriate to Import: AnnotationDbi and importFrom(AnnotationDbi, summary) (or wherever the generic for summary that you are trying to use is created). Martin> package B, then the default summary method is called instead of the > correct one, despite that I have > > Browse[1]> showMethods("summary") > Function: summary (package base) > object="AnnDbBimap" > object="ANY" > object="Bimap" > object="DBIObject" > object="HyperGResultBase" > object="KEGGHyperGResult" > object="LinearMResultBase" > object="ListHyperGResult" > object="PFAMHyperGResult" > object="SQLiteConnection" > object="SQLiteDriver" > object="SQLiteResult" > > Browse[1]> class(gos[[1]]) > [1] "GOListHyperGResult" > > But I still get: > > Browse[1]> is(gos[[1]], "ListHyperGResult") > [1] TRUE > Browse[1]> summary(gos[[1]]) > Length Class Mode > 1 GOListHyperGResult S4 > > What am I doing wrong? > >> sessionInfo() > R version 2.9.0 (2009-04-17) > x86_64-redhat-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] stats graphics grDevices utils datasets methods base > > other attached packages: > [1] hgu95av2.db_2.2.12 ALL_1.4.4 ExpressionView_0.1 > [4] caTools_1.9 bitops_1.0-4.1 KEGG.db_2.2.5 > [7] GO.db_2.2.5 RSQLite_0.7-1 DBI_0.2-4 > [10] eisa_0.1 genefilter_1.24.2 Category_2.10.0 > [13] AnnotationDbi_1.6.0 Biobase_2.4.1 isa2_0.1 > > loaded via a namespace (and not attached): > [1] annotate_1.22.0 graph_1.22.2 GSEABase_1.6.0 RBGL_1.20.0 > [5] splines_2.9.0 survival_2.35-4 tools_2.9.0 XML_2.6-0 > [9] xtable_1.5-5 > > Thanks, > Gabor >