[Rd] dynamic lists at C level

Dirk Eddelbuettel edd at debian.org
Tue Nov 26 17:43:13 CET 2013


Hi Adrian,

On 26 November 2013 at 18:12, Adrian Duşa wrote:
| Dear R-devel,
| 
| I am trying to do something similar to dynamic length lists in R, but
| at C level.
| 
| In R, that would be rather trivial:
| - determine the length of the list
| - create the list object
| - store the values for each component
| - access value components by using "[["
| 
| At C level, for a single component where I need to store a vector of
| length 5, I do:
| 
| int *p_result;
| SEXP my_list = PROTECT(allocVector(VECSXP, 1));
| SEXP result = SET_VECTOR_ELT(my_list, 0, allocVector(INTSXP, 5));
| p_result = INTEGER(result);
| 
| 
| The number "1" (the length of "my_list") is dynamic, however.
| Is there a web reference where I could do some further reading on this topic?

If you are open to C++, there is a fair amount of documentation for Rcpp
which some of us find easier.   

Turing-equivalence does of course hold, and all we do here can also be done
at the C level given that we use the same 'SEXP foo(SEXP a, SEXP b, ...)'
interface -- while hiding most of it.

Here is a simple case where we define a creator function on the fly, compile,
link and load it and then have it creates lists of length 2 and 4, respectively:

R> library(Rcpp)
R> cppFunction("List adrian(int n) { return List(n); } ")
R> adrian(2)
[[1]]
NULL

[[2]]
NULL

R> adrian(4)
[[1]]
NULL

[[2]]
NULL

[[3]]
NULL

[[4]]
NULL

R> 

Hope this helps,  Dirk


-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com



More information about the R-devel mailing list