Dirk, Thanks.<div><br></div><div>As Dirk previously points out, every element of Rcpp::List is of type SEXP. So I looked at </div><div>some R's doc, we should be able to get information (attributes in R's term: int or real, names, dims) </div>
<div>from the SEXP at least using R's C functions such as examples at </div><div><a href="http://cran.r-project.org/doc/manuals/R-exts.html#Attributes">http://cran.r-project.org/doc/manuals/R-exts.html#Attributes</a> </div>
<div>and the functions used in Dirk's example. </div><div><br></div><div>Jiqiang </div><div><div><br><div class="gmail_quote">On Wed, May 9, 2012 at 4:55 PM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><br>
On 9 May 2012 at 16:38, Bob Carpenter wrote:<br>
| Thanks again, Dirk, for being so responsive to<br>
| our newbie queries. There's some more inline below.<br>
|<br>
| On 5/8/12 11:34 PM, Dirk Eddelbuettel wrote:<br>
| ><br>
| > On 8 May 2012 at 23:17, Jiqiang Guo wrote:<br>
| > | Suppose I have a function in CPP as<br>
| > |<br>
| > | void cppfun(Rcpp::List lst) {<br>
| > | ......<br>
| > | }<br>
| > |<br>
| > | Then I would like to call this cppfun in R code as say<br>
| > | cppfun(list(a=a, b=b, c=c, ...)), in which<br>
| ><br>
| > (Well you need a SEXP somefun(SEXP ...) interface from R)<br>
|<br>
| We (I'm working with Jiqiang) were hoping to<br>
| let Rcpp do the heavy wrapping here following<br>
| the std::vector example in the Rcpp modules doc (last<br>
| full example).<br>
|<br>
| > | a, b, c could be of different types and their type might be<br>
| different as this<br>
| > | function gets called another time. So I would like to know the<br>
| type of a, b,<br>
| > | c in the CPP code. Could someone help me out or point to me some other<br>
| > | approaches? Thanks.<br>
| ><br>
| > It's a good question. I have at time thought about that a little, but<br>
| have no<br>
| > immediate solution for you.<br>
| ><br>
| > One could possible use something C++-ish via traits.<br>
|<br>
| We are using traits/policies all over our own code,<br>
| but this isn't a compile-time type/behavioral config issue. The<br>
| types someone passes us in a call that takes a list<br>
| could be anything, and we need to check they're compatible<br>
| with what we're expecting and handle the error if they're<br>
| not.<br>
|<br>
| > One could also use C level macros from the R API which simply test<br>
| for types<br>
| > as after all each element of a List must be a SEXP, so we use the<br>
| SEXP-style<br>
| > macros.<br>
|<br>
| This could work. We just don't know what's in an<br>
| SEXP or how to get the SEXP from the Rcpp::List entries.<br>
<br>
</div></div>See below for a stylized (working) example.<br>
<div><br>
| What we (I'm working with Jiqiang) need is to be able to<br>
|<br>
| 1. access the value of an Rcpp list entry by name.<br>
<br>
</div>Name or position work, yes.<br>
<br>
But if you know the name, don't you know the type too?<br>
<div><br>
| 2. recover the basic type, integer or floating point<br>
<br>
</div>Integer always casts up to float so you get just use NumericVector if ...<br>
<br>
| 3. recover the dimensions<br>
<br>
... all you want is the length.<br>
<div><br>
| 4. recover the values as a vector<br>
<br>
</div>My stylized example does that, allbeit for matrices.<br>
<div><br>
| Presumably that's available somewhere in the Rcpp::List<br>
| if you can use it to communicate back and forth<br>
| losslessly with R.<br>
<br>
</div>Sort of. Rcpp::List allows for mixed types, just as in R. In C/C++, these are<br>
SEXP. And SEXP can be converted via Rcpp::as<> or implicitly because that is<br>
what Rcpp does anyway.<br>
<div><br>
| > In the end I always went with more explicit code design: only use a<br>
| List for<br>
| > transfer to / from R, and otherwise use explicit C++ types.<br>
|<br>
| I completely agree with Dirk's last point -- we only<br>
| want to use Rcpp as transport to/from R.<br>
|<br>
| Is there a place to find the API doc somewhere,<br>
| like what methods are available on an Rcpp::List?<br>
<br>
</div>There are our writeups, ie the vignettes.<br>
<br>
But there is so much code that it is hard to document all. There is the<br>
doxygen generated documentation too.<br>
<div><br>
<br>
| Otherwise, I can just look at the code. I'm about to<br>
| try to look through the source now, so maybe I'll be<br>
| able to answer our own question.<br>
|<br>
| - Bob<br>
<br>
</div>Here is a simple example:<br>
<br>
<br>
R><br>
R> suppressMessages(library(inline))<br>
R><br>
R> src <- '<br>
+ Rcpp::List lst(lstSexp);<br>
+<br>
+ int n = lst.length();<br>
+ Rcpp::IntegerMatrix dims(n, 2);<br>
+<br>
+ for (int i=0; i<n; i++) {<br>
+ SEXP s = lst[i]; // could also go by name<br>
+ if (Rf_isInteger(s)) { // isInteger() from R API<br>
+ Rcpp::IntegerMatrix m(s);<br>
+ dims(i,0) = m.nrow();<br>
+ dims(i,1) = m.ncol();<br>
+ } else if (Rf_isNumeric(s)) { // idem<br>
+ Rcpp::NumericMatrix m(s);<br>
+ dims(i,0) = m.nrow();<br>
+ dims(i,1) = m.ncol();<br>
+ } else if (Rf_isString(s)) { // idem<br>
+ Rcpp::CharacterMatrix m(s);<br>
+ dims(i,0) = m.nrow();<br>
+ dims(i,1) = m.ncol();<br>
+ } else {<br>
+ dims(i,0) = R_NaInt;<br>
+ dims(i,1) = R_NaInt;<br>
+ }<br>
+ }<br>
+ return dims;<br>
+ '<br>
R><br>
R> fun <- cxxfunction(signature(lstSexp="list"), # types are checked, 'list' just for info<br>
+ body=src,<br>
+ plugin="Rcpp")<br>
R><br>
R> fun(list(a=matrix(1:9,3,3), b=matrix(1L:4L,2,2), c=NULL, d=new.env(), e=rnorm))<br>
[,1] [,2]<br>
[1,] 3 3<br>
[2,] 2 2<br>
[3,] NA NA<br>
[4,] NA NA<br>
[5,] NA NA<br>
R><br>
<br>
<br>
Notice how we pass two different matrices, a NULL, an environment and a<br>
function just for kicks.<br>
<br>
Hope this helps, Dirk<br>
<div><br>
|<br>
|<br>
|<br>
| _______________________________________________<br>
| Rcpp-devel mailing list<br>
| <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
| <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
<br>
</div><div>--<br>
R/Finance 2012 Conference on May 11 and 12, 2012 at UIC in Chicago, IL<br>
See agenda, registration details and more at <a href="http://www.RinFinance.com" target="_blank">http://www.RinFinance.com</a><br>
</div><div><div>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</div></div></blockquote></div><br></div>
</div>