Using R in your Java program
I just ran into two nice packages rJava and JRI that allow running R code from Java and Java code from R. The packages are developed by Department of Computer Oriented Statistics and Data Analysis at Universität Augsburg.
The code below is an excerpt of a simple example that computes the fitness of a population using R. The fitness function is OneMax. For more examples check the demos in the JRI distribution.
Rengine re=new Rengine(args, false, new TextConsole());
if (!re.waitForR()) {
throw new Exception(“Could not load R!”);
}
…
try {
REXP x;
// The population
boolean [][] baaPop = new boolean[4][2];
baaPop[0][0] = false; baaPop[0][1] = false;
baaPop[1][0] = false; baaPop[1][1] = true;
baaPop[2][0] = true; baaPop[2][1] = false;
baaPop[3][0] = true; baaPop[3][1] = true;
// Loading the population into R
String sIndStr = “IND_”;
StringBuffer sb = new StringBuffer();
re.assign(sIndStr+0,baaPop[0]);
sb.append(sIndStr+0);
for ( int i=1, iMax=baaPop.length ; i
re.assign(sIndStr+i,baaPop[i]);
sb.append(”,”+sIndStr+i);
}
re.eval(”pop
// Computing OneMax
x=re.eval(”fit
// Printing the fitness values
for ( int i:x.asIntArray() )
System.out.println(i);
}
catch ( Exception e ) {…
}
Posted by admin on October 25th, 2006 under Illigal-blogging
Comments: none
Write a comment