Re: [R] Java parser for R data file?

From: <kchine_at_ebi.ac.uk>
Date: Thu, 6 Dec 2007 21:25:41 -0000 (GMT)


Dear David,

You may also consider using the biocep project' tools and frameworks. they provide an advanced bridge that allow you to exchange between R and Java any standard R Object and any mapped S4 object. the object extracted to Java (an RList for you data) can be serialized to a file (saved as a java object). you can then read the serialized object very easily from java and this is obviously much faster than parsing and more elegant than a "proprietary" serialization/deserialization .

the easiest way to achieve this is to create the Serialized RList via the Virtual R Workbench (Universal IDE for R). you can run the R workbench on any kind of OS via the biocep sources: http://www.ebi.ac.uk/microarray-srv/frontendapp/BIOCEP_README.txt

or just use this link to get the software installed on your machine if you are using Mac OS or windows
http://www.ebi.ac.uk/microarray-srv/frontendapp/rworkbench.jnlp

create your data via the workbench R Console (you may also use the script editor or just read your list from a file) go to the menu "Java", choose "Save R/Java Object to local file" select the name you've given to your list and choose a file name.

to retrieve your list in java, all you need is to add the RJB.jar to your class path and use this simple code:

RList l=(RList)new ObjectInputStream(new FileInputStream("J:/list.ser")).readObject();

and that's it.
this works for much more complex R objects (ExpressionSet..)

best wishes,

Karim

>
> On 12/5/07 12:15 PM, "Prof Brian Ripley" <ripley@stats.ox.ac.uk> wrote:
>
> On Wed, 5 Dec 2007, David Coppit wrote:
>
>> Or, given that I'm dealing with just a single array, would it be better
>> to
>> roll my own I/O using write.table or write.matrix from the MASS package?
>
> It would be much easier. The save() format is far more complex than you
> need. However, I would use writeBin() to write a binary file and read
> that in in Java, avoiding the binary -> ASCII -> binary conversion.
>
> Thanks for the suggestion-writeBin works quite well. For posterity, here's
> what I did:
>
> On the R side:
>
> # Assumes that there are no special values in the tofList, such as
> NA_REAL,
> # R_PosInf, R_NegInf, ISNAN, R_FINITE. See the "R Data Import/Export"
> manual.
> saveListAsBinary <- function( tofList, filename )
> {
> outConn <- file( filename, "wb" );
>
> for (m in 1:length(tofList)) {
> writeBin(names(tofList)[[m]], outConn);
> writeBin(length(tofList[[m]]), outConn, size = 4, endian = "big");
> writeBin(tofList[[m]], outConn, size = 4, endian = "big");
> }
>
> close(outConn);
> }
>
> saveListAsBinary(myList, "outfile.RDat");
>
> On the Java side:
>
> public static void read_R_Output(String filename, ArrayList<String>
> names,
> ArrayList<ArrayList<Float>> data)
> {
> try {
> DataInputStream dataInputStream = new DataInputStream(
> new BufferedInputStream(new FileInputStream(filename)));
>
> boolean endOfFile = false;
>
> while (!endOfFile) {
> try {
> StringBuffer sb = new StringBuffer();
>
> byte c;
> while ((c = dataInputStream.readByte()) != 0)
> sb.append((char)c);
>
> names.add(new String(sb));
>
> int cols = dataInputStream.readInt();
>
> ArrayList<Float> row = new ArrayList<Float>(cols);
>
> for (int i = 0; i < cols; i++)
> row.add(dataInputStream.readFloat());
>
> data.add(row);
> } catch (EOFException e) {
> endOfFile = true;
> }
> }
>
> dataInputStream.close();
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
>
> Regards,
> David
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 06 Dec 2007 - 21:30:39 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 06 Dec 2007 - 22:30:17 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.