[Bioc-devel] s4 guidance

Stefano Berri S.Berri at leeds.ac.uk
Mon Feb 21 14:53:39 CET 2011


Hi.

could somebody give me some guidance and direction?

I started to make a S4 module for bioconductor.
I am stuck to adding values to a slot using a method.

here a proof of concept

The code, class and methods:
############################

setClass (
    Class = 'test',
    representation = representation (
        x = "numeric",
        doubleX = "numeric"
    )   
)

setGeneric(name = "myDouble", function(object){standardGeneric("myDouble")})
setMethod("myDouble", "test", function(object){
        object at doubleX <- 2*object at x
    })

###########################

# then I type

myT <- new(Class = "test", x = c(1,2,3))
# hoping to populate "myDouble" slot
myDouble(myT)
# but if I look into
myT at doubleX
# I get "numeric(0)"


to make it work I have to return the object
{object at doubleX <- 2*object at x; return (object)}
but then it returns the object (with associated "show" method) which I do
not want.


I also struggle to find documentation online on S4. I have "R programming
for bioinformatics" which cover the basics and then a few bits here and
there, but I could not find a nice tutorial and a reference guide...

I have some other questions:
Comparing to other object-oriented programming languages, I expect to find
similar concepts. 
 - Is there a way to hide/encapsulate slots and methods? If I type "myT" and
then press Tab twice it shows all slots that can be read and even written
to! I makes the whole hassle to object oriented programming useless if the
user can overwrite variables, does it not?

Sorry if my questions are naive...

Thanks for your help

Stefano Berri



More information about the Bioc-devel mailing list