Dynamic Wrapping and Recursion with Rcpp

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 Hadley’s guide to R’s C internals guide here first, as we will be using some of these functions for navigating native R SEXPs. (Reading it will also give you an appreciation for just how much work Rcpp does in insulating us from the ugliness of the R API.)

From the R API, we’ll be using the TYPEOF macro, as well as referencing the internal R types:

REALSXP for numeric vectors,

INTSXP for integer vectors,

VECSXP for lists

We’ll start with a simple example: an Rcpp function that takes a list, loops through it, and: