Publications by Rcpp Gallery
Using RcppParallel to aggregate to a vector
This article demonstrates using the RcppParallel package to aggregate to an output vector. It extends directly from previous demonstrations of single-valued aggregation, through providing necessary details to enable aggregation to a vector, or by extension, to any arbitrary form. The General Problem Many tasks require aggregation to a vector resu...
4167 sym R (2225 sym/4 pcs) 2 img
Documenting Rcpp functions and classes in R packages
Introduction Roxygen2 is a convenient way to document functions in R packages. Based on the Doxygen model, it parses relevant information from the comments and generates the corresponding man/*.Rd files. The major strength of this model – besides not having to write *.Rd files by hand – is that documentation ends up living right next to the f...
5611 sym R (1810 sym/10 pcs) 2 img
Creating a data.table from C++
Introduction Rcpp provides the DataFrame class which enables us to pass data.frame object between C++ and R. DataFrame objects are key to R and used very widely. They also provide the basis from which two key packages extend them. One of these, the tibble package, operates in a similar fashion to data.frame and treats the data as immutable. Upon...
3827 sym R (3935 sym/14 pcs) 2 img
Nullable Optional Arguments in Rcpp functions
Introduction Often we need to have optional arguments in R of Rcpp functions with default values. Sometimes, the default value for the optional parameters is set to be NULL. Rcpp provides the Nullable <> to set default value as to be R_NilValue (equivalent of NULL in Rcpp). There have been several StackOverflow posts on using the Nullable behavio...
2102 sym R (3907 sym/22 pcs) 2 img
Recreating rmultinom and rpois with Rcpp
Sometimes one needs to mimic the exact behavior of R’s Distributions within C++ code. The incredible Rcpp team has provided access to these distributions through Rmath.h (in the R:: namespace), as well as through the Rcpp:: namespace where there can be two forms: scalar as in R, and vectorized via Rcpp sugar. The behavior of these functions may...
3586 sym R (2298 sym/10 pcs) 2 img
Mixing Rcpp modules and Rcpp attributes
Introduction With Rcpp attributes Rcpp modules (described in the Rcpp vignettes) it is easy to expose C++ classes and functions to R. This note describes how to use classes exported by modules in conjunction with functions exported using Rcpp attributes through the use of RCPP_EXPOSED* macros. In the following snippets, a simple example is given ...
2344 sym R (630 sym/10 pcs) 2 img
Handling R6 objects in C++
Introduction When we are using R6 objects and want to introduce some C++ code in our project, we may also want to interact with these objects using Rcpp. With this objective in mind, the key to interacting with R6 objects is that they are essentially environments, and so they can be treated as such. In this example, we will define a ‘Person’...
2866 sym R (3728 sym/11 pcs) 2 img
Using Scheme from R via Guile and Rcpp
Introduction GNU Guile is a programming language “designed to help programmers create flexible applications that can be extended by users or other programmers with plug-ins, modules, or scripts”. It is an extension language platform, and contains an efficient compiler and virtual machine allowing both out of the box use for programs in Scheme...
2659 sym R (1623 sym/5 pcs) 2 img
Using iconv via the R exported header
Introduction Character encodings can be tricky and frustrating to deal with. Several newer languages such as Go or Julia default to native UTF-8 on all platforms, greatly facilitating and easing use of characters with languages other than English on all common platforms. With R we are not quite as lucky. UTF-8 is mostly working as desired on �...
2108 sym R (1600 sym/2 pcs) 2 img
Constructing a Sparse Matrix Class in Rcpp
Introduction It is no secret that sparse matrix operations are faster in C++ than in R. RcppArmadillo and RcppEigen do a great job copying sparse matrices from R to C++ and back again. But note the word “copy”. Every time RcppArmadillo converts an R sparse matrix to an arma::SpMat<T> object, it has to creates a deep copy due to the differe...
5387 sym R (3675 sym/24 pcs) 2 img