On 11/19/2012 04:21 PM, Andrea Spano wrote:> Hello the list,
>
> As a simple example:
>
>
>> rm(list = ls())> setClass("tre",
representation(x="numeric"))> setMethod("show",
"tre", def = function(object) cat(object at x))[1]
"show"> setMethod("summary", "tre", def =
function(object) cat("This is a tre of value ", object at x,
"\n"))Creating a generic function for ?summary? from package ?base? in
the global environment[1] "summary"> ls()[1] "summary"
>
>
> R copies generic summary into the current environment.
>
> I understand it as: If you want R to create a specific "summary"
> method for objects of class "tre" R needs the generic method in
the
> same environment (R_GloabalEnv).
>
> In fact, summary is listed by ls in in my local env.
>
>
> Why R does not do the same with generic method "show"?
>
>
> I know that generic method "show" is from package
"methods" while
> summary is package "base". Does it matter somehow?
There are several object systems in R. Your use of setMethod indicates that you
are creating an S4 method, to be associated with an S4 generic. 'show'
is a
generic in the S4 object system, and setMethod(show, <...>) associates
your
method with this generic. 'summary' is a generic in the S3 object
system.
setMethod(summary, <...>) realizes that there is not yet an S4 generic
summary,
so creates an S4 generic (with the S3 generic as the default method) and then
adds your method to the newly created S4 generic. The S3 generic is not copied
to the global environment.
Martin
>
>
> Can anyone point me to the right reference?
>
>
> Thanks in advance for your help ...
>
>
> Andrea
>
> [[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.
>
--
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