[Bioc-devel] s4 guidance
Joern Toedling
Joern.Toedling at curie.fr
Mon Feb 21 15:31:33 CET 2011
Hello,
there are a several good introductions to S4 classes out there, for example
check out "A (not so) short introduction to S4" by Christophe Genolini which
is available from this page:
http://cran.r-project.org/other-docs.html
And to avoid the use of "@" at user-level, one should provide accessor
methods to users to retrieve and set the values for all slots.
However, as far as I am aware, there is no way to prevent users from making
use of "@" altogether.
Regarding your example, I think it would be best if populating test at doubleX
happens either in the initialization function and/or the replace method for
test at x.
Cheers,
Joern
On Mon, 21 Feb 2011 13:53:39 +0000, Stefano Berri wrote
> 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
>
> _______________________________________________
> Bioc-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/bioc-devel
---
Joern Toedling
Institut Curie -- U900
26 rue d'Ulm, 75005 Paris, FRANCE
Tel. +33 (0)156246927
More information about the Bioc-devel
mailing list