Publications by Rcpp Gallery

Passing user-supplied C++ functions

21.01.2013

Baptiste asked on StackOverflow about letting users supply C++ functions for use with Armadillo / RcppArmadillo. This posts helps with an extended answer. There is nothing specific about Armadillo here, this would the same way with Eigen, the GSL or any other library a user wants to support (and provides his or her own as<>() and wrap() converter...

1804 sym R (1244 sym/8 pcs) 2 img

Quick conversion of a list of lists into a data frame

22.01.2013

Data frames are one of R’s distinguishing features. Exposing a list of lists as an array of cases, they make many formal operations such as regression or optimization easy to represent. The R data.frame operation for lists is quite slow, in large part because it exposes a vast amount of functionality. This sample shows one way to write a much f...

1354 sym R (1145 sym/2 pcs) 2 img

Using Boost’s foreach macro

30.01.2013

Boost provides a macro, BOOST_FOREACH, that allows us to easily iterate over elements in a container, similar to what we might do in R with sapply. In particular, it frees us from having to deal with iterators as we do with std::for_each and std::transform. The macro is also compatible with the objects exposed by Rcpp. Side note: C++11 has introd...

1646 sym R (1167 sym/3 pcs) 2 img

Sorting Numeric Vectors in C++ and R

31.01.2013

Consider the problem to sort all elements of the given vector in ascending order. We can simply use the function std::sort from the C++ STL. #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] NumericVector stl_sort(NumericVector x) { NumericVector y = clone(x); std::sort(y.begin(), y.end()); return y; } library(rbenchmark) set...

1972 sym R (2159 sym/5 pcs) 2 img

Using Boost via the new BH package

31.01.2013

Earlier today the new BH package arrived on CRAN. Over the years, Jay Emerson, Michael Kane and I had numerous discussions about a basic Boost infrastructure package providing Boost headers for other CRAN packages. JJ and Romain chipped in as well, and Jay finally took the lead by first creating a repo on R-Forge. And now the package is out, so w...

1872 sym Python (764 sym/3 pcs) 2 img

Fast factor generation with Rcpp

27.02.2013

Recall that factors are really just integer vectors with ‘levels’, i.e., character labels that get mapped to each integer in the vector. How can we take an arbitrary character, integer, numeric, or logical vector and coerce it to a factor with Rcpp? It’s actually quite easy with Rcpp sugar: #include <Rcpp.h> using namespace Rcpp; template ...

2359 sym R (1803 sym/3 pcs) 2 img

Using Rcpp with Boost.Regex for regular expression

01.03.2013

Gabor asked about Rcpp use with regular expression libraries. This post shows a very simple example, based onone of the Boost.RegEx examples. We need to set linker options. This can be as simple as Sys.setenv("PKG_LIBS"="-lboost_regex") With that, the following example can be built: // cf www.boost.org/doc/libs/1_53_0/libs/regex/example/snippets...

710 sym R (1959 sym/3 pcs) 2 img

Generating a multivariate gaussian distribution using RcppArmadillo

12.03.2013

There are many ways to simulate a multivariate gaussian distribution assuming that you can simulate from independent univariate normal distributions. One of the most popular method is based on the Cholesky decomposition. Let’s see how Rcpp and Armadillo perform on this task. #include <RcppArmadillo.h> // [[Rcpp::depends(RcppArmadillo)]] using ...

1233 sym R (1974 sym/4 pcs) 2 img

Using bigmemory with Rcpp

14.03.2013

The bigmemory package allows users to create matrices that are stored on disk, rather than in RAM. When an element is needed, it is read from the disk and cached in RAM. These objects can be much larger than native R matrices. Objects stored as such larger-than-RAM matrices are defined in the big.matrix class and they are designed to behave simil...

2492 sym R (1405 sym/2 pcs) 2 img

Dynamic Wrapping and Recursion with Rcpp

08.04.2013

We can leverage small parts of the R’s C API in order to infer the type of objects directly at the run-time of a function call, and use this information to dynamically wrap objects as needed. We’ll also present an example of recursing through a list. To get a basic familiarity with the main functions exported from R API, I recommend reading H...

2976 sym R (2117 sym/5 pcs) 2 img