ref: creating references

Description Usage Arguments Details Value WARNING R 1.8 WARNING S+ WARNING Historical remarks Note Author(s) See Also Examples

Description

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

Usage

1
ref(name, loc = parent.frame())

Arguments

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+

Details

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.

Value

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"

WARNING

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.

R 1.8 WARNING

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.

S+ WARNING

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).

Historical remarks

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:

  1. advantagepassing by reference can save memory compared to passing by value

  2. disadvantagepassing by reference is more dangerous than passing by value

  3. howeverthe implementation is purely in S, thus rather channels existing danger than adding new danger

  4. restrictionassigning to a subsetted part of a referenced object was inefficient in S+ (was S+ version 3)

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:

  1. no idempotence 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.

  2. no write protection The 1997 suggestion included a write protection attribute of references, allowing for read only references and allowing for references that could only be changed by functions that know the access code. Reasons for this: there is no need for readonly references (due to copy on modify) and oop provides better mechanisms for security.

  3. no static variables The suggestion made in 1997 did include an implementation of static variables realized as special cases of references with a naming convention which reduced the risc of name collisions in the 1997 practice of assigning to frame 0. Now R has namespaces and the oop approach of Henrik Bengtsson using environments is to be prefered over relatively global static objects.

Note

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")

Author(s)

Jens Oehlschl<e4>gel

See Also

as.ref, deref, deref<-, exists.ref, is.ref, print.ref, HanoiTower

Examples

1
2
3
4
5
  v <- 1
  r <- ref("v")
  r
  deref(r)
  cat("For more examples see ?deref\n")

ref documentation built on May 2, 2019, 6:08 p.m.

Related to ref in ref...