[Rcpp-devel] Persistent C++ objects within R/Rcpp?
bates at stat.wisc.edu
Mon Jun 25 22:44:42 CEST 2012
On Mon, Jun 25, 2012 at 3:38 PM, Andy Garcia <andyg at ufl.edu> 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?
It can, in a way. Look up the Rcpp::ExternalPtr class. You pass back
a pointer to the C++ object then pass it back down in calls to other
C++ code. The thing you must be aware of is that
serialization/unserialization (i.e. save/load) on the R object will,
naturally, not be able to restore your C++ object so the external
pointer comes back as a NULL pointer.
> Could I just create a global object within C++?
> Any feedback would be appreciated,
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
More information about the Rcpp-devel