An 'Rcpp' Interface to Active Bindings

Provides an easy way to fill an environment with active bindings that call a C++ function.


It's easy to create active bindings in R via makeActiveBinding(). This package faciliates the creation of active bindings that link back to C++ code. It provides an interface that allows binding several identifiers in an environment to the same C++ function, which is then called with the name (and a payload) as argument.

You can install bindrcpp from github with:

devtools::install_github("krlmlr/bindrcpp")

The following C++ module exports a function test_tolower_bindings() that creates active bindings that return the binding name in lowercase.

#include <Rcpp.h>
 
// [[Rcpp::depends(bindrcpp)]] 
#include <bindrcpp.h>
 
#include <algorithm>
#include <string>
 
using namespace Rcpp;
 
using namespace bindrcpp;
 
SEXP tolower_callback(const String& name, PAYLOAD) {
  std::string name_string = name;
  std::transform(name_string.begin(), name_string.end(), name_string.begin(), ::tolower);
  return CharacterVector(name_string);
}
 
// [[Rcpp::export]] 
SEXP test_tolower_bindings(CharacterVector names, Environment parent) {
  // We don't pass any payload here 
  return bindrcpp::create_env_string(
    names, &tolower_callback, PAYLOAD(NULL), parent);
}

This function can be called from R:

env <- test_tolower_bindings(c("Converting", "to", "LOWERCASE"), .GlobalEnv)
ls(env)
#> [1] "Converting" "LOWERCASE"  "to"
env$Converting
#> [1] "converting"
env$to
#> [1] "to"
env$LOWERCASE
#> [1] "lowercase"
env$y
#> NULL

The bindings are read-only:

env$Converting <- "CONVERTING"
#> Error: Binding is read-only.

News

bindrcpp 0.1 (2016-12-08)

Initial CRAN release.

  • create_env_string() creates an environment with active bindings, with names given as a character vector. Access of these bindings triggers a call to a C++ function with a fixed signature (GETTER_FUNC_STRING); this call contains the name of the binding (as character) and an arbitrary payload (PAYLOAD, essentially a wrapped void*).
  • create_env_symbol() is similar, the callback function accepts the name of the binding as symbol instead of character (GETTER_FUNC_SYMBOL).
  • populate_env_string() and populate_env_symbol() populate an existing environment instead of creating a new one.
  • Use LinkingTo: bindrcpp and #include <bindrcpp.h> to access these functions from your package.
  • Reexported from bindr: create_env() and populate_env().

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("bindrcpp")

0.2 by Kirill Müller, 5 months ago


https://github.com/krlmlr/bindrcpp, https://krlmlr.github.io/bindrcpp


Report a bug at https://github.com/krlmlr/bindrcpp/issues


Browse source code at https://github.com/cran/bindrcpp


Authors: Kirill Müller [aut, cre], RStudio [cph]


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports Rcpp, bindr

Suggests testthat

Linking to Rcpp, plogr


Imported by dplyr.


See at CRAN