Title: | Working Comfortably with Pointers and Shortcuts to R Objects |
---|---|
Description: | R has no built-in pointer functionality. The 'pointr' package fills this gap and lets you create pointers to R objects, including subsets of dataframes. This makes your R code more readable and maintainable. |
Authors: | Joachim Zuckarelli [aut, cre] |
Maintainer: | Joachim Zuckarelli <[email protected]> |
License: | GPL-3 |
Version: | 0.2.0 |
Built: | 2025-02-19 05:14:19 UTC |
Source: | https://github.com/jsugarelli/pointr |
The pointr package allows to work with pointers to R
objects/selection in order to make the R code more readable and
maintainable.
The main function of the package are: ptr()
to
create a pointer, rm.ptr()
to remove a pointer, and
where.ptr()
to check the target object of a pointer.
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)
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")
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")