[Rcpp-devel] Persistent C++ objects within R/Rcpp?

Dirk Eddelbuettel edd at debian.org
Tue Jun 26 02:00:42 CEST 2012


Andy,

On 25 June 2012 at 16:38, Andy Garcia wrote:
| First, thank you to the Rcpp team for their work on Rcpp. It's an incredible
| resource to get C++ code executing from R.
| 
| I've worked through setting up the correct development environment (Ian
| Fellow's post on Eclipse + Rcpp was great with a few tweaks for a Linux
| install) as well some of the examples. The next step I'd like to take is
| develop some prototype code for implementing an individual-based simulation.
| 
| Let's say I have a function that creates the simulated population based on some
| number of input parameters:
| 
| RcppExport SEXP CreatePopulation( /* some amount of input parameters*/ );
| 
| I would then want other functions called in R via Rcpp to operate on the
| population or return specific results. The part I'm struggling with is how
| would the population state persist when created in CreatePopulation to be then
| access/modified in other functions. My initial though is to have some sort of
| population object, however I don't know how this would persist when the code
| returns from execution in C++. R can't store a C++ object to be passed back,
| correct? Could I just create a global object within C++?
| 
| Any feedback would be appreciated,

I am not sure whether you really mean "persist" when you say "persist".  

Some common meaning of "persist" implies "continues to exists / be accessible
after code has stopped running". In that sense "persist" often refers to
storing on disk or in a db for retrieval in another, later session. Doug's
answer to you aimed at this direction too.

But it seems you simply want a creator function in C++ which creates a
simulation object, which you then modify and alter in R, possibly give back
to C++ etc.  Is that correct?

In the case, Rcpp can help easily.  Any object you create (with Rcpp) and
return to R will live as long as your R session lives. You can pass it back
and forth between R and C++ til the cows come home --- Rcpp creates objects
in a manner that is compatible with R's object lifecycle.  This is difficult
to explain concisely but we try in the JSS paper aka the Rcpp-introduction
vignette.  

If I misread you, never mind and sorry for wasted bandwidth.

Cheers, Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com  


More information about the Rcpp-devel mailing list