Home:ALL Converter>RVector/REXP to ArrayList

RVector/REXP to ArrayList

Ask Time:2014-09-16T13:39:30         Author:The_F

Json Formatter

I'm having some trouble with the JRI-Package. Basically I'm programming in Java and I access a SQL-Database through R with RJDBC. Anyway, my problem is more of a R/JRI-Problem than anything else, I just don't want you to be confused.

My Code:

RVector result_a = re.eval("dbGetQuery(conn, \"select movies_id from movies\")").asVector();
REXP result_b = re.eval("dbGetQuery(conn, \"select movies_id from movies\")");
System.out.println(result_a.firstElement());
List<Object> S = Arrays.asList(result_a.get(0));
for(int i = 0; i < S.size(); i++) {
    System.out.println("Nr. " + i + ": " + S.get(i));
}
System.out.println(result_b.getContent());

Output:

[REAL* (1.0, 2.0, 3.0, 4.0, 5.0)]
Nr. 0: [REAL* (1.0, 2.0, 3.0, 4.0, 5.0)]
[[REAL* (1.0, 2.0, 3.0, 4.0, 5.0)]]

The thing is, that for some reason the Objects REXP (and also RVector, though not shown) are covered in double brackets (when being printed). I suppose, it is an vector in a vector? And the actual problem: The data is compressed in one single place, I want it to be like this:

Nr. 0: 1.0
Nr. 1: 2.0
Nr. 2: 3.0
Nr. 3: 4.0
Nr. 4: 5.0

I tried out all the functions I could use, but nothing seems to work. It's always spit out as a single String or Object or whatever and I can't really pinpoint the problem.

I appreciate any hint. :)

Author:The_F,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/25861366/rvector-rexp-to-arraylist
yy