Reputation: 13
I have developed java code using JRI for executing the R code on Java. Now, I have come across a problem (picking up distinct values in the column of a data frame) where I have to use sqldf package of R and am trying to use its function in my Java code. Here is the sample code (Orange is the dataset already present in R by default):
public class RCode2 {
public static JRIEngine re;
private static org.rosuda.REngine.REXP rexp;
public RCode2()
{
try {
re = new JRIEngine(new String [] {"--vanilla"});
} catch (REngineException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Rengine created, waiting for R");
}
public static void main(String[] args) {
RCode2 rCode2= new RCode2();
rCode2.testSqldf();
re.close();
}
public void testSqldf(){
rexp=re.parseAndEval("library(sqldf,lib.loc=\"C:/Program Files/R/R-"+"2.15.0/library\")\n");
System.out.println(rexp.isNull());
rexp=re.parseAndEval("dframe<-sqldf(\"select * from Orange\")");
System.out.println(rexp.isNull());
}
}
The output of the above code is:
Rengine created, waiting for R
true
true
This means that "dframe" contains nothing. But when I try to run the same sqldf func: sqldf("select * from Orange") on R then it gives me 35 rows. How do I make use of sqldf functions in java so as to get desired results?
My environment variables are set as follows:
R_HOME=C:\Program Files\R\R-2.15.0
R_LIBS=C:\Program Files\R\R-2.15.0\library
R_LIBS_USER=C:\Program Files\R\R-2.15.0\library
Path=C:\Program Files\R\R-2.15.0\bin\i386;C:\Program Files\R\R-2.15.0\library\rJava\jri
Upvotes: 1
Views: 829
Reputation: 13932
The code you posted doesn't even compile. After cleaning it up it works just fine:
C:\Program Files\R\R-2.15\library\rJava\jri>run RCode2
Rengine created, waiting for R
org.rosuda.REngine.REXPString@10d448[14]
org.rosuda.REngine.REXPGenericVector@1bf216a+[3]named
If you have issues with your code you should make sure you provide I/O from R console so you can actually see errors caused by your code (see TextCondole class in the examples).
Finally, stats-rosuda-devel mailing list is the place to ask about rJava/JRI.
The code:
import org.rosuda.REngine.JRI.*;
import org.rosuda.REngine.*;
public class RCode2 {
public static JRIEngine re;
private static org.rosuda.REngine.REXP rexp;
public RCode2()
{
try {
re = new JRIEngine(new String [] {"--vanilla"});
} catch (REngineException e) {
e.printStackTrace();
}
System.out.println("Rengine created, waiting for R");
}
public static void main(String[] args) {
RCode2 rCode2= new RCode2();
rCode2.testSqldf();
re.close();
}
public void testSqldf(){
try {
rexp = re.parseAndEval("library(sqldf)");
System.out.println(rexp);
rexp = re.parseAndEval("dframe<-sqldf(\"select * from Orange\")");
System.out.println(rexp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Upvotes: 1