MyNixOS website logo
Description

Working Comfortably with Pointers and Shortcuts to R Objects.

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.

Pointers/shortcuts in R with the ‘pointr’ package

Overview

R’s built-in copy-on-modify behavior prevents the user from having two symbols always pointing to the same object. Because pointers, as they are common in other programming languages, are essentially symbols (variables) related to an object that has already another symbol attached to it, it is clear that pointers do not fit naturally into R’s language concept.

However, pointers would be incredibly useful, e.g. when you work with complex subsets of dataframes. These complex filtering conditions make the code harder to read and to maintain. For this reason, it would be good to have a kind of ‘abbreviation’ or ‘shortcut’ that lets you write such filtering conditions more efficiently.

The pointr package provides functionality to create pointers to any R object easily, including pointers to subsets/selections from dataframes.

Working with pointr

Installing and loading pointr

To install the CRAN version of pointr from the R console, just call:

install.packages("pointr", dependencies = TRUE)

Before using pointr, it needs to be attached to the package search path:

library(pointr)

Now, we are ready to go.

Functions

From the user’s perspective, pointr provides three simple functions:

  • ptr(symbol1, symbol2) creates a pointer called symbol1 to the object in symbol2. The function has no return value. The symbol1 pointer variable is created by the function. Both arguments, symbol1 and symbol2, are strings.

  • rm.ptr(symbol1, keep=TRUE) removes the pointer. It deletes the hidden access function .symbol1(). If keep == FALSE it also deletes the pointer variable symbol1. If, however keep == FALSE a copy of the object that the pointer refers to is stored in the symbol1 variable. The symbol1 argument is a string.

  • where(symbol1) shows the name of the object the pointer symbol1 points to. The symbol1 argument is a character vector.

Pointers work like the referenced variable itself. You can, for example, print them (which prints the contents of the referenced variable) or assign values to them (which assigns the values to the referenced variable).

Examples

Example 1: A simple vector

First, we define a variable myvar and create a pointer mypointer to this variable. Accessing the pointer mypointer actually reads myvar.

myvar <- 3
ptr("mypointer", "myvar")
mypointer
## [1] 3

Accordingly, changes to myvar can be seen using the pointer.

myvar <- 5
mypointer
## [1] 5

The pointer can also be used in assignments; this changes the variables the pointer points to:

mypointer <- 7
myvar
## [1] 7

Example 2: Subsetting a dataframe

We create a simple dataframe:

df <- data.frame(list(var1 = c(1,2,3), var2 = c("a", "b", "c")), stringsAsFactors = FALSE)
df
##   var1 var2
## 1    1    a
## 2    2    b
## 3    3    c

Now we set a pointer sel to a subset of df:

i <- 2
ptr("sel", "df$var2[i]")

We can now change…

sel <- "hello"
df$var2[i]
## [1] "hello"

and read data from df using the sel pointer:

df$var2[i] <- "world"
sel
## [1] "world"

We can also check easily where our pointer points to:

where.ptr("sel")
## [1] "world"

When the index variable i changes, our pointer adjusts accordingly:

i <- 3
sel
## [1] "c"

Technical note

Active bindings are used to create the pointr pointers. For each pointer an object with active binding is created. Every time the pointer is accessed, the active binding calls a hidden function called .pointer where pointer is the name of the pointer variable. This function evaluates the assignment (if the user assigns a value to the pointer) or evaluates the object the pointer refers to as such (if the user accesses the contents of the object the pointer points to). This way it is possible not only to address objects like vectors or dataframes but also to have pointers to things like, for example, subsets of datafames.

All pointr functions operate in the environment in which the pointer is created.

Metadata

Version

0.2.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows