Re: [Rd] How to overload the assignment operator?

2007-11-13 Thread Matthias Kohl
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  
 day03  
 svn rev43063   
 language   R   
 version.string R version 2.6.0 (2007-10-03)

 --

 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel


__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to overload the assignment operator?

2007-11-13 Thread Oleg Sklyar
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  
  day03  
  svn rev43063   
  language   R   
  version.string R version 2.6.0 (2007-10-03)
 
  --
 
  __
  R-devel@r-project.org mailing list
  https://stat.ethz.ch/mailman/listinfo/r-devel
 
 
 __
 R-devel@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

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to overload the assignment operator?

2007-11-13 Thread Jens Oehlschlägel
Thanks Matthias,

 are you looking for setReplaceMethod?

So far the package I m writing has avoided using anything from S4 and the 
implications of touching S4 are not clear to me. The package aims on providing 
an alternative to 'atomic' data stored in ram, i.e. large atomic data stored on 
disk. I need some advice how to do this maximally performant, which probably 
means pure S3!?

Best regards


Jens Oehlschlägel
--

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to overload the assignment operator?

2007-11-13 Thread Simon Urbanek

On Nov 13, 2007, at 9:19 AM, Jens Oehlschlägel wrote:

 Thanks Matthias,

 are you looking for setReplaceMethod?

 So far the package I m writing has avoided using anything from S4  
 and the implications of touching S4 are not clear to me. The package  
 aims on providing an alternative to 'atomic' data stored in ram,  
 i.e. large atomic data stored on disk. I need some advice how to do  
 this maximally performant, which probably means pure S3!?


You cannot use S3 here, because you want to dispatch on the *second*  
argument.

I don't think you want to do what you described - you would slow down  
everything in R considerably just by making `-` a generic (and in  
fact you cannot do that for a good reason).

Why don't you take the external pointer approach that many others take  
to provide proxy objects to external data? (DB access, mem-mapped  
files, cross-language objects, etc.) That allows you to define your  
storage semantics very efficiently in C. You can still choose to  
define any syntactic sugar you want in either S3 or S4.

Cheers,
Simon

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to overload the assignment operator?

2007-11-13 Thread Prof Brian Ripley

On Tue, 13 Nov 2007, Jens Oehlschlägel wrote:


Thanks Matthias,


are you looking for setReplaceMethod?


So far the package I m writing has avoided using anything from S4 and 
the implications of touching S4 are not clear to me. The package aims on 
providing an alternative to 'atomic' data stored in ram, i.e. large 
atomic data stored on disk. I need some advice how to do this maximally 
performant, which probably means pure S3!?


setReplaceMethod() is just syntactic sugar for setting an S4 method on a 
replacement function (read the function definition to see so).  You can 
set S3 replacement methods, and the S4 mechanism just piggy-backs on that.


I think the conceptual problem is that the assignment operator does not 
actually do any copying: it creates a binding of a symbol to a value. 
Any copying which occurs happens when the value is (potentially) changed. 
There is no provision for that to depend on the class (rather than the 
type) of the object.  Since external pointers are never duplicated, you 
ought to be able to take advantage of that to design the copying semantics 
you want.


--
Brian D. Ripley,  [EMAIL PROTECTED]
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, UKFax:  +44 1865 272595__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to overload the assignment operator?

2007-11-13 Thread Gabor Grothendieck
Check out the g.data package in case that's what you are looking for.  It
uses promises until the data is actually used.

On Nov 13, 2007 9:19 AM, Jens Oehlschlägel [EMAIL PROTECTED] wrote:
 Thanks Matthias,

  are you looking for setReplaceMethod?

 So far the package I m writing has avoided using anything from S4 and the 
 implications of touching S4 are not clear to me. The package aims on 
 providing an alternative to 'atomic' data stored in ram, i.e. large atomic 
 data stored on disk. I need some advice how to do this maximally performant, 
 which probably means pure S3!?

 Best regards


 Jens Oehlschlägel

 --

 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel


__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to overload the assignment operator?

2007-11-13 Thread Jens Oehlschlägel
Thank you Brian,

 setReplaceMethod() is just syntactic sugar for setting an S4 method on a 
 replacement function (read the function definition to see so).  You can 
 set S3 replacement methods, and the S4 mechanism just piggy-backs on that.

So I understand that setReplaceMethod will not help.

 I think the conceptual problem is that the assignment operator does not 
 actually do any copying: it creates a binding of a symbol to a value. 
 Any copying which occurs happens when the value is (potentially) changed. 

I would be even happier if the cloning would only occur on any attempt to 
change the external pointer / proxy-object. 

 There is no provision for that to depend on the class (rather than the 
 type) of the object.  

