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.
It is recommended to use the newer _wrapped
functions that support passing an arbitrary Rcpp::List
as payload. This allows to store an Rcpp::XPtr
to a C++ object in that list. The XPtr
then will be released when the payload is garbage-collected, and the C++ object will be destroyed.
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.
// [[Rcpp::depends(bindrcpp)]]SEXP// [[Rcpp::export]]SEXP
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.
xxx_env_yyy_wrapped()
functions expect a list as payload, and a callback function with List
instead of PAYLOAD
as second argument. This helps controlling the lifetime of objects associated with a callback: these can be placed in an XPtr
inside the List
. The xxx_env_yyy()
functions have been aliased to xxx_env_yyy_typed()
(#7).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.LinkingTo: bindrcpp
and #include <bindrcpp.h>
to access these functions from your package.bindr
: create_env()
and populate_env()
.