[Rcpp-devel] Data Persistence with Rcpp

Dirk Eddelbuettel edd at debian.org
Thu Sep 8 14:08:18 CEST 2011

On 8 September 2011 at 03:36, Christian Gunning wrote:
| On Wed, Sep 7, 2011 at 3:13 PM,
| <rcpp-devel-request at r-forge.wu-wien.ac.at> wrote:
| >  The question absolutely pertains to using the inline functionality.
| I think this is a reasonable R/C++ question -- i.e. Rcpp.  inline is
| more or less orthogonal to Rcpp.  inline is a great Rcpp jump-starter.

I fully agree: inline is 100% orthogonal to Rcpp. It "merely" helps Rcpp a
lot for prototyping.

But just because inline is easy to use for small projects / tests does not
mean we should shy away from properly building packages when we want them.
More on that below.
| > As each new data arrives, I want to pass it to my C++ function.
| After thinking about this for a bit, this might be a reasonable use
| case for an Rcpp-module.  You can instantiate a module as an R object,
| it will hold state, and can be easily updated, queried, etc.  The key
| weakness here is that modules can't at present be serialized via
| save()/load().  So, if you can do all your processing in one R
| session, you might want to take a look at this.  The code ends up
| clean and powerful.  I've been headed this way myself for
| computationally extensive simulations that depend on time-varying
| parameters residing in R dataframes.

You can use modules, but you do not require modules to do it.

I have done similar things simply setting up a class, attached to a global
variable or maybe a singleton instance.  In the simplest case, have

 -- a private variable holding the data
 -- a setter adding data being collected 
 -- a getter retrieving data or summaries
 -- a constructor initialising the state variable

So then you need wrapper functions for the getter, setter and an init
function.  C'est tout.  All this can be tied to a global var holding a
pointer to the object initiated on a first call.  The database interface
package in R work in a similar fashion, and they don't eve use C++.

Now, in a package we even have a well defined entry point where we can
initialize things.  Inline can do not such thing.

But again, this list is not meant to educate about code design and all this
is (while interesting) actually somewhat off-topic in my view.
| Incidentally, an important question to ask yourself is:  where are
| your bottlenecks?  It wasn't clear to me in reading your question
| *why* you need C++.  The most popular use-case of Rcpp seems to be the
| huge speed advantage.  Where/why do you need speed?  This should
| highlight the code that needs to get pushed into C++.  R can compete
| with compiled code in plenty of cases...

Sharing other mailing lists with Noah, I have an idea...


Two new Rcpp master classes for R and C++ integration scheduled for 
New York (Sep 24) and San Francisco (Oct 8), more details are at

More information about the Rcpp-devel mailing list