sref {refer} | R Documentation |
Create a Safer Reference to an Object
Description
Create a reference to an arbitrary R object. See ref
for more details. sref
behaves
similar to ref
, but does not have support for direct operations on the referenced object.
Usage
sref(x)
Arguments
x |
object to be referenced. |
Details
sref
is similar to ref
; it accepts either an R object or an expression, then records
its location. ref
objects prioritize convenience, while sref
objects prioritize clarity and
safety. For example, `[`
and `$`
can be used on a ref
object to access the elements
of the underlying object, while `[<-`
and `$<-`
can be used to overwrite elements within.
These do not work for sref
objects. Furthermore, base mathematical functions such as `+`
and sqrt
also will not automatically dereference before applying.
Examples
x <- 1:10
ref_x <- ref(x)
sref_x <- sref(x)
## These operations will run:
ref_x + 5
ref_x[1:4]
ref_x[7] <- 5
## These operations will not run:
# sref_x + 5
# sref_x[1:4]
# sref_x[7] <- 5