Rsession provides an easy to use java class giving access to remote or local R session. The back-end engine is Rserve, locally spawned automatically if necessary.
Rsession differs from Rserve as it is a higher level API, and it includes server side startup of Rserve. Therefore, it is easier to use in some point of vue, as it provides a multi session R engine (including for Windows, thanks to an ugly turn-around).
Another alternative is JRI, but it does not provide multi-sessions feature. If you just need one R session in your java code, JRI is a good solution.
import static org.math.R.Rsession.*;
...
public static void main(String args[]) {
Rsession s = Rsession.newInstanceTry(System.out);
double[] rand = (double[]) s.eval("rnorm(10)",null); //create java variable from R command
...
s.set("c", Math.random()); //create R variable from java one
s.save(new File("save.Rdata"), "c"); //save variables in .Rdata
s.rm("c"); //delete variable in R environment
s.load(new File("save.Rdata")); //load R variable from .Rdata
...
s.set("df", new double[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}, "x1", "x2", "x3"); //create data frame from given vectors
double value= (Double) (cast(s.eval("df$x1[3]"))); //access one value in data frame
...
s.toJPEG(new File("plot.jpg"), 400, 400, "plot(rnorm(10))"); //create jpeg file from R graphical command (like plot)
String html = s.asHTML("summary(rnorm(100))"); //format in html using R2HTML
System.out.println(html);
String txt = s.asString("summary(rnorm(100))"); //format in text
System.out.println(txt);
...
System.out.println(s.installPackage("sensitivity", true)); //install and load R package
System.out.println(s.installPackage("wavelets", true));
s.end();
}
First, install R from http://cran.r-project.org.
Add lib/Rsession*.jar:lib/Rserve*.jar:lib/REngine*.jar in your project classpath:
- copy https://github.com/yannrichet/rsession/blob/master/Rsession/dist/rsession.jar
- copy Rserve https://search.maven.org/remotecontent?filepath=org/rosuda/REngine/Rserve/1.8.1/Rserve-1.8.1.jar
- copy REngine https://search.maven.org/remotecontent?filepath=org/rosuda/REngine/REngine/2.1.0/REngine-2.1.0.jar
or include maven dependency:
<dependencies>
...
<dependency>
<groupId>com.github.yannrichet</groupId>
<artifactId>rsession</artifactId>
<version>1.8.2</version>
</dependency>
...
</dependencies>
Then, use it in your code:
- create new Rsession:
- (easy) local spawning of Rserve (for Windows XP, Mac OS X, Linux 32 & 64):
Rsession s = Rsession.newInstanceTry(System.out,null);
- OR connect to remote Rserve (previously started with /usr/bin/R CMD Rserve --vanilla --RS-conf Rserve.conf):
Rsession s = Rsession.newRemoteInstance(System.out,RserverConf.parse("R://192.168.1.1")); //connect to local Rserve (previously started with /usr/bin/R CMD Rserve --vanilla --RS-conf Rserve.conf): Rsession s = Rsession.newLocalInstance(System.out,null);
- do your work in R and get Java objects
- create Java objects from R command using
(Object o is automatically cast to double, double, double,String, String, ...)Object o = s.eval("...",HashMap<String,Object> vars)
- OR
create your R objects using
s.set("...",...)
call any R command usings.evalR("...")
cast to Java objects usingRsession.Rcast(...)
if needed use remote R packages install & load:s.installPackage("...", true);
you can access R command answers as string using:s.asHTML("...")
s.asString("...")
,s.toJPEG(File f,"...")
- finally close your Rserve instance:
s.end();