{"id":14067822,"url":"https://github.com/jsugarelli/pointr","last_synced_at":"2025-10-22T04:00:05.727Z","repository":{"id":56935358,"uuid":"272181971","full_name":"jsugarelli/pointr","owner":"jsugarelli","description":"Working comfortably with pointers and shortcuts to R objects","archived":false,"fork":false,"pushed_at":"2020-12-21T10:42:01.000Z","size":8,"stargazers_count":8,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-04T08:36:42.622Z","etag":null,"topics":["pointers","r-lang","r-language","r-package"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jsugarelli.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-14T10:46:01.000Z","updated_at":"2024-09-03T18:28:59.000Z","dependencies_parsed_at":"2022-08-21T05:50:10.839Z","dependency_job_id":null,"html_url":"https://github.com/jsugarelli/pointr","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jsugarelli/pointr","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsugarelli%2Fpointr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsugarelli%2Fpointr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsugarelli%2Fpointr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsugarelli%2Fpointr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jsugarelli","download_url":"https://codeload.github.com/jsugarelli/pointr/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsugarelli%2Fpointr/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267798625,"owners_count":24145727,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-30T02:00:09.044Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["pointers","r-lang","r-language","r-package"],"created_at":"2024-08-13T07:05:47.900Z","updated_at":"2025-10-22T04:00:05.627Z","avatar_url":"https://github.com/jsugarelli.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":[],"readme":"Pointers/shortcuts in R with the ‘pointr’ package\n================\n\n## Overview\n\nR’s built-in copy-on-modify behavior prevents the user from having two\nsymbols always pointing to the same object. Because pointers, as they\nare common in other programming languages, are essentially symbols\n(variables) related to an object that has already another symbol attached\nto it, it is clear that pointers do not fit naturally into R’s language\nconcept.\n\nHowever, pointers would be incredibly useful, e.g. when you work with\ncomplex subsets of dataframes. These complex filtering conditions make\nthe code harder to read and to maintain. For this reason, it would be\ngood to have a kind of ‘abbreviation’ or ‘shortcut’ that lets you write\nsuch filtering conditions more efficiently.\n\nThe **`pointr`** package provides functionality to create pointers to\nany R object easily, including pointers to subsets/selections from\ndataframes.\n\n## Working with `pointr`\n\n### Installing and loading `pointr`\n\nTo install the CRAN version of **`pointr`** from the R console, just\ncall:\n\n``` r\ninstall.packages(\"pointr\", dependencies = TRUE)\n```\n\nBefore using **`pointr`**, it needs to be attached to the package search\npath:\n\n``` r\nlibrary(pointr)\n```\n\nNow, we are ready to go.\n\n### Functions\n\nFrom the user’s perspective, **`pointr`** provides three simple\nfunctions:\n\n  - **`ptr(symbol1, symbol2)`** creates a pointer called `symbol1` to\n    the object in `symbol2`. The function has no return value. The\n    `symbol1` pointer variable is created by the function. Both\n    arguments, `symbol1` and `symbol2`, are strings.\n\n  - **`rm.ptr(symbol1, keep=TRUE)`** removes the pointer. It deletes the\n    hidden access function `.symbol1()`. If `keep == FALSE` it also\n    deletes the pointer variable `symbol1`. If, however `keep == FALSE`\n    a copy of the object that the pointer refers to is stored in the\n    `symbol1` variable. The `symbol1` argument is a string.\n\n  - **`where(symbol1)`** shows the name of the object the pointer\n    `symbol1` points to. The `symbol1` argument is a character vector.\n\nPointers work like the referenced variable itself. You can, for example,\nprint them (which prints the contents of the referenced variable) or\nassign values to them (which assigns the values to the referenced\nvariable).\n\n### Examples\n\n#### Example 1: A simple vector\n\nFirst, we define a variable `myvar` and create a pointer `mypointer` to\nthis variable. Accessing the pointer `mypointer` actually reads `myvar`.\n\n``` r\nmyvar \u003c- 3\nptr(\"mypointer\", \"myvar\")\nmypointer\n```\n\n    ## [1] 3\n\nAccordingly, changes to `myvar` can be seen using the pointer.\n\n``` r\nmyvar \u003c- 5\nmypointer\n```\n\n    ## [1] 5\n\nThe pointer can also be used in assignments; this changes the variables\nthe pointer points to:\n\n``` r\nmypointer \u003c- 7\nmyvar\n```\n\n    ## [1] 7\n\n#### Example 2: Subsetting a dataframe\n\nWe create a simple dataframe:\n\n``` r\ndf \u003c- data.frame(list(var1 = c(1,2,3), var2 = c(\"a\", \"b\", \"c\")), stringsAsFactors = FALSE)\ndf\n```\n\n    ##   var1 var2\n    ## 1    1    a\n    ## 2    2    b\n    ## 3    3    c\n\nNow we set a pointer `sel` to a subset of `df`:\n\n``` r\ni \u003c- 2\nptr(\"sel\", \"df$var2[i]\")\n```\n\nWe can now change…\n\n``` r\nsel \u003c- \"hello\"\ndf$var2[i]\n```\n\n    ## [1] \"hello\"\n\nand read data from `df` using the `sel` pointer:\n\n``` r\ndf$var2[i] \u003c- \"world\"\nsel\n```\n\n    ## [1] \"world\"\n\nWe can also check easily where our pointer points to:\n\n``` r\nwhere.ptr(\"sel\")\n```\n\n    ## [1] \"world\"\n\nWhen the index variable `i` changes, our pointer adjusts accordingly:\n\n``` r\ni \u003c- 3\nsel\n```\n\n    ## [1] \"c\"\n\n## Technical note\n\nActive bindings are used to create the **`pointr`** pointers. For each\npointer an object with active binding is created. Every time the pointer\nis accessed, the active binding calls a hidden function called\n`.pointer` where *`pointer`* is the name of the pointer variable. This\nfunction evaluates the assignment (if the user assigns a value to the\npointer) or evaluates the object the pointer refers to as such (if the\nuser accesses the contents of the object the pointer points to). This\nway it is possible not only to address objects like vectors or\ndataframes but also to have pointers to things like, for example,\nsubsets of datafames.\n\nAll **`pointr`** functions operate in the environment in which the\npointer is created.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsugarelli%2Fpointr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjsugarelli%2Fpointr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsugarelli%2Fpointr/lists"}