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

Bob Carpenter carp at alias-i.com
Wed May 9 22:38:05 CEST 2012

Thanks again, Dirk, for being so responsive to
our newbie queries.  There's some more inline below.

On 5/8/12 11:34 PM, Dirk Eddelbuettel wrote:
 > On 8 May 2012 at 23:17, Jiqiang Guo wrote:
 > | Suppose I have a function in CPP as
 > |
 > | void cppfun(Rcpp::List lst) {
 > |     ......
 > | }
 > |
 > | Then I would like to call this cppfun in R code as say
 > | cppfun(list(a=a, b=b, c=c, ...)), in which
 > (Well you need a SEXP somefun(SEXP ...) interface from R)

We (I'm working with Jiqiang) were hoping to
let Rcpp do the heavy wrapping here following
the std::vector example in the Rcpp modules doc (last
full example).

 > | a, b, c could be of different types and their type might be 
different as this
 > | function gets called another time.  So I would like to know the 
type of a, b,
 > | c in the CPP code.  Could someone help me out or point to me some other
 > | approaches?  Thanks.
 > It's a good question. I have at time thought about that a little, but 
have no
 > immediate solution for you.
 > One could possible use something C++-ish via traits.

We are using traits/policies all over our own code,
but this isn't a compile-time type/behavioral config issue.  The
types someone passes us in a call that takes a list
could be anything, and we need to check they're compatible
with what we're expecting and handle the error if they're

 > One could also use C level macros from the R API which simply test 
for types
 > as after all each element of a List must be a SEXP, so we use the 
 > macros.

This could work.  We just don't know what's in an
SEXP or how to get the SEXP from the Rcpp::List entries.

What we (I'm working with Jiqiang) need is to be able to

   1.  access the value of an Rcpp list entry by name.
   2.  recover the basic type, integer or floating point
   3.  recover the dimensions
   4.  recover the values as a vector

Presumably that's available somewhere in the Rcpp::List
if you can use it to communicate back and forth
losslessly with R.

 > In the end I always went with more explicit code design: only use a 
List for
 > transfer to / from R, and otherwise use explicit C++ types.

I completely agree with Dirk's last point -- we only
want to use Rcpp as transport to/from R.

Is there a place to find the API doc somewhere,
like what methods are available on an Rcpp::List?
Otherwise, I can just look at the code.  I'm about to
try to look through the source now, so maybe I'll be
able to answer our own question.

- Bob

More information about the Rcpp-devel mailing list