Skip to content
Prev 200075 / 398506 Next

how to pass matrices from C to R effectively

On 12/11/2009 6:45 PM, Hao Cen wrote:
That .C call doesn't match the header of the function below:  you didn't 
pass row and col.
Why would you have to copy the data?  Just tell your C function to use 
the memory that R sent to it, instead of copying entries into it.  (R 
may be doing a copy when you extract the results, though.)

If your C code can't use memory provided to it, then there are no simple 
ways to avoid copying.  The complicated way is to use the .Call 
interface instead of .C.  There are examples in Writing R Extensions. 
They *will* require modifications to your code if it can't take a 
pointer to the memory to use.

For the question of the dimensions being unknown in advance:  Just write 
two functions.  The first does the calculations up to the point where it 
needs the memory, the second works on an appropriately sized array, that 
your R code allocated based on the first result.  Or use the .Call() 
interface.

Duncan Murdoch