[Rd] allocVector reference

Andrew Piskorski atp at piskorski.com
Thu Mar 15 13:01:49 CET 2007

On Wed, Mar 14, 2007 at 05:44:04PM -0000, Matthew Dowle wrote:
> Hi,
> I'm trying to write a function to return an R vector which points
> directly to a contiguous subset of another vector, without taking a
> copy.

Matthew, I don't know the answer to your question, but this all seems
related to support for references in R.  I've included my notes on R
references below.

Ah, I think Jens Oehlschlaegel's "ref" package is what you want:


  Class refdata is a transparent wrapper to matrices and data.frames
  which allows for memory efficient nested subsetting. I.e. you can
  create a subset of a subset ... of a data.frame without duplicating
  the data in memory, instead only indexing information is duplicated.

> Since SEXPREC is a header followed by the data, rather than the
> header plus a pointer to the data, I'm not sure what I'm trying to
> do is possible.  Is there a way of doing this?  Similar in spirit to
> how the R assignment "x=y" does not copy y until x is modified,

Is you last statement above in fact true?  I was under the impression
that R does NOT do copy on write, that when you make a copy of a
variable, R immediately allocates memory and makes a deep copy of the

But you're using old deprecated "=" for assignment, which is weird, so
maybe you mean when you pass named arguments to a function?  Function
args are evaluated lazily, and I think that is used (I don't know how
exactly) to give copy on write behavior - but only for function

My (older) notes on R references:

R does not support reference variables and does not do copy on write -
when you copy a variable, it eagerly allocates all memory and makes a
deep copy.  (I believe S-Plus has the same behavior but have not
checked.)  This can be annoying...

There are however specific exceptions.  For example, R environments
are treated as references, NOT values like most everything else in R.
The July 2006 "attributes of environments" thread makes that clear:


Jens Oehlschlaegel's ref package implements references for both R and


Henrik Bengtsson's R.oo package emulates reference variables via R
environments (but perhaps only in the context of his OO framework, I'm
not sure).


Bengtsson also wrote a 2002 paper, "Implementing support for
references in R":


Further out, see also Tierny's (mostly old) R development notes

  Notes on References, External Objects, or Mutable State for R:
  Simple References with Finalization:
  Finalization and Weak References in R:

Andrew Piskorski <atp at piskorski.com>

More information about the R-devel mailing list