[Rd] pass by reference for S4

Martin Morgan mtmorgan at fhcrc.org
Fri Jan 23 15:49:27 CET 2009


"Biczok Rudolf" <r.biczok at dkfz-heidelberg.de> writes:

> Hi all;
>
>
> Is it possible to modify the "@" operator that it can handle references
> (or external pointers) 

R has the notion of an external pointer, which is a pointer to C-level
data. Is that what you mean? See ?"externalptr-class" and the 'Writing
R Extensions' manual for details.

More likely you mean that you want to pass references rather than
values. An approach is to create an environment and place objects in
it; environments have pass-by-reference semantics.

  env <- new.env(parent=emptyenv())
  env$myobj <- ...

Environments can be placed in slots of an S4 class, so the contents of
instances of the class will not be copied. You have to pay attention
when creating the S4 instance that each instance gets a new
environment, e.g., defining an 'initialize' method or a class
constructor.

 setClass("MyClass", representation=representation(env="environment"))
 setMethod("initialize", "MyClass", function(.Object, myData, ...) {
     env <- new.env(parent=emptyenv())
     env$myData <- myData
     callNextMethod(.Object, env=env, ...)
 })

and then

> obj <- new("MyClass", myData=1:10)
> obj at env$myData
 [1]  1  2  3  4  5  6  7  8  9 10

R users will be confused by pass-by-reference semantics.  Others might
point you to the R.oo or proto packages.

Martin

> of S4 Object instead of the real Objects?
>
>
> Or is there any technical or compatibility issue which make it
> impossible?
>
>
>  
>
>
> I look forward to hear from you and also hope that you don't hate me for
> my nooby question.
>
>
>  
>
>
> Yours sincerely,
>
>
> Rudolf Biczok
>
>
>  
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M2 B169
Phone: (206) 667-2793



More information about the R-devel mailing list