All Projects → Enchufa2 → RcppXPtrUtils

Enchufa2 / RcppXPtrUtils

Licence: other
XPtr Add-Ons for 'Rcpp'

Programming Languages

r
7636 projects
C++
36643 projects - #6 most used programming language

Projects that are alternatives of or similar to RcppXPtrUtils

rcppredis
R interface to Redis using the hiredis library
Stars: ✭ 45 (+164.71%)
Mutual labels:  cran, rcpp
inline
Inline C, C++ or Fortran functions in R
Stars: ✭ 33 (+94.12%)
Mutual labels:  cran, rcpp
rcppfastfloat
Rcpp Bindings for the 'fastfloat' Header-Only Library
Stars: ✭ 18 (+5.88%)
Mutual labels:  cran, rcpp
rcppgsl
Rcpp integration for GNU GSL vectors and matrices
Stars: ✭ 28 (+64.71%)
Mutual labels:  cran, rcpp
rcpp progress
RcppProgress R package: An interruptible progress bar with OpenMP support for c++ in R packages
Stars: ✭ 26 (+52.94%)
Mutual labels:  rcpp
thinkr
Some tools for cleaning up messy 'Excel' files to be suitable for R
Stars: ✭ 21 (+23.53%)
Mutual labels:  cran
TSP
Traveling Salesperson Problem - R package
Stars: ✭ 54 (+217.65%)
Mutual labels:  cran
vosonSML
R package for collecting social media data and creating networks for analysis.
Stars: ✭ 65 (+282.35%)
Mutual labels:  cran
DGCA
Differential Gene Correlation Analysis
Stars: ✭ 32 (+88.24%)
Mutual labels:  cran
PopED
Population Experimental Design (PopED) in R
Stars: ✭ 27 (+58.82%)
Mutual labels:  cran
D3partitionR
R package to visualise interactively hierarchical data.
Stars: ✭ 36 (+111.76%)
Mutual labels:  cran
rTRNG
R package providing access and examples to TRNG C++ library
Stars: ✭ 17 (+0%)
Mutual labels:  rcpp
apsimx
R package for APSIM-X
Stars: ✭ 30 (+76.47%)
Mutual labels:  cran
fpp3package
All data sets required for the examples and exercises in the book "Forecasting: principles and practice" (3rd ed, 2020) by Rob J Hyndman and George Athanasopoulos <http://OTexts.org/fpp3/>. All packages required to run the examples are also loaded.
Stars: ✭ 93 (+447.06%)
Mutual labels:  cran
Ramble
A R parser based on combinatory parsers.
Stars: ✭ 19 (+11.76%)
Mutual labels:  cran
rcpptoml
Rcpp Bindings to C++ parser for TOML files
Stars: ✭ 26 (+52.94%)
Mutual labels:  cran
bruceR
📦 BRoadly Useful Convenient and Efficient R functions that BRing Users Concise and Elegant R data analyses.
Stars: ✭ 110 (+547.06%)
Mutual labels:  cran
ABCoptim
An implementation of the Artificial Bee Colony (ABC) Algorithm
Stars: ✭ 26 (+52.94%)
Mutual labels:  rcpp
globals
🌐 R package: Identify Global Objects in R Expressions
Stars: ✭ 27 (+58.82%)
Mutual labels:  cran
ctrdata
Aggregate and analyse information on clinical trials from public registers
Stars: ✭ 26 (+52.94%)
Mutual labels:  cran

RcppXPtrUtils: XPtr Add-Ons for ‘Rcpp’

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.

Installation

Install the release version from CRAN:

install.packages("RcppXPtrUtils")

The installation from GitHub can be done with the remotes package:

remotes::install_github("Enchufa2/RcppXPtrUtils")

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)(int, double);

// [[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='...')

library(RcppXPtrUtils)

func_r <- function(n, l) rexp(n, l)
func_cpp <- cppXPtr("SEXP foo(int n, double l) { return rexp(n, l); }")

microbenchmark::microbenchmark(
  execute_r(func_r, 1.5),
  execute_cpp(func_cpp, 1.5)
)
#> Unit: microseconds
#>                        expr       min        lq       mean     median        uq
#>      execute_r(func_r, 1.5) 14910.161 16261.928 17628.8078 17468.1140 18635.388
#>  execute_cpp(func_cpp, 1.5)   213.123   223.125   310.2708   237.0265   279.808
#>        max neval cld
#>  22657.568   100   b
#>   2417.878   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:

func_cpp
#> 'SEXP foo(int n, double l)' <pointer: 0x55909eb28830>
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 'int', should be 'SEXP'.
checkXPtr(func_cpp, "SEXP", c("int"))
#> Error in checkXPtr(func_cpp, "SEXP", c("int")): Bad XPtr signature:
#>   Wrong number of arguments, should be 2'.
checkXPtr(func_cpp, "SEXP", c("double", "int"))
#> Error in checkXPtr(func_cpp, "SEXP", c("double", "int")): Bad XPtr signature:
#>   Wrong argument type 'double', should be 'int'.
#>   Wrong argument type 'int', should be 'double'.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].