dynamic lists at C level
Hi Dirk, Thanks for your reply, at some point I did consider the possibility of using Rcpp, but at that time I had already wrote a considerable amount of regular C code. Now I am only trying to extend that code a little, translating everything in Rcpp would probably consume more time (although I agree it might be easier to use). I read the "Writing R Extensions" manual, I could only find how to handle with R lists in C (section 5.9.6 Handling lists), but I could not find how to <create> a list in C, and access its components, and this is basically where I got stuck. Best, Adrian
On Tue, Nov 26, 2013 at 6:43 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
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
Adrian Dusa
Romanian Social Data Archive
1, Schitu Magureanu Bd.
050025 Bucharest sector 5
Romania
Tel.:+40 21 3126618 \
+40 21 3120210 / int.101
Fax: +40 21 3158391