Skip to content

export C++ array to R

2 messages · whizvast, Simon Urbanek

#
Hi, I am a newbie on C++

Right now I have an array of doubles in C++.

Is there a way to "export" that array into R? Of course, I can allocate 
the memory block first using "allocVector" and copying the array contents 
one by one.

But, what if that array is fairly large? Copying doesn't look that
efficient.
I was thinking of setting the data pointer(DATAPTR) point to that array,
and adjust the LENGTH of SEXP. But I don't know how to do that.

Any comment/answer would be much appreciated. Thank you.
1 day later
#
On Apr 18, 2009, at 4:12 AM, whizvast wrote:

            
The short answer is no, you can't, because the memory has to be  
allocated by R. If you are allocating the memory yourself, you can  
simply use allocVector instead of malloc/new to allocate the array in  
the first place - that saves you the copying and is the more usual  
approach for such R packages.

Cheers,
Simon