From osklyar at ebi.ac.uk  Tue Nov 13 15:07:57 2007
From: osklyar at ebi.ac.uk (Oleg Sklyar)
Date: Tue, 13 Nov 2007 14:07:57 +0000
Subject: [Rd] How to overload the assignment operator?
In-Reply-To: <4739AC35.1080209@stamats.de>
References: <20071113133559.270580@gmx.net>  <4739AC35.1080209@stamats.de>
Message-ID: <1194962877.6631.6.camel@maitai.windows.ebi.ac.uk>

I think the question is not about setReplaceMethod, here is the idea:

## setReplaceMethod-like situation:

> names(x) <- c("a","b")

## the question of interest

> class(x)
[1] myClass
> y <- x ## this to run a user-defined clone method for class 'myClass'

Why is this important - if x contains data that actually needs to be
duplicated this would come handy. I'd like to know the solution as well.
Maybe it's my ignorance that I don't know the answer and the response
will be RTFM - fine, just point out where :)

Best,
Oleg



On Tue, 2007-11-13 at 14:52 +0100, Matthias Kohl wrote:
> are you looking for "setReplaceMethod"?
> hth
> Matthias
> 
> Jens Oehlschl?gel wrote:
> > Dear all,
> >
> > what is the proper way to make the assignment operator generic and define methods depending on the class of the assigned value?
> >
> > Best regards
> >
> >
> > Jens Oehlschl?gel
> >
> > P.S. I vaguely remember that this was possible in S+. In R I tried to no avail: 
> >
> >   # using this like h<-1:3 gives Error: in `<-.default`(h, 1:3) : invalid (do_set) left-hand side to assignment
> >   "<-.default" <- get("<-") 
> >
> >   # using this does fail on subassignments like: h <- 1:3 ; h[1] <- 7 (h still is 1:3)
> >   "<-.default" <- function(x, value){
> >     assign(deparse(substitute(x)), value, parent.frame())
> >     invisible(x)
> >   }
> >
> >   # this seems to work
> >   "<-" <- function(x, value){
> >     UseMethod("<-", value)
> >   }
> >
> >   # whenever the assigned value has class 'ff' I want to do execute something like
> >   "<-.ff" <- function(x, value){
> >     y <- clone(value)
> >     assign(deparse(substitute(x)), y, parent.frame())
> >     y
> >   }
> >
> >
> >   
> >> version
> >>     
> >                _                           
> > platform       i386-pc-mingw32             
> > arch           i386                        
> > os             mingw32                     
> > system         i386, mingw32               
> > status                                     
> > major          2                           
> > minor          6.0                         
> > year           2007                        
> > month          10                          
> > day            03                          
> > svn rev        43063                       
> > language       R                           
> > version.string R version 2.6.0 (2007-10-03)
> >
> > --
> >
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
-- 
Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466



[Rd] How to overload the assignment operator?
Oleg Sklyar