ptr {pointr} | R Documentation |
Create, remove and analyze pointers in R. Pointers can point to any R object, including selections/subsets.
ptr(symbol1, symbol2)
symbol1 %=% symbol2
rm.ptr(symbol1, keep = FALSE)
where.ptr(symbol1)
symbol1 |
The name of the pointer, as a one-element character vector. |
symbol2 |
The object/selection the pointer will point to, as a one-element character vector. |
keep |
A logical value relevant when removing a pointer with
|
The ptr()
function and the %=%
operator will create
a pointer to an R object, like a vector, list, dataframe or even a
subset/selection from a dataframe. where.ptr()
shows where a pointer
actually points to. Existing pointers can be removed usig the
rm.ptr()
function. Pointers created with pointr use active
bindings that call a hidden access function everytime the pointer is
accessed. This hidden access function is named .pointer()
(where
pointer
is the name of the pointer variable) and is created in the
environment from which ptr()
is called. It is not necessary to call
this hidden access function as a pointer user. The hidden access function
is removed when rm.ptr()
is called.
ptr()
, %=%
and rm.ptr()
have no return value.
ptr()
and %=%
create the pointer variable (argument
symbol1
) in the environment from which it is called.
where.ptr
returns the object/selection a pointer points to as a
character vector.
Thanks to Chad Hammerquist for contributing the
pointr
operator %=%
.
library(pointr)
# Pointer to simple variable
myvar <- 3
ptr("mypointer", "myvar")
mypointer
myvar <- 5
mypointer
mypointer <- 7
myvar
# Alternative: Use the pointr operator %=%
myvar <- 3
mypointr %=% myvar
myvar
# Pointer to subset from dataframe
df <- data.frame(list(var1 = c(1,2,3), var2 = c("a", "b", "c")), stringsAsFactors = FALSE)
df
i <- 2
ptr("sel", "df$var2[i]")
sel <- "hello"
df$var2[i]
df$var2[i] <- "world"
sel
where.ptr("sel")