ref {ref} | R Documentation |
Package ref
implements references for S (R/S+).
Function ref
creates references.
For a memory efficient wrapper to matrixes and data.frames which allows nested subsetting see refdata
ref(name, loc = parent.frame())
name |
name of an (existing) object to be referenced |
loc |
location of the referenced object, i.e. an environment in R or a frame in S+ |
In S (R/S+) paramters are passed by value and not by reference.
When passing big objects, e.g. in recursive algorithms, this can quickly eat up memory.
The functions of package ref
allow to pass references in function calls.
The implementation is purely S and should work in R and S+.
Existence of the referenced object is not checked by function ref
.
Usually as.ref
is more convenient and secure to use.
There is also a print method for references.
a list with
name |
name of the referenced object |
loc |
location of the referenced object, i.e. an environment in R or a frame in S+ |
and class "ref"
Usually functions in S have no side-effects except for the main effect of returning something. Working with references circumvents this programming style and can have considerable side-effects. You are using it at your own risk.
Changing parts of referenced objects has been slowed down by order of magnitudes since R version 1.8, see performance test examples on the help page for deref
.
Hopefully the old performance can be restored in future versions.
Package ref should generally work under R and S+. However, when changing very small parts of referenced objects, using references under S+ might be inefficient (very slow with high temporary memory requirements).
This package goes back to an idea submitted April 9th 1997 and code offered on August 17th 1997 on s-news.
The idea of implementing references in S triggered an intense discussion on s-news. The status reached in 1997 can be summarized as follows:
Due to the last restriction the code was never submitted as a mature library. Now in 2003 we have a stable version of R and astonishingly assigning to a subsetted part of a referenced object can be implemented efficient. This shows what a great job the R core developers have done. In the current version the set of functions for references was dramatically simplified, the main differences to 1997 beeing the following:
deref
and deref<-
now are a simple function and no longer are methods. This decision was made due top performance reasons. As a consequence, deref()
no longer is idempotent: one has to know whether an object is a reference. Function is.ref
provides a test. Using this type of references is fine for prototyping in a non-objectoriented programming style. For bigger projects and safer programming you should consider the approach suggested by Henrik Bengtsson at http://www.maths.lth.se/help/R/ImplementingReferences (announced to be released as package "oo" or "classes")
Jens Oehlschlägel
as.ref
, deref
, deref<-
, exists.ref
, is.ref
, print.ref
, HanoiTower
v <- 1 r <- ref("v") r deref(r) cat("For more examples see ?deref\n")