Publications by Rcpp Gallery
Extending R with C++ and Fortran
A recent social-media question by James Curran inquired about the best, or recommended ways, to extend R with Fortran code. Part of the question was whether the .Fortran() interface was still recommended or as there is ‘conflicting advice’ out there. Dirk then followed up and pointed to the (stunning!) performance gains reported by glmnet w...
1965 sym R (1060 sym/3 pcs)
STL Transform
The STL transform function can be used to pass a single function over a vector. Here we use a simple function square(). #include <Rcpp.h> using namespace Rcpp; inline double square(double x) { return x*x ; } // [[Rcpp::export]] std::vector<double> transformEx(const std::vector<double>& x) { std::vector<double> y(x.size()); std::transfo...
561 sym R (712 sym/2 pcs)
STL transform + remove_copy for subsetting
We have seen the use of the STL transform functions in the posts STL transform and Transforming a matrix. We use the same logic in conjuction with a logical (ie boolean) vector in order subset an initial vector. #include <Rcpp.h> using namespace Rcpp; using namespace std; const double flagval = __DBL_MIN__; // works //const double flagval = NA_...
780 sym R (914 sym/2 pcs)
STL random_shuffle for permutations
The STL also contains random sampling and shuffling algorithms. We start by looking at random_shuffle. There are two forms. The first uses an internal RNG with its own seed; the second form allows for a function object conformant to the STL’s requirements (essentially, given N produce a uniform draw greater or equal to zero and less than N). Th...
1013 sym R (656 sym/2 pcs)
STL random_sample
An earlier post looked at random shuffle for permutations. The STL also supports creation of random samples. Alas, it seems that this functionality has not been promoted to the C++ standard yet — so we will have to do with what is an extensions by the GNU g++ compiler. The other drawback is the sampling without replacement. As in the previous p...
910 sym R (689 sym/2 pcs)
Sugar Functions head and tail
The R functions head and tail return the first (last) n elements of the input vector. With Rcpp sugar, the functions head and tail work the same way as they do in R. Here we use std::sort from the STL and then tail to return the top n items (items with the highest values) of the input vector. #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::ex...
844 sym R (640 sym/3 pcs)
STL for_each and generalized iteration
The STL contains a very general looping or sweeping construct in the for_each algorith. It can be used with function objects (such as the simple square function used here) but also with custom class which can be used to keep to keep state. #include <Rcpp.h> using namespace Rcpp; // somewhat silly little class derived from unary_function<T, void...
680 sym R (1381 sym/2 pcs)
Armadillo subsetting
A StackOverflow question asked how convert from arma::umat to arma::mat. The former is format used for find and other logical indexing. For the particular example at hand, a call to the conv_to converter provided the solution. We rewrite the answer here using the newer format offered by Rcpp attributes and its sourceCpp() function. #include <Rcpp...
939 sym R (732 sym/3 pcs)
Accessing environments
Extending R with C++ code by using Rcpp typically involves function calls by leveraging the existing .Call() interface of the R API. Passing values back and forth is then done in manner similar to programming with functions. However, on occassion it is useful to access enviroments (such as the global environment). We can also pass environments (w...
1025 sym R (387 sym/3 pcs)
Armadillo eigenvalues
Today a (slightly confused) question on StackOverflow wondered how to access R’s facilities for eigenvalues calculations from C code. For this, we need to step back and consider how this is done. In fact, R farms the calculation out to the BLAS. On could possibly access R’s functions—but would then have to wrestle with the data input/output...
1414 sym R (568 sym/3 pcs)