Re: Reading/writing RData files

719 views
Skip to first unread message

Bertram, Alexander

unread,
Oct 2, 2012, 4:07:46 AM10/2/12
to renji...@googlegroups.com
Hi Kevin,

Yes, this is a use case we use Renjin for as well. You will still need to create an evaluation Context
for writing data, but you don't have to load the base packages, so it shouldn't be expensive. At some point
in the near future I'm going to untangle the data classes (org.renjin.sexp.*) from the evaluator so that you 
will have a lightweight library for doing this kind of data processing.

Here's a quick example of how this might work:

ListVector.NamedBuilder dataframe = new ListVector.NamedBuilder();
dataframe.add("x", new DoubleArrayVector(1,2,3,4,5,6));
dataframe.add("y", new DoubleArrayVector(6,5,4,3,2,1));
dataframe.setAttribute("class", new StringArrayVector("data.frame"));

PairList.Builder list = new PairList.Builder();
list.add("df", dataframe.build());

Context context = Context.newTopLevelContext();  // this is still necessary for the moment, but should not be expensive
FileOutputStream fos = new FileOutputStream("my.RData");
GzipOutputStream zos = new GzipOutputStream(fos);
RDataWriter writer = new RDataWriter(context, zos);
writer.save(list.build());
writer.close();

// run external R process:
// load("my.RData")
// x <- compute.something(df)
// save("x", file="results.RData")

FileInputStream in = new FileInputStream("results.RData");
GzipInputStream zin = new GzipInputStream(in);
RDataReader reader = new RDataReader(zin);
PairList results = reader.readFile();

SEXP x = results.getElementByTag("x");

~~~~

This code is off the top of my head so you may need to fix a few syntax errors!

Best,
Alex

On Tue, Oct 2, 2012 at 9:01 AM, Kevin <kevin....@gmail.com> wrote:
I just came across this very cool project while looking for Java/R interop.  I'm curious if it is possible to read and write .RData files without spinning up a whole R environment.  I'd like to take some simple java collections and write them to an .RData file for some offline processing by a native R process.  When the computation is complete, I'd like to read in the .RData file again.
 
I've managed a trivial test case of creating an RDataReader and calling .readFile() which is promising.  Before I go too far, is this a supported use case?  Would it be possible for future builds of renjin produce a minimal RData serialization library for use in java apps?

Thanks,
Kevin

kevin....@gmail.com

unread,
Oct 2, 2012, 12:09:29 PM10/2/12
to renji...@googlegroups.com
Excellent.  I'll give it a try and I look forward to the lightweight library.

Thanks,
Kevin

ryanco...@gmail.com

unread,
Jan 16, 2015, 6:20:10 AM1/16/15
to renji...@googlegroups.com
Hi Alexander Bertram,
I try your sample code, but RDataWriter  does not have "close()" method, and I use "renjin-studio-0.7.0-RC7-jar-with-dependencies.jar" to open my.RData, I get the "Error: Unexpected end of ZLIB input stream".
How to solve this Error?
Thank you!

ryanco...@gmail.com

unread,
Jan 17, 2015, 9:54:27 AM1/17/15
to renji...@googlegroups.com, ryanco...@gmail.com
I have solved this error.
I provide the correct code for writing a RData file base on Alexander Bertram's code:

ListVector.NamedBuilder dataframe = new ListVector.NamedBuilder();
dataframe.add("x", new DoubleArrayVector(1,2,3,4,5,6));
dataframe.add("y", new DoubleArrayVector(6,5,4,3,2,1));
dataframe.setAttribute("class", new StringArrayVector("data.frame"));

PairList.Builder list = new PairList.Builder();
list.add("df", dataframe.build());

Context context = Context.newTopLevelContext(); 
FileOutputStream fos = new FileOutputStream("my.RData");
GZIPOutputStream zos = new GZIPOutputStream(fos);
RDataWriter writer = new RDataWriter(context, zos);
writer.save(list.build());
zos.close();

Varshini Ravichandran

unread,
Jan 12, 2023, 5:28:41 AM1/12/23
to Renjin
i tried this but getting Exception for using  GzipInputStream.
Reply all
Reply to author
Forward
0 new messages