Press "Enter" to skip to content

Pointers/shortcuts in R with the ‘pointr’ package

[This article was first published on Topics in R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)


Want to share your content on R-bloggers? click here if you have a blog, or here if you don’t.

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 (variable) 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 indredibly 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. Thepointr 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.

18 Comments

  1. I am regular visitor, how are you everybody? This article posted at this web site is in fact pleasant.

  2. SMS July 9, 2020

    I love looking through a post that can make people think. Also, many thanks for permitting me to comment!

  3. Like!! I blog quite often and I genuinely thank you for your information. The article has truly peaked my interest.

  4. SMS July 9, 2020

    Your site is very helpful. Many thanks for sharing!

  5. jordan retro July 14, 2020

    I have to show my appreciation to the writer for rescuing me from such a instance. Right after searching throughout the world-wide-web and seeing advice which are not beneficial, I was thinking my life was well over. Being alive minus the strategies to the problems you have sorted out by way of your main post is a critical case, as well as the ones that might have in a wrong way damaged my entire career if I hadn’t discovered the blog. Your actual talents and kindness in touching all the stuff was very useful. I am not sure what I would’ve done if I had not discovered such a subject like this. I’m able to at this moment look forward to my future. Thanks for your time so much for the skilled and effective guide. I won’t be reluctant to recommend the website to any person who needs and wants guide about this issue.

  6. balenciaga sneakers July 17, 2020

    I must express my thanks to you for rescuing me from such a difficulty. As a result of checking throughout the world wide web and seeing recommendations which were not beneficial, I assumed my life was done. Living without the presence of approaches to the issues you have fixed by means of your main guideline is a serious case, and the kind which may have in a negative way damaged my career if I had not come across your site. The skills and kindness in maneuvering all the pieces was valuable. I don’t know what I would’ve done if I had not come upon such a point like this. It’s possible to now relish my future. Thanks a lot very much for your skilled and sensible help. I won’t hesitate to recommend your site to anyone who requires recommendations on this issue.

  7. jordan shoes July 20, 2020

    Thanks a lot for providing individuals with remarkably special opportunity to discover important secrets from this web site. It really is very awesome plus stuffed with amusement for me and my office colleagues to search your web site minimum thrice in one week to find out the new things you will have. And definitely, I’m usually impressed concerning the unbelievable inspiring ideas you serve. Selected two tips in this article are unequivocally the most effective we have all ever had.

  8. kd 12 shoes July 30, 2020

    My husband and i got excited Jordan could deal with his investigation from the precious recommendations he made using your blog. It is now and again perplexing to just choose to be freely giving information and facts that many some others could have been selling. And we take into account we now have the blog owner to appreciate for this. All of the illustrations you’ve made, the straightforward web site menu, the friendships you will make it possible to promote – it’s got most wonderful, and it’s really letting our son in addition to us reckon that this topic is fun, and that is truly pressing. Thanks for the whole lot!

  9. supreme August 2, 2020

    My husband and i were really ecstatic Edward could complete his studies from the ideas he came across through your web pages. It is now and again perplexing to simply possibly be offering points which usually people may have been trying to sell. And we also know we have the blog owner to thank for this. All the illustrations you made, the easy blog menu, the friendships you can aid to foster – it is most great, and it’s really aiding our son in addition to our family imagine that the issue is fun, which is certainly particularly mandatory. Thanks for all the pieces!

  10. supreme new york August 5, 2020

    I wanted to put you a very small observation to finally give thanks again on the awesome opinions you have shared here. It has been unbelievably open-handed with people like you to present openly what exactly a number of us could possibly have supplied for an ebook to generate some money for their own end, mostly now that you might have tried it in case you desired. These good ideas additionally served as a easy way to know that most people have the same dream like my very own to learn somewhat more in regard to this condition. I believe there are millions of more fun moments in the future for folks who scan through your website.

  11. lebron 15 August 7, 2020

    I enjoy you because of all of the labor on this web site. Ellie really loves going through investigations and it is simple to grasp why. All of us notice all of the dynamic way you produce helpful secrets through your web site and inspire response from people on this area of interest then our own girl has been discovering so much. Take advantage of the rest of the new year. You’re performing a fabulous job.

  12. web hosting services August 11, 2020

    I really like your blog.. very nice colors & theme. Did you design this website yourself
    or did you hire someone to do it for you? Plz reply as I’m
    looking to create my own blog and would like
    to know where u got this from. thanks a lot

  13. Fine way of describing, and pleasant post to obtain facts regarding my presentation topic, which i am going to convey in university.

    adreamoftrains webhosting

Leave a Reply

Your email address will not be published.