Class & Method, S3 / S4

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Class & Method, S3 / S4

Dominik Locher
Hi Martin

Thanks a lot for your short example. If you input

test(testObj)

it will return

22

However, how is it possible that the value will be saved in the object

i.e. (does not work currently!!??)
setMethod("test", signature=c("connect"),
  function( obj ) { obj@value<-obj@value / 2 })

so that test(testObj) will save a new value to obj@value or

testObj@value will return 22.

Thanks.
Nik

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Class & Method, S3 / S4

Martin Morgan
Again, R has a different paradigm from what you're used to. R is a
'pass by value' language. So 'obj' inside the method is a *copy* of
testObj, and your assignment changes the 'value' slot of the copy. An
R way of doing this might be

setMethod("test", signature=c("connect"),
  function( obj ) {
    obj@value <- obj@value / 2
    ## and other manipulations...
    obj
})

testObj <- test( testObj )

I 'know' these things by reading Chambers' Programming with Data, and
by looking at exsiting code (ok, and maybe some other ways, too
;). There are some example packages suggested in this thread

https://stat.ethz.ch/pipermail/r-devel/2005-November/035370.html

If you have an R source distribution, look in, for instance,

<PATH-TO-R-SRC>/src/library/stats4/R

At the R command prompt, do something like

library(stats4)
library(help=stats4)
?"update-methods"
getMethods("update")

A warning, I guess, is that looking at complicated code (and the help
pages for the methods package) can be confusing without the kind of
foundation that Chambers' book provides. Maybe others on the list will
provide some hints for introductory documentation on S4 classes and
methods.

Hope that's enough to get you going!

Martin


"Dominik Locher" <[hidden email]> writes:

> Hi Martin
>
> Thanks a lot for your short example. If you input
>
> test(testObj)
>
> it will return
>
> 22
>
> However, how is it possible that the value will be saved in the object
>
> i.e. (does not work currently!!??)
> setMethod("test", signature=c("connect"),
>   function( obj ) { obj@value<-obj@value / 2 })
>
> so that test(testObj) will save a new value to obj@value or
>
> testObj@value will return 22.
>
> Thanks.
> Nik
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Class & Method, S3 / S4

Prof Brian Ripley
You could also define a replacement function and methods for it, when

test(testObj) <- 22

would change testObj itself.  This is done via setReplaceMethod, but you
will need to look at Chambers (1998) to understand that, as
?setReplaceMethod leads to a page that does not describe it apart from
the call sequence (nor is it in the index of the book, but see pp 340-2).


On Thu, 2 Mar 2006, Martin Morgan wrote:

> Again, R has a different paradigm from what you're used to. R is a
> 'pass by value' language. So 'obj' inside the method is a *copy* of
> testObj, and your assignment changes the 'value' slot of the copy. An
> R way of doing this might be
>
> setMethod("test", signature=c("connect"),
>  function( obj ) {
>    obj@value <- obj@value / 2
>    ## and other manipulations...
>    obj
> })
>
> testObj <- test( testObj )
>
> I 'know' these things by reading Chambers' Programming with Data, and
> by looking at exsiting code (ok, and maybe some other ways, too
> ;). There are some example packages suggested in this thread
>
> https://stat.ethz.ch/pipermail/r-devel/2005-November/035370.html
>
> If you have an R source distribution, look in, for instance,
>
> <PATH-TO-R-SRC>/src/library/stats4/R
>
> At the R command prompt, do something like
>
> library(stats4)
> library(help=stats4)
> ?"update-methods"
> getMethods("update")
>
> A warning, I guess, is that looking at complicated code (and the help
> pages for the methods package) can be confusing without the kind of
> foundation that Chambers' book provides. Maybe others on the list will
> provide some hints for introductory documentation on S4 classes and
> methods.
>
> Hope that's enough to get you going!
>
> Martin
>
>
> "Dominik Locher" <[hidden email]> writes:
>
>> Hi Martin
>>
>> Thanks a lot for your short example. If you input
>>
>> test(testObj)
>>
>> it will return
>>
>> 22
>>
>> However, how is it possible that the value will be saved in the object
>>
>> i.e. (does not work currently!!??)
>> setMethod("test", signature=c("connect"),
>>   function( obj ) { obj@value<-obj@value / 2 })
>>
>> so that test(testObj) will save a new value to obj@value or
>>
>> testObj@value will return 22.
>>
>> Thanks.
>> Nik
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Loading...