Mh, unless the internal copying mechanism would call a clone generic for 
non-atomic objects

 Since external pointers are never duplicated, you 
 ought to be able to take advantage of that to design the copying semantics 
 you want.

How that? How do I know that any user has assigned/modified the external 
pointer (or a proxy object containing the external pointer) such that I can 
invoke the cloning?

Best regards


Jens Oehlschlägel
--

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to overload the assignment operator?

2007-11-13 Thread Jens Oehlschlägel
Thanks Simon,

 You cannot use S3 here, because you want to dispatch on the *second*  
 argument.

- - function(x, value)UseMethod(-, value)
DOES dispatch on the second argument (see the dispatchsecond example below)

 Why don't you take the external pointer approach that many others take  
 to provide proxy objects to external data? (DB access, mem-mapped  
 files, cross-language objects, etc.) That allows you to define your  
 storage semantics very efficiently in C. You can still choose to  
 define any syntactic sugar you want in either S3 or S4.

I AM using the external pointer approach and this works verly well - if one 
tolerates that the external data is not duplicated when the proxy object is 
copied. I was wondering however, if it is possible to perfectly mimic R's 
copying semantics: duplicate external object (and the external pointer) on 
assignment (or even better only on modify after assignment).

Best regards

Jens Oehlschlägel

 dispatchsecond - function(first, second)
+ UseMethod(dispatchsecond, second)
 
 dispatchsecond.default - function(first, second){
+ cat(here default\n)
+ }
 
 dispatchsecond.ff - function(first, second){
+ cat(here ff\n)
+ }
 
 default - 1
 ff - 1
 class(ff) - ff
 
 dispatchsecond(1,default)
here default
 dispatchsecond(1,ff)
here ff
 
--

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to overload the assignment operator?

2007-11-13 Thread Jens Oehlschlägel
Thanks Simon,

 You cannot use S3 here, because you want to dispatch on the *second*  
 argument.

- - function(x, value)UseMethod(-, value)
DOES dispatch on the second argument (see the dispatchsecond example below)

 Why don't you take the external pointer approach that many others take  
 to provide proxy objects to external data? (DB access, mem-mapped  
 files, cross-language objects, etc.) That allows you to define your  
 storage semantics very efficiently in C. You can still choose to  
 define any syntactic sugar you want in either S3 or S4.

I AM using the external pointer approach and this works verly well - if one 
tolerates that the external data is not duplicated when the proxy object is 
copied. I was wondering however, if it is possible to perfectly mimic R's 
copying semantics: duplicate external object (and the external pointer) on 
assignment (or even better only on modify after assignment).

Best regards

Jens Oehlschlägel

 dispatchsecond - function(first, second)
+ UseMethod(dispatchsecond, second)
 
 dispatchsecond.default - function(first, second){
+ cat(here default\n)
+ }
 
 dispatchsecond.ff - function(first, second){
+ cat(here ff\n)
+ }
 
 default - 1
 ff - 1
 class(ff) - ff
 
 dispatchsecond(1,default)
here default
 dispatchsecond(1,ff)
here ff
 
--

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to overload the assignment operator?

2007-11-13 Thread Prof Brian Ripley

On Tue, 13 Nov 2007, Jens Oehlschlägel wrote:


Thank you Brian,


setReplaceMethod() is just syntactic sugar for setting an S4 method on a
replacement function (read the function definition to see so).  You can
set S3 replacement methods, and the S4 mechanism just piggy-backs on that.


So I understand that setReplaceMethod will not help.


I think the conceptual problem is that the assignment operator does not
actually do any copying: it creates a binding of a symbol to a value.
Any copying which occurs happens when the value is (potentially) changed.


I would be even happier if the cloning would only occur on any attempt to 
change the external pointer / proxy-object.


There is no provision for that to depend on the class (rather than the
type) of the object.


Mh, unless the internal copying mechanism would call a clone generic for 
non-atomic objects


But as I said, there is no provision for that.  Nor is there going to be. 
One reason is performance, as Simon has hinted.  Another is consistency: 
duplication might well be called from places where class is being ignored.



Since external pointers are never duplicated, you
ought to be able to take advantage of that to design the copying semantics
you want.


How that? How do I know that any user has assigned/modified the external 
pointer (or a proxy object containing the external pointer) such that I can 
invoke the cloning?


I don't even know what you want to do.  But I don't think copying is the 
place to find out if a user changed an object: presumably it was done via 
your interface.



--
Brian D. Ripley,  [EMAIL PROTECTED]
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, UKFax:  +44 1865 272595__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel