Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

is_reference

Is an object referencing another?


Description

There are typically two situations where two symbols may refer to the same object.

  • R objects usually have copy-on-write semantics. This is an optimisation that ensures that objects are only copied if needed. When you copy a vector, no memory is actually copied until you modify either the original object or the copy is modified.

    Note that the copy-on-write optimisation is an implementation detail that is not guaranteed by the specification of the R language.

  • Assigning an uncopyable object (like an environment) creates a reference. These objects are never copied even if you modify one of the references.

Usage

is_reference(x, y)

Arguments

x, y

R objects.

Examples

# Reassigning an uncopyable object such as an environment creates a
# reference:
env <- env()
ref <- env
is_reference(ref, env)

# Due to copy-on-write optimisation, a copied vector can
# temporarily reference the original vector:
vec <- 1:10
copy <- vec
is_reference(copy, vec)

# Once you modify on of them, the copy is triggered in the
# background and the objects cease to reference each other:
vec[[1]] <- 100
is_reference(copy, vec)

rlang

Functions for Base Types and Core R and 'Tidyverse' Features

v0.4.11
MIT + file LICENSE
Authors
Lionel Henry [aut, cre], Hadley Wickham [aut], mikefc [cph] (Hash implementation based on Mike's xxhashlite), Yann Collet [cph] (Author of the embedded xxHash library), RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.