Publications by Rcpp Gallery
Introduction to exception handling
One of the many features that make C++ different from C is exception handling. This is a somewhat big topic, and large codebases sometimes eschew exceptions for lack of traceability in truly large programs (and eg the Google in-house C++ style guide is a well-known example of the Just say no school). Opinions are divided; exceptions are generally...
1913 sym R (1216 sym/5 pcs) 2 img
A first example of using Boost
Boost is, to quote the quote by Sutter and Alexandrescu which adornes the Boost website, …one of the most highly regarded and expertly designed C++ library projects in the world. The impact of Boost on C++ cannot be overstated. Boost is, at its core, a collection of thoroughly designed and peer-reviewed libraries. Some of these have been includ...
1744 sym R (781 sym/3 pcs) 2 img
A second example of using Boost
We introduced Boost in a first post doing some integer math. In this post we want to look at the very versatile Boost.Lexical_Cast library to convert text to numbers – see the Motivation for more. As before, I should note that I write this post on a machine with Boost headers in a standard system location. So stuff just works. If you have to in...
1036 sym R (624 sym/2 pcs) 2 img
Timing normal RNGs
In previous articles, we have seen that Rcpp can be particularly useful for simulations as it executes code at C++ speed. A very useful feature the API provided by R is the access to the R RNGs so that simulations at the C++ level can get precisely the same stream of random numbers as an R application would. But sometimes that is not a requiremen...
1678 sym R (1969 sym/8 pcs) 2 img
Creating xts objects from source
A recent post showed how to access the attributes of an xts object. We used an xts object as these are powerful and popular—but any R object using attributed could be used to illustrate the point. In this short post, we show how one can also do the inverse in order to create an xts object at the C++ source level. We use a somewhat useless objec...
1201 sym R (1423 sym/3 pcs) 2 img
Using the GSL to compute eigenvalues
Two posts showed how to compute eigenvalues using Armadillo and using Eigen. As we also looked at using theGNU GSL, this post will show how to conpute eigenvalues using GSL. As mentioned in the previous GSL post, we instantiate C language pointers suitable for GSL (here the matrix M). Those must be freed manually, as shown before the return state...
902 sym R (1221 sym/3 pcs) 2 img
Using Rcpp to access the C API of xts
The xts package by Jeff Ryan and Josh Ulrich is an immensely powerful tool that is widely used for timeseries work with R. Recently, the question about how to use it from Rcpp came up on StackOverflow and in a thread on the rcpp-devel list. In fact, xts has had an exposed API since 2008, but it wasn’t used and as I found out also not quite for ...
1936 sym R (1642 sym/3 pcs) 2 img
Robust Estimators of Location and Scale
First, the median_Rcpp function is defined to compute the median of the given input vector. It is assumed that the input vector is unsorted, so a copy of the input vector is made using clone and then std::nth_element is used to access the nth sorted element. Since we only care about accessing one sorted element of the vector for an odd length vec...
1162 sym R (1403 sym/2 pcs) 2 img
Coercion of matrix to sparse matrix (dgCMatrix) and maintaining dimnames.
Consider the following matrix nr <- nc <- 6 set.seed <- 123 m <- matrix(sample(c(rep(0,9), 1),nr*nc, replace=T), nrow=nr, ncol=nc) sum(m)/length(m) [1] 0.1667 dimnames(m) <- list(letters[1:nr], letters[1:nc]) m a b c d e f a 0 0 0 0 0 1 b 0 0 0 1 0 1 c 0 0 0 0 0 0 d 0 0 0 0 0 0 e 1 1 0 0 0 0 f 0 0 0 1 0 0 This matrix can be coerced to a sp...
705 sym R (4093 sym/5 pcs) 2 img
Custom as and wrap converters example
The RcppBDT package interfaces Boost.Date_Time with R. Both systems have their own date representations—and this provides a nice example of custom as<>() and wrap() converters. Here, we show a simplified example. We start with the forward declarations: #include <RcppCommon.h> #include <boost/date_time/gregorian/gregorian_types.hpp> // Gregori...
1162 sym R (1261 sym/4 pcs) 2 img