Publications by Rcpp Gallery
Calling R Functions from C++
At its very essence, Rcpp permits easy access to native R objects at the C++ level. R objects can be simple vectors, list or matrices; compound data structures created from these; objects of S3, S4 or Reference Class vintage; or language objects as for example environments. Accessing a function object is no different. And calling a function can...
1683 sym R (347 sym/3 pcs)
Performance Benchmark of Running Sum Functions
First, let us consider a running sum function in pure R. To get started, I looked at the source code of the TTR package to see the algorithm used in runSum. The runSum function uses a Fortran routine to compute the running/rolling sum of a vector. The run_sum_R function below is my interpretation of that algorithm implemented in R. Many thanks to...
2817 sym R (3228 sym/4 pcs)
Using the Rcpp Timer
Sine the 0.10.2 release, Rcpp contains an internal class Timer which can be used for fine-grained benchmarking. Romain motivated Timer in a post to the mailing * list where Timer is used to measure the different components of the costs of random number generation. A slightly modified version of that example follows below. #include <Rcpp.h> #inclu...
1225 sym R (738 sym/2 pcs)
Using the Rcpp sugar function clamp
Since the 0.10.* release series, Rcpp contains a new sugar function clamp which can be used to limit vectors to both a minimum and maximim value. This recent StackOverflow question permitted clamp to shine. We retake some of the answers, including the clamp entry by Romain. We first define the three R versions. pminpmaxClamp <- function(x, a, b) ...
1355 sym R (1066 sym/4 pcs)
Handling Strings with Rcpp
This is a quick example of how you might use Rcpp to send and receive R ‘strings’ to and from R. We’ll demonstrate this with a few operations. Sort a String with R Note that we can do this in R in a fairly fast way: my_strings <- c("apples", "and", "cranberries") R_str_sort <- function(strings) { sapply( strings, USE.NAMES=FALSE, function...
2203 sym R (1888 sym/5 pcs)
Using Rcout for output synchronised with R
The Writing R Extensions manual, which provides the gold standard of documentation as far as extending R goes, suggests to use Rprintf and REprintf for output (from C/C++ code) as these are matched to the usual output and error streams maintained by R itself. Also, use of std::cout and std::cerr (as common in standard C++ code) is flagged when ru...
1560 sym R (763 sym/3 pcs)
First steps in using C++11 with Rcpp
The recent release of the C++11 standard has brought a lot of attention to the new language features. Rcpp, as a CRAN package, follows CRAN policy in not (yet!!) supporting the standard for its purported non-portable status. Even as of the current g++ version, we still need to explicitly enable C++11 support which we can do here from R prior to c...
1461 sym R (664 sym/7 pcs)
A first lambda function with C++11 and Rcpp
Yesterday’s post started to explore the nice additions which the new C++11 standard is bringing to the language. One particularly interesting feature are lambda functions which resemble the anonymous functions R programmers have enjoyed all along. This shows a simple example. First, we again make sure the compiler knows that we want C++11: Sys....
1497 sym R (759 sym/5 pcs)
Using Eigen for eigenvalues
A previous post showed how to compute eigenvalues using the Armadillo library via RcppArmadillo. Here, we do the same using Eigen and the RcppEigen package. #include <RcppEigen.h> // [[Rcpp::depends(RcppEigen)]] using Eigen::Map; // 'maps' rather than copies using Eigen::MatrixXd; // variable size matrix, double...
792 sym R (879 sym/3 pcs)
Getting attributes to use xts objects
An earlier post illustrated that R object attributes can be set at the C++ level. Naturally, we can also read them from an object. This proves particularly useful for xts objects which are, in essence, numerical matrices with added attributed that are used by a rich set of R operators and functions. Here, we show how to access these attributes. #...
1297 sym R (1402 sym/4 pcs)