[R] mutable objects

Duncan Temple Lang duncan at research.bell-labs.com
Mon Jul 15 21:25:44 CEST 2002


Vadim Ogranovich wrote:
> Hi,
> 
> I need "mutable" objects that I can safely modify in my C code. All call
> interfaces, e.g. .C, .Call, etc., require you to not modify the "in" args.

I took a quick look at the code for .Call() and it appears not to copy
its arguments. As a result, however, you can use this to modify objects in
place and they effects will persist. This is not "recommended" but
does work for many purposes.

A simple example where one can see  this effect is 
with the following C code.

#include "Rinternals.h"

SEXP
foo(SEXP x, SEXP y)
{
  int n = LENGTH(x), i;

  for(i = 0; i < n; i++) {
          REAL(x)[i] += 1.0;
          REAL(y)[i] *= REAL(y)[i];
  }

  return(x);
}

Calling this from R as 

 x <- as.numeric(1:10)
 y <- as.numeric(1:10)
 z <- .Call("foo", x, y)

Then look at the values of x, y and z.



The .Call() is currently incompatabile with S4 & S-Plus 6, I believe.
We should probably correct this by adding a COPY argument and making
duplication the default behavior.


The OOP package provides both i) a way for creating mutable objects
using environments and ii) a formal mechanism for specifying classes
and inheritance from those classes. It sounds like you (currently)
only need environments. The package is available from 
   http://www.omegahat.org/OOP

 D.



> The description of the OOP package in R-news from Sep 2001 alludes to a
> possiblity of such objects, but the package itself is no longer on Omegahat
> and I am not sure I need the full machinery.
> 
> Thanks, Vadim
> 
> 
> P.S. I wonder what is the relationship between the "methods" package and the
> OOP package described in R-news from Sep 2001.
> 
> -------------------------------------------------- 
> DISCLAIMER 
> This e-mail, and any attachments thereto, is intended only for use by the
> addressee(s) named herein and may contain legally privileged and/or
> confidential information.  If you are not the intended recipient of this
> e-mail, you are hereby notified that any dissemination, distribution or
> copying of this e-mail, and any attachments thereto, is strictly prohibited.
> If you have received this e-mail in error, please immediately notify me and
> permanently delete the original and any copy of any e-mail and any printout
> thereof. 
> 
> E-mail transmission cannot be guaranteed to be secure or error-free.  The
> sender therefore does not accept liability for any errors or omissions in
> the contents of this message which arise as a result of e-mail transmission.
> 
> NOTICE regarding privacy and confidentiality 
> 
> Knight Trading Group may, at its discretion, monitor and review the content
> of all e-mail communications. 
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
_______________________________________________________________

Duncan Temple Lang                duncan at research.bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-3217
700 Mountain Avenue, Room 2C-259  fax:    (908)582-3340
Murray Hill, NJ  07974-2070       
         http://cm.bell-labs.com/stat/duncan
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list