Very briefly, it seems you are expecting pass-by-reference semantics, but R
programming is mostly pass-by-value.  Your myDouble method receives an
object's value, does something to compute a vector, and returns the vector.
The original object is unaltered, as you observe.  You might review sources
of the graph or Biobase packages in Bioconductor to get a feel for
prevailing S4 practice in this project.  S4 programming is programming in R,
with some additional metadata management and relationships among values and
packages that are documented in places noted.

As for the hiding and encapsulation, NAMESPACE discipline gets some of that,
and look at help("bindenv") for some related material.  There is a new
approach to OOP with R that can be learned about with
help("ReferenceClasses") at least in R 2.13.

On Mon, Feb 21, 2011 at 8:53 AM, Stefano Berri <S.Berri@leeds.ac.uk> 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@doubleX <- 2*object@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@doubleX
> # I get "numeric(0)"
>
>
> to make it work I have to return the object
> {object@doubleX <- 2*object@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@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/bioc-devel
>

	[[alternative HTML version deleted]]

