[Rcpp-devel] Data Persistence with Rcpp

Douglas Bates bates at stat.wisc.edu
Thu Sep 8 14:46:10 CEST 2011


On Thu, Sep 8, 2011 at 7:08 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
>
> 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.

I use a similar implementation in the lme4Eigen package available on
the lme4 project on R-forge.  It is based on reference classes on the
R side and external pointers to hold the C++ object(s) for use in
active binding functions or methods for the object from the reference
class.  This is similar to the implementation of Rcpp modules but I
have trouble understanding the implementation of modules (the code is
quite complex) and could not see how to extend them.  For one thing if
you save and reload an instance of an Rcpp module class it is DOA when
you reload it.  All the external pointers in the object and the class
become null pointers.  In this implementation I just need to check on
whether one pointer is a null pointer and, in that case, re-initialize
the C++ object based on other information that I store in the class as
R objects.


> | 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...
>
> Dirk
>
> --
> 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
> http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10
> http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>


More information about the Rcpp-devel mailing list