Learn R Programming

ref (version 0.95)

ref: creating references

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

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+

Value

  • a list with
  • namename of the referenced object
  • loclocation 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. advantage
{passing by reference can save memory compared to passing by value} disadvantage{passing by reference is more dangerous than passing by value} however{the implementation is purely in S, thus rather channels existing danger than adding new danger} restriction{assigning to a subsetted part of a referenced object was inefficient in S+ (was S+ version 3)}

emph

can

enumerate

  1. no idempotence

code

is.ref

item

  • no write protection
  • no static variables

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.

See Also

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

Examples

Run this code
v <- 1
  r <- ref("v")
  r
  deref(r)
  cat("For more examples see ?deref
")

Run the code above in your browser using DataLab