From cgenolin at u-paris10.fr  Mon Mar 15 11:33:17 2010
From: cgenolin at u-paris10.fr (Christophe Genolini)
Date: Mon, 15 Mar 2010 11:33:17 +0100
Subject: [Rd] Strange behavior of assign in a S4 method.
Message-ID: <4B9E0CED.6030405@u-paris10.fr>

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)



[Rd] Strange behavior of assign in a S4 method.
Christophe Genolini