[R] Method dispatch

Martin Morgan mtmorgan at fhcrc.org
Tue Mar 2 00:09:55 CET 2010


On 03/01/2010 01:31 PM, Zhang,Yanwei wrote:
> Dear all,
> 
> In a package, I defined a method for "summary" using
setMethod(summary, signature="abc") for my class "abc", but when the
package is loaded, the function "summary(x)" where x is of class "abc"
seems to have called the default summary function for "ANY" class.
Shouldn't it call the method I have defined? How could I get around with
that? Thanks.

Hi Wayne -- It's hard to tell from what you've written, but this and
your earlier question on 'S4 issues' sounds like a NAMESPACE problem.
Are you using a name space? If so, provide additional detail. Also,
verify that you are only loading your package and not another, e.g., one
with a 'summary' generic that is being found before yours.

Probably your best bet is to simplify as much as possible -- I have a
single file in 'pkgA/R', with

  setClass("A", "numeric")
  setMethod("summary", "A", function(object, ...) "A")


> library(pkgA)
> summary(new("A"))
[1] "A"
> sessionInfo()
R version 2.10.1 Patched (2010-02-23 r51168)
x86_64-unknown-linux-gnu

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=C              LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] pkgA_1.0

Martin


> 
> 
> Wayne (Yanwei) Zhang
> Statistical Research
> CNA
> 
> 
> 
> 
> 
> NOTICE:  This e-mail message, including any attachments and appended messages, is for the sole use of the intended recipients and may contain confidential and legally privileged information.
> If you are not the intended recipient, any review, dissemination, distribution, copying, storage or other use of all or any portion of this message is strictly prohibited.
> If you received this message in error, please immediately notify the sender by reply e-mail and delete this message in its entirety.
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


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

Location: Arnold Building M1 B861
Phone: (206) 667-2793



More information about the R-help mailing list