Re: [Rd] Strange behavior of assign in a S4 method.

From: Christophe Genolini <cgenolin_at_u-paris10.fr>
Date: Tue, 16 Mar 2010 14:14:21 +0100

Dear Wolfgang,

Thanks for the showMethod and the link.

>
> Perhaps juggling with the 'n' argument of 'parent.frame' could help in
> hacking something together that 'works'
I just change the argument list of foo2 to perfectly match with the definition.
> but as far as I can see what you want to is an abuse of R's pass by
> value / functional language semantics.
>
Yes I kown. When I learnt object programming, one of the basic was that some methods was design to change internals value of the fields. It is what I try to do here.
> For example, try these and check whether this results in what you
> intended:
>
> foo2(3)
> foo2(e+2)
> sapply(1:5, foo2)
> ls()
This will not apply, because I my case, the function foo2 is avalable only for object of class FooClass and the only possible use will be :

toto <- new("FooClass")
....
foo2(toto)

Best wishes
Christophe

> Best wishes
> Wolfgang
>
>
> Christophe Genolini scripsit 15/03/10 11:33:
>> Hi the list,
>> I define a method that want to change an object without assignation
>> (foo(x) and not x<-foo(x)) using deparse and assign.
>> But when the argument of the method does not match *exactly* with the
>> definition of the generic function, assign does not work...
>> Anything wrong?
>>
>> Christophe
>>
>> #------ Does not work ------#
>> setGeneric("foo1",function(x,...){standardGeneric("foo1")})
>>
>> setMethod(f="foo1",signature="numeric",definition=
>> function(x,y=1,...){
>> nameX<-deparse(substitute(x))
>> x <- x^2
>> assign(nameX,x,envir=parent.frame())
>> }
>> )
>>
>> e <- 3
>> foo1(e,y=5)
>> cat(e)
>>
>>
>> #------ Does work ------#
>> setGeneric("foo2",function(x,...){standardGeneric("foo2")})
>>
>> setMethod(f="foo2",signature="numeric",definition=
>> function(x,...){
>> nameX<-deparse(substitute(x))
>> x <- x^2
>> assign(nameX,x,envir=parent.frame())
>> }
>> )
>>
>> e <- 3
>> foo2(e,y=5)
>> cat(e)
>>
>> ______________________________________________
>> R-devel_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>
>
> --
> Wolfgang Huber
> EMBL
> http://www.embl.de/research/units/genome_biology/huber/contact
>
>
>



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 16 Mar 2010 - 13:16:32 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 16 Mar 2010 - 18:31:08 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive