I would like to implement some R package, written in C code.
The C code must:
- take an array (of any type) as input.
- produce array as output (of unpredictable size).
What is the best practice of implementing array passing?
At the moment C code is called with .C()
. It accesses array directly from R, through pointer. Unfortunately same can't be done for output, as output dimensions need to be known in advance which is not true in my case.
Would it make sense to pass array from C to R through a file? For example, in ramfs, if using linux?
Update 1: Here the exact same problem was discussed. There, possible option of returning array with unknown dimensions was mentioned:
Splitting external function into two, at the point before calculating the array but after dimensions are known. First part would return dimensions, then empty array would be prepared, then second part would run and populate the array in R.
In my case full dimensions are known only once whole code is executed, so this method would mean running C code twice. Taking a guess on maximal array size isn't optional either.
Update 2: It seems only way to do this is to use .Call()
instead, as power suggested. Here are few good examples: http://www.sfu.ca/~sblay/R-C-interface.ppt.
Thanks.
.C()
and.Call()
functions in R base. Numerous helpful questions and answers can be found on StackOverflow, as well as Eddelbuettel and Francois's sites linked from the Rcpp package page. – jthetzel