[R] How to get the definition of a class?

Martin Morgan mtmorgan at fhcrc.org
Wed Oct 14 05:38:50 CEST 2009


Peng Yu wrote:
> On Tue, Oct 13, 2009 at 9:38 PM, Martin Morgan <mtmorgan at fhcrc.org> wrote:
>> Peng Yu wrote:
>>> Hi,
>>>
>>> ExonFeatureSet
>>>
>>> I have an object of the above class. The following document mentioned it.
>>>
>>> http://www.bioconductor.org/packages/2.5/bioc/vignettes/oligo/inst/doc/ClassesUsedInOligo.pdf
>>>
>>> But I would like to see its defintion. I'm wondering if there is a way
>>> in R to give me the definition of any class?
>> It is defined by a call to setClass in the package that defines it
>> (oligoClasses); I believe the package source (in this case,
>> oligoClasses/R/AllClasses.R) is the only place where the explicit call
>> is available.
>>
>> A representation of the class is available with
>>
>>  cls = getClass("ExonFeatuereSet")
>>
>> show(cls) displays a summary of the class, class(cls) tells you how
>> classes are represented (as class classRepresentation),
>> class?classRepresentation provides information on the representation of
>> classes.
>>
>> oligoClasses:::.__C__ExonFeatureSet is the definitive instance of the
>> class representation (hiding inside the name space and obscured, so
>> clearly not meant for direct access).
> 
> Are functions like slotNames, show, etc., that are mentioned in
> Section C (Memo) of the Appendix in "A (Not So) Short Introduction to
> S4" defined in R language? Or they are function that should be provide
> by any S4 class?

One would not normally implement a slotNames method. This is generally
true of functions operating on classRepresentation instances.

show is a generic, with a default method. Most S4 classes will have show
methods.

setClass("A", representation(x="integer"))

setMethod(show, "A", function(object) {
    cat("Class:", class(object), "\n")
    cat("x length:", length(slot(object, "x")), "\n")
})

> new("A")
Class: A
x length: 0
> slotNames("A")
[1] "x"
> slotNames(new("A"))
[1] "x"
> showMethods(show)
Function: show (package methods)
object="A"
[and many more]
> selectMethod(show, "A")
Method Definition:

function (object)
{
    cat("Class:", class(object), "\n")
    cat("x length:", length(slot(object, "x")), "\n")
}

Signatures:
        object
target  "A"
defined "A"

> trace(show, browser, signature="A")
Tracing specified method for function "show" in environment
<namespace:methods>
Warning: Tracing only in the namespace; to untrace you will need:
untrace("show", where = getNamespace("methods"))
[1] "show"
attr(,"package")
[1] "methods"
> new("A")
Tracing structure(function (object)  .... on entry
Called from: eval(expr, envir, enclos)
Browse[1]> ls()
[1] "object"
Browse[1]> Q

Martin

> 
> ______________________________________________
> 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