From ripley at stats.ox.ac.uk  Tue Mar 16 08:11:20 2010
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Tue, 16 Mar 2010 07:11:20 +0000 (GMT)
Subject: [Rd] Strange behavior of assign in a S4 method.
In-Reply-To: <4B9E0CED.6030405@u-paris10.fr>
References: <4B9E0CED.6030405@u-paris10.fr>
Message-ID: <alpine.OSX.1.00.1003160711080.41250@tystie.local>

Where did you get the idea that 'x' comes from the parent frame? 
There is no guarantee of this, and you are relying on/stumbling over 
implementation details here.  (An extra call is inserted by 
setMethod()  to handle the matching up of argumnets, but that is 
something which may change, and has changed in the history of the 
'methods' package.)

Note that 'x' might be an expression, even foo2(3, y=5).  You need to 
check it is a symbol.

It would seem to me to be much better to use a replacement function 
here (via setReplaceMethod in S4 land) and let the parser do most of 
the basic checking.

Incidentally, at least in modern English, an assignation is (Collins 
English Dictionary)

  	a secret or forbidden arrangement to meet

and it seems rather that you _are_ trying to do something 'secret or 
forbidden'.  (I suspect you mean 'assignment'.)

On Mon, 15 Mar 2010, Christophe Genolini wrote:

> 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
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
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



[Rd] Strange behavior of assign in a S4 method.
Prof Brian Ripley