bindr

Parametrized active bindings

https://github.com/krlmlr/bindr

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.8%) to scientific vocabulary
Last synced: 10 months ago · JSON representation

Repository

Parametrized active bindings

Basic Info
Statistics
  • Stars: 28
  • Watchers: 3
  • Forks: 0
  • Open Issues: 5
  • Releases: 2
Created over 9 years ago · Last pushed 10 months ago
Metadata Files
Readme Changelog License

README.Rmd

---
output:
  github_document:
    html_preview: false
---



```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "README-"
)

pkgload::load_all()

clean_output <- function(x, options) {
  index <- x
  index <- gsub("─", "-", index)
  index <- strsplit(paste(index, collapse = "\n"), "\n---\n")[[1]][[2]]
  writeLines(index, "index.md")

  x <- fansi::strip_sgr(x)
  x
}

options(cli.num_colors = 256)

local({
  hook_source <- knitr::knit_hooks$get("document")
  knitr::knit_hooks$set(document = clean_output)
})
```

# bindr 


[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html)
[![R build status](https://github.com/krlmlr/bindr/workflows/rcc/badge.svg)](https://github.com/krlmlr/bindr/actions)
[![Coverage Status](https://img.shields.io/codecov/c/github/krlmlr/bindr/master.svg)](https://app.codecov.io/github/krlmlr/bindr?branch=master) [![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/bindr)](https://cran.r-project.org/package=bindr)



Active bindings in R are much like properties in other languages:
They look like a variable,
but querying or setting the value triggers a function call.
They can be created in R via
[`makeActiveBinding()`](https://www.rdocumentation.org/packages/base/versions/3.3.1/topics/bindenv),
but with this API the function used to compute or change the value of a binding cannot take additional arguments.
The `bindr` package faciliates the creation of active bindings that are linked to a function that receives the binding name,
and an arbitrary number of additional arguments.


## Installation

You can install `bindr` from GitHub with:

```{r gh-installation, eval = FALSE}
# install.packages("devtools")
devtools::install_github("krlmlr/bindr")
```


## Getting started

For illustration, the `append_random()` function is used.
This function appends a separator (a dash by default) and a random letter
to its input, and talks about it, too.

```{r append-random}
set.seed(20161510)
append_random <- function(x, sep = "-") {
  message("Evaluating append_random(sep = ", deparse(sep), ")")
  paste(x, sample(letters, 1), sep = sep)
}

append_random("a")
append_random("X", sep = "+")
```

In this example, we create an environment that contains bindings
for all lowercase letters, which are evaluated with `append_random()`.
As a result, a dash and a random letter are appended to the name of the binding:

```{r create}
library(bindr)
env <- create_env(letters, append_random)
ls(env)
env$a
env$a
env$a
env$c
env$Z
```

Bindings can also be added to existing environments:

```{r populate}
populate_env(env, LETTERS, append_random, "+")
env$a
env$Z
```


## Further properties

Both named and unnamed arguments are supported:

```{r named-unnamed}
create_env("binding", paste, "value", sep = "-")$binding
```

A parent environment can be specified for creation:

```{r parent-env}
env2 <- create_env("a", identity, .enclos = env)
env2$a
env2$b
get("b", env2)
```

The bindings by default have access to the calling environment:

```{r env-access}
create_local_env <- function(names) {
  paste_with_dash <- function(...) paste(..., sep = "-")
  binder <- function(name, append) paste_with_dash(name, append)
  create_env(names, binder, append = "appending")
}

env3 <- create_local_env("a")
env3$a
```

All bindings are read-only:

```{r failing, error=TRUE}
env3$a <- NA
env3$a <- NULL
```


Existing variables or bindings are not overwritten:

```{r overwrite, error=TRUE}
env4 <- as.environment(list(a = 5))
populate_env(env4, list(quote(b)), identity)
ls(env4)
populate_env(env4, letters, identity)
```


## Active bindings and C++

Active bindings must be R functions.
To interface with C++ code, one must bind against an exported Rcpp function, possibly with `rng = false` if performance matters.
The [`bindrcpp`](https://github.com/krlmlr/bindrcpp#readme) package
uses `bindr` to provide an easy-to-use C++ interface for parametrized active bindings,
and is the recommended way to interface with C++ code.
In the remainder of this section,
an alternative using an exported C++ function is shown.

The following C++ module exports a function `change_case(to_upper = FALSE)`,
which is bound against in R code later.

```{Rcpp cpp-mod, cache = TRUE}
#include 

#include 
#include 

using namespace Rcpp;

// [[Rcpp::export(rng = FALSE)]]
SEXP change_case(Symbol name, bool to_upper = false) {
  std::string name_string = name.c_str();
  std::transform(name_string.begin(), name_string.end(),
                 name_string.begin(), to_upper ? ::toupper : ::tolower);
  return CharacterVector(name_string);
}
```

Binding from R:

```{r bind-cpp-from-r}
env <- create_env(list(as.name("__ToLower__")), change_case)
populate_env(env, list(as.name("__tOuPPER__")), change_case, TRUE)
ls(env)
env$`__ToLower__`
get("__tOuPPER__", env)
```

Owner

  • Name: Kirill Müller
  • Login: krlmlr
  • Kind: user
  • Company: @cynkra

GitHub Events

Total
  • Create event: 43
  • Issues event: 1
  • Release event: 1
  • Watch event: 1
  • Delete event: 33
  • Push event: 113
  • Pull request event: 70
Last Year
  • Create event: 43
  • Issues event: 1
  • Release event: 1
  • Watch event: 1
  • Delete event: 33
  • Push event: 113
  • Pull request event: 70

Committers

Last synced: almost 2 years ago

All Time
  • Total Commits: 113
  • Total Committers: 3
  • Avg Commits per committer: 37.667
  • Development Distribution Score (DDS): 0.204
Past Year
  • Commits: 23
  • Committers: 2
  • Avg Commits per committer: 11.5
  • Development Distribution Score (DDS): 0.348
Top Committers
Name Email Commits
Kirill Müller k****r@m****g 90
Kirill Müller k****l@c****m 15
krlmlr k****r 8
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 10 months ago

All Time
  • Total issues: 4
  • Total pull requests: 75
  • Average time to close issues: N/A
  • Average time to close pull requests: 4 days
  • Total issue authors: 2
  • Total pull request authors: 2
  • Average comments per issue: 0.0
  • Average comments per pull request: 0.01
  • Merged pull requests: 68
  • Bot issues: 0
  • Bot pull requests: 26
Past Year
  • Issues: 2
  • Pull requests: 74
  • Average time to close issues: N/A
  • Average time to close pull requests: 4 minutes
  • Issue authors: 2
  • Pull request authors: 2
  • Average comments per issue: 0.0
  • Average comments per pull request: 0.0
  • Merged pull requests: 68
  • Bot issues: 0
  • Bot pull requests: 26
Top Authors
Issue Authors
  • krlmlr (3)
  • hadley (1)
Pull Request Authors
  • krlmlr (49)
  • github-actions[bot] (26)
Top Labels
Issue Labels
Pull Request Labels
CRAN release :station: (4)

Packages

  • Total packages: 2
  • Total downloads:
    • cran 1,958 last-month
  • Total docker downloads: 232,329
  • Total dependent packages: 3
    (may contain duplicates)
  • Total dependent repositories: 28
    (may contain duplicates)
  • Total versions: 5
  • Total maintainers: 1
cran.r-project.org: bindr

Parametrized Active Bindings

  • Versions: 3
  • Dependent Packages: 2
  • Dependent Repositories: 15
  • Downloads: 1,958 Last month
  • Docker Downloads: 232,329
Rankings
Dependent repos count: 7.4%
Downloads: 8.6%
Stargazers count: 10.1%
Dependent packages count: 13.6%
Average: 14.8%
Docker downloads count: 21.2%
Forks count: 27.8%
Maintainers (1)
Last synced: 10 months ago
conda-forge.org: r-bindr
  • Versions: 2
  • Dependent Packages: 1
  • Dependent Repositories: 13
Rankings
Dependent repos count: 9.8%
Dependent packages count: 29.0%
Average: 37.9%
Stargazers count: 46.7%
Forks count: 66.1%
Last synced: 10 months ago

Dependencies

.github/workflows/R-CMD-check-dev.yaml actions
  • ./.github/workflows/check * composite
  • ./.github/workflows/custom/after-install * composite
  • ./.github/workflows/custom/before-install * composite
  • ./.github/workflows/dep-matrix * composite
  • ./.github/workflows/install * composite
  • ./.github/workflows/rate-limit * composite
  • ./.github/workflows/update-snapshots * composite
  • actions/checkout v4 composite
  • r-lib/actions/setup-r v2 composite
.github/workflows/R-CMD-check.yaml actions
  • ./.github/workflows/check * composite
  • ./.github/workflows/commit * composite
  • ./.github/workflows/custom/after-install * composite
  • ./.github/workflows/custom/before-install * composite
  • ./.github/workflows/dep-matrix-suggests * composite
  • ./.github/workflows/git-identity * composite
  • ./.github/workflows/install * composite
  • ./.github/workflows/pkgdown-build * composite
  • ./.github/workflows/pkgdown-deploy * composite
  • ./.github/workflows/rate-limit * composite
  • ./.github/workflows/roxygenize * composite
  • ./.github/workflows/style * composite
  • ./.github/workflows/update-snapshots * composite
  • actions/checkout v4 composite
  • r-lib/actions/setup-r v2 composite
.github/workflows/check/action.yml actions
  • actions/upload-artifact main composite
  • r-lib/actions/check-r-package v2 composite
.github/workflows/commit/action.yml actions
.github/workflows/dep-matrix/action.yml actions
.github/workflows/dep-matrix-suggests/action.yml actions
.github/workflows/fledge.yaml actions
  • ./.github/workflows/git-identity * composite
  • ./.github/workflows/install * composite
  • actions/checkout v4 composite
.github/workflows/get-extra/action.yml actions
.github/workflows/git-identity/action.yml actions
.github/workflows/install/action.yml actions
  • ./.github/workflows/get-extra * composite
  • krlmlr/ccache-action parallel-dir composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/lock.yaml actions
  • dessant/lock-threads v4 composite
.github/workflows/pkgdown-build/action.yml actions
.github/workflows/pkgdown-deploy/action.yml actions
  • nick-fields/retry v2 composite
.github/workflows/pkgdown.yaml actions
  • ./.github/workflows/custom/after-install * composite
  • ./.github/workflows/custom/before-install * composite
  • ./.github/workflows/git-identity * composite
  • ./.github/workflows/install * composite
  • ./.github/workflows/pkgdown-build * composite
  • ./.github/workflows/pkgdown-deploy * composite
  • ./.github/workflows/rate-limit * composite
  • actions/checkout v4 composite
.github/workflows/pr-commands.yaml actions
  • actions/checkout v4 composite
  • r-lib/actions/pr-fetch master composite
  • r-lib/actions/pr-push master composite
  • r-lib/actions/setup-r master composite
.github/workflows/rate-limit/action.yml actions
.github/workflows/revdep.yaml actions
  • actions/checkout v4 composite
  • actions/upload-artifact main composite
  • r-lib/actions/setup-pandoc v2 composite
.github/workflows/roxygenize/action.yml actions
.github/workflows/style/action.yml actions
  • actions/cache v3 composite
.github/workflows/update-snapshots/action.yml actions
  • peter-evans/create-pull-request v5 composite
DESCRIPTION cran
  • testthat * suggests