XPtr Add-Ons for 'Rcpp'

Provides the means to compile user-supplied C++ functions with 'Rcpp' and retrieve an 'XPtr' that can be passed to other C++ components.

Build Status Coverage Status CRAN_Status_Badge Downloads

The RcppXPtrUtils package provides the means to compile user-supplied C++ functions with 'Rcpp' and retrieve an XPtr that can be passed to other C++ components.


Install the release version from CRAN:


The installation from GitHub requires the devtools package.


Use case

Let's suppose we have a package with a core written in C++, connected to an R API with Rcpp. It accepts a user-supplied R function to perform some processing:

#include <Rcpp.h>
using namespace Rcpp;
template <typename T>
NumericVector core_processing(T func, double l) {
  double accum = 0;
  for (int i=0; i<1e3; i++)
    accum += sum(as<NumericVector>(func(3, l)));
  return NumericVector(1, accum);
// [[Rcpp::export]] 
NumericVector execute_r(Function func, double l) {
  return core_processing<Function>(func, l);

But calling R from C++ is slow, so we can think about improving the performance by accepting a compiled function. In order to do this, the core can be easily extended to accept an XPtr to a compiled function:

typedef SEXP (*funcPtr)(intdouble);
// [[Rcpp::export]] 
NumericVector execute_cpp(SEXP func_, double l) {
  funcPtr func = *XPtr<funcPtr>(func_);
  return core_processing<funcPtr>(func, l);

To easily leverage this feature, the RcppXPtrUtils package provides cppXPtr(), which compiles a user-supplied C++ function using Rcpp::cppFunction() and returns an XPtr:

# compile the code above
# Rcpp::sourceCpp(code='...')
func_r <- function(n, l) rexp(n, l)
func_cpp <- cppXPtr("SEXP foo(int n, double l) { return rexp(n, l); }")
  execute_r(func_r, 1.5),
  execute_cpp(func_cpp, 1.5)
#> Unit: microseconds
#>                        expr       min         lq       mean     median
#>      execute_r(func_r, 1.5) 13559.538 14823.5195 15968.6778 15492.5525
#>  execute_cpp(func_cpp, 1.5)   169.514   201.2005   264.3814   227.4585
#>          uq       max neval cld
#>  16518.2570 21561.296   100   b
#>    268.2495  1780.512   100  a

The object returned by cppXPtr() is just an externalptr wrapped into an object of class XPtr, which stores the signature of the function. If you are a package author, you probably want to re-export cppXPtr() and ensure that user-supplied C++ functions comply with the internal signatures in order to avoid runtime errors. This can be done with the checkXPtr() function:

#> 'SEXP foo(int n, double l)' <pointer: 0x55c9681442d0>
checkXPtr(func_cpp, "SEXP", c("int", "double")) # returns silently
checkXPtr(func_cpp, "int", c("int", "double"))
#> Error in checkXPtr(func_cpp, "int", c("int", "double")): Bad XPtr signature:
#>   Wrong return type 'SEXP', should be 'int'.
checkXPtr(func_cpp, "SEXP", c("int"))
#> Error in checkXPtr(func_cpp, "SEXP", c("int")): Bad XPtr signature:
#>   Wrong number of arguments, should be 1.
checkXPtr(func_cpp, "SEXP", c("double", "int"))
#> Error in checkXPtr(func_cpp, "SEXP", c("double", "int")): Bad XPtr signature:
#>   Wrong argument type 'int', should be 'double.
#>   Wrong argument type 'double', should be 'int.


Reference manual

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


0.1.1 by Iñaki Ucar, 25 days ago


Report a bug at https://github.com/Enchufa2/RcppXPtrUtils/issues

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

Authors: Iñaki Ucar [aut, cph, cre]

Documentation:   PDF Manual  

MIT + file LICENSE license

Imports Rcpp

Suggests testthat

Imported by SAMCpack.

Suggested by parallelDist.

See at CRAN