[Rcpp-devel] type information about elements in Rcpp::List

Dirk Eddelbuettel edd at debian.org
Thu May 10 02:08:54 CEST 2012

On 9 May 2012 at 18:19, Steve Lianoglou wrote:
| Hi,
| On Wed, May 9, 2012 at 5:34 PM, Jiqiang Guo <guojq28 at gmail.com> wrote:
| > Dirk, Thanks.
| >
| > As Dirk previously points out, every element of Rcpp::List is of type SEXP.
| >  So I looked at
| > some R's doc, we should be able to get information (attributes in R's term:
| > int or real, names, dims)
| > from the SEXP at least using R's C functions such as examples at
| > http://cran.r-project.org/doc/manuals/R-exts.html#Attributes
| > and the functions used in Dirk's example.
| Assuming I'm following along here:
| As someone who trusts my R skills much more than my C(++) ones, I feel
| like I'd jimmy this scenario into one which I'd do the "heavy lifting"
| of type-checking the elements of a list and dispatching them to
| appropriate C++ code given their class in R, then collate the results
| back in R.
| (assuming the looping in R won't kill you, speedwise).
| Know what I mean?
| For instance, I'd have some R function that takes the list and directs traffic:
| theFunction <- function(x) {
|   stopifnot(inherits(x, "list"))
|   result <- sapply(x, function(xx) {
|     cpp.fun.name <- paste("theFunction", class(xx), sep="_")
|     ## Check cpp.fun.name to make sure it's a valid function
|     ## you defined in your C++ code, else raise an error about
|     ## unsupported class
|     .Call(cpp.fun.name, xx, PACKAGE="yourpackage")
| And you'd have suitable functions defined in your C++ side for the
| different types of things you want to support, ie:
| RcppExport SEXP theFunction_integer(SEXP xx);
| RcppExport SEXP theFunction_numeric(SEXP xx);
| RcppExport SEXP theFunction_character(SEXP xx);
| and so on ...

Good thinking.  It's just that sometimes we may get such lists from other C++
code rather than from R, so we'd also like to emulate the R behaviour.

And with the various Rf_isXXX() functions (for different values of XXX) we
should be able to get there...


R/Finance 2012 Conference on May 11 and 12, 2012 at UIC in Chicago, IL
See agenda, registration details and more at http://www.RinFinance.com

More information about the Rcpp-devel mailing list