[Rd] reinterpreting externalptr in R

romain at r-enthusiasts.com romain at r-enthusiasts.com
Wed Dec 26 22:04:02 CET 2012


For Rcpp, you can use the XPtr class template. Say you want to deal 
with pointers to Foo. You can create an R external pointer like this:

SEXP create_xp_foo(){
     Foo* foo = new Foo ;
     XPtr<Foo> xp( foo ) ;
     return xp ;

Now, "xp" is an R external pointer, i.e. a SEXP of type EXTPTRSXP, that 
you can send back to the R side. This constructor assumes ownership of 
the pointer, so when the SEXP is no longer protected, it becomes 
candidate for garbage collection, and then the finalizer deletes the 
object using C++ delete operator. The XPtr template lets you control 
these finalizers. We can discuss further if you want to know more.

If you then send it back to the C++ side, you can get back the Foo 
pointer like this:

SEXP use_foo( SEXP xp ){
     XPtr<Foo> foo(xp);
     // conversion operator
     Foo* p_foo = foo ;
     do_whatever( p_foo ) ;
     return R_NilValue ;

The XPtr template defines C++ operators so that the XPtr<Foo> object 
can be converted to a Foo*.

You can find details about the XPtr template in Rcpp documentation or 
perhaps just reading the source at r-forge:

This simply builds on the macros that the R api offers, so perhaps 
"writing R extensions" or "R internals" are good sources of information.


Le 2012-12-25 18:15, Simon Urbanek a écrit :
> On Dec 25, 2012, at 6:39 AM, andre__ wrote:
>> Hi,
>> I am using swig to build a wrapper for an c-function to use it in R. 
>> I would
>> like to define a generic function, which gives back a void pointer. 
>> On the R
>> side, I know what this pointer is pointing to, whether it is an 
>> integer or
>> an string, or something else... but I need to somehow reinterpret 
>> the
>> resulting "externalptr" into another form...
>> a function that looks like the following:
>> *void* test(){
>>    std::string str="test";
>>    return str.c_str();
>> }*
>> when I call this from R:
>> *str <- test()
>> typeof(str) % result is: "externalptr"*
>> how could I reinterpret this to a charcterarray, to a numeric, to 
>> list, or
>> ...
> The simple answer is you can't. External pointers are entirely opaque
> to R, so *you* have to write that code that interprets them. 
> Obviously
> it's then up to you to create the corresponding R object from the
> external pointer.
> You may want to have a look at interface packages like Rcpp, rJava,
> Rserve, ... to understand how objects are converted to/from other
> languages.
> Cheers,
> Simon
>> Thanks for any suggestions ;)

More information about the R-devel mailing list