R Programming Through Java
R Programming Through Java
through Java
B.Bhuvaneswaran
Assistant Professor (SS)
Department of Computer Science & Engineering
Rajalakshmi Engineering College
Thandalam
Chennai 602 105
[email protected]
R Programming Through
Java
Rserve Configuration
The Rserve webpage gives details for how
one goes about installing and starting the
server.
Briefly, one must first download the Rserve
package in R, followed by starting the R
server by loading the library and calling
the Rserve() function, or running Rserve
as a daemon via the command line using
the 'R CMD Rserve' syntax.
import org.rosuda.REngine.REXP;
import org.rosuda.REngine.Rserve.*;
public class RTest1 {
public static void main(String[] args) throws Exception{
RConnection c = null;
try{
c = new RConnection();
REXP x = c.eval("R.version.string");
System.out.println(x.asString());
}catch(Exception e){
e.printStackTrace();
}finally{
if ( c != null ){
try{
c.close();
}finally{}
}
}
}
}
import org.rosuda.REngine.REXP;
import org.rosuda.REngine.Rserve.*;
/**
* Demonstration for accessing R via java
*
*/
public class RTest2 {
public static void main(String[] args) throws Exception{
double[] myvalues = {1.0, 1.5, 2.2, 0.5, 0.9, 1.12};
RConnection c = null;
try{
c = new RConnection();
c.assign("myvalues", myvalues);
REXP x = c.eval("mean(myvalues)");
System.out.println(x.asDouble());
x = c.eval("sd(myvalues)");
System.out.println(x.asDouble());
}catch(Exception e){
e.printStackTrace();
}finally{
if ( c != null ){
try{
c.close();
}finally{}
}
}
}
}
Multithreading
Disadvantages
Networking
Timing
Steps
install.packages("Rserve")
library(Rserve)
Rserve()
References
http://www.algosome.com/articles/running-r-code-in-java.html