Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write an Rdata file from C++

Tags:

c++

r

Suppose I have a C++ program that has a vector of objects that I want to write out to an Rdata data.frame file, one observation per element of the vector. How can I do that? Here is an example. Suppose I have

vector<Student> myStudents;

And Student is a class which has two data members, name which is of type std::string and grade which is of type int.

Is my only option to write a csv file?

Note that Rdata is a binary format so I guess I would need to use a library.

A search for Rdata [r] [C++] came up empty.

like image 305
Xu Wang Avatar asked Jun 08 '12 18:06

Xu Wang


1 Answers

I think nobody has bothered to extract a binary file writer from the R sources to be used independently from R.

Almost twenty years ago I did the same for Octave files as their format is simply: two integers for 'n' and 'k', followed by 'n * k' of data -- so you could read / write with two function calls each.

I fear that for R you would have to cover too many of R's headers -- so the easiest (?) route may be to give the data to R, maybe via Rserve ('loose' connection over tcp/ip) and RInside (tighter connection via embedding), and have R write it.

Edit: In the years since the original answer was written, one such library has been created: librdata.

like image 141
Dirk Eddelbuettel Avatar answered Nov 14 '22 22:11

Dirk Eddelbuettel