Skip to content

[Rcpp-devel] Rcpp/C++ serialization of R objects?

4 messages · rsparapa at mcw.edu, Romain Francois, Dirk Eddelbuettel

#
Hi Gang:

I have been through the docs, and if this is answered, then I missed
it.  Is there a recommended way to serialize an R object with Rcpp?

For example, suppose I have created a List in C++ and I want to store
it with the R equivalent of saveRDS() <because I want to read it in at
some point later on from within R via readRDS()>.

More details if needed...  I am doing some long-running MCMC
calculations.  And, I would like the C++ code to store the
results every, say, 10000 iterations.  Of course, I could do
this in a loop from R.  But, I want to be sure that is really
necessary before I re-write my R code ;o)
#
Le 29/10/2013 16:24, Rodney Sparapani a ?crit :
Not at the moment. It is pretty easy though to make an R call to saveRDS 
using Function ...

Environment base = Environment::base_namespace() ;
Function saveRDS[ "saveRDS" ] ;

List precious = ... // whatever you want to saveRDS
saveRDS( precious, "somefile") ;

  
    
#
Hi Rodney,
On 29 October 2013 at 10:24, Rodney Sparapani wrote:
| Hi Gang:
| 
| I have been through the docs, and if this is answered, then I missed
| it.  Is there a recommended way to serialize an R object with Rcpp?

It's a very good question. 

In the context of my small rhiredis package (a fork / cleanup / demo started
from Wush's initial attempt; both on Github) I poked around the R source but
did not find a good way to access its functionality.  In this case, where we
want to serialize and then push to the (NoSQL) db redis, it turns out that
relying on R's serialize / deserialize is good enough.  You can look there; I
have some simple timing demos there.

| For example, suppose I have created a List in C++ and I want to store
| it with the R equivalent of saveRDS() <because I want to read it in at
| some point later on from within R via readRDS()>.

Yes, I think I would like to have that functionality to some extent.

Now, _for basic C++ types_serialization is of course understood and
implemented so you could just the appropriate, say, Boost library.

For _R_ types I think we will be hard-pressed to do better than R, and at
pain to do it without R. The nestedness of types, attributes, lazy
evaluation, ... all come in.

So your best bet here ... may be to bite the bullet and use an
Rcpp::Function() to just call R on it.
 
Until we convince the Powers That Be to give us an API from C (and hence C++).

| More details if needed...  I am doing some long-running MCMC
| calculations.  And, I would like the C++ code to store the
| results every, say, 10000 iterations.  Of course, I could do
| this in a loop from R.  But, I want to be sure that is really
| necessary before I re-write my R code ;o)

You could call back from C++ to R. If your simulations take 12 hours, you you
waste a few (sub-)seconds to transfer snapshots to R, serialize them and then
dump to disk it shouldn't really matter.  And it would give you a) a security
blanket to fall back on if later parts of the simulation go belly-up and b) a
way to analyse results while the sim is still going.  

I like it.  I am sure other people have ideas too.  

Dirk
2 days later
#
On 29 October 2013 at 10:38, Dirk Eddelbuettel wrote:
| Yes, I think I would like to have that functionality to some extent.
| 
| Now, _for basic C++ types_serialization is of course understood and
| implemented so you could just the appropriate, say, Boost library.
| 
| For _R_ types I think we will be hard-pressed to do better than R, and at
| pain to do it without R. The nestedness of types, attributes, lazy
| evaluation, ... all come in.

Simon U., in another off-list exchange, reminded me that Rinternals.h has

  void R_Serialize(SEXP s, R_outpstream_t stream);
  SEXP R_Unserialize(R_inpstream_t stream);

using

  R_Init*PStream, R_InitFile*PStream, R_InitConn*PStream

That might be worth a shot if you want to serialize at the C++ level.

Dirk