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

Steve Lianoglou mailinglist.honeypot at gmail.com
Thu May 10 00:19:24 CEST 2012


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")
  }, USE.NAMES=TRUE)

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 ...

HTH,
-steve

-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact


More information about the Rcpp-devel mailing list