[Rcpp-devel] Rcpp & static variables

Romain Francois romain at r-enthusiasts.com
Thu May 6 13:36:47 CEST 2010


Le 06/05/10 13:12, Giuseppe Milicia a écrit :
>
> Dirk,
>
> Unfortunately the pieces of code that use static variables are lost in the depth of my libraries.
>
> I used to mix R and Java using rJava, when you do that you can keep object references in R rather easily, that is not immediately possible with Rcpp. Somehow I didn't think about static variables (after all we just have a shared library loaded by the R process), so now I'm using static variables to make my system stateful, it seems to work rather nicely :)

You can use external pointers to do similar things. For example, if your 
"state" variable is an std::vector<int>, you can deal with it using 
external pointers.

fx <- cppfunction( signature(), '
// create a pointer
std::vector<int> *v = new std::vector<int>(5) ;

// wrap it as an R external pointer
Rcpp::XPtr< std::vector<int> > pv(v,true);

return pv ;
' )

now you can return this to the R side :

 > xp <- fx()
 > str( xp )
<externalptr>
 > xp
<pointer: 0x10056cd30>
 > typeof( xp )
[1] "externalptr


Then you can pass it back to c++ :

fy <- cppfunction( signature( xp_ = "externalptr" ), '
	// wrap it up again
	Rcpp::XPtr< std::vector<int> > xp(xp_);
	
	// now you can use xp as a pointer to std::vector<int>
	xp->push_back( 10 ) ;
	int size = xp->size() ;

	return wrap( size ) ;
' )


 > fy( xp )
[1] 6
 > fy( xp )
[1] 7
 > fy( xp )
[1] 8


This is what rJava uses. It is more work than with java because java has 
runtime reflection, which is not available in C++.

See also: 
http://romainfrancois.blog.free.fr/index.php?post/2010/01/08/External-pointers-with-Rcpp

> In a Rcpp document I read (can't find it anymore, perhaps an old vignette?) there was a paragraph about Rcpp being stateless

I'm not familiar with this, so it must be before I started to 
contribute. Maybe Dirk knows better. Maybe this was from before Dirk 
started to maintain.

In any case, please refer to the current documentation.

> perhaps the vignette could mention that you can use static variables to keep state around? It's obvious on hindsight, but somehow I didn't connect the dots when I started to using Rcpp...
 >
> Cheers,
>
> //Giuseppe
>
> ________________________________________
> From: Dirk Eddelbuettel [edd at debian.org]
> Sent: 30 April 2010 18:14
> To: Giuseppe Milicia
> Cc: rcpp-devel at lists.r-forge.r-project.org
> Subject: Re: [Rcpp-devel] Rcpp&  static variables
>
> Hi Guiseppe,
>
> Thanks for reposting here!
>
> On 30 April 2010 at 16:07, Giuseppe Milicia wrote:
> | Guys,
> |
> | I posted this on R-help and Dirk kindly pointed out that this list is a much better place for this sort of questions...
> |
> | I was wondering whether anyone experimented with Rcpp and static
> | variables. I remember reading that Rcpp is essentially stateless. That
> | makes sense. However I just run a piece of code that contains a static STL
> | structure, surprisingly  it seems that those static variables are
> | preserved...
>
> I have not used it explicitly.
>
> But I do use code in production that instantiates singletons etc to keep
> e.g. connection handles to a backend.  I initialize this on package load --
> using a nice clean hook provided by R and then hold on to it until the
> package unloads, which in practice is when the R session ends.  That works
> cleanly via constructor and deconstructor calls.
>
> | The question is, can we use static variable to reliably preserve state
> | between Rcpp calls? If this is the case, this should be a better way to
> | store state than by passing state variables between R and C++.
>
> I don't see why not. Can you mock up an example of what you are trying to do?
>
> --
>    Regards, Dirk


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/9aKDM9 : embed images in Rd documents
|- http://tr.im/OIXN : raster images and RImageJ
|- http://tr.im/OcQe : Rcpp 0.7.7



More information about the Rcpp-devel mailing list