Rsession provides an easy to use java class giving access to remote or local R sessions. The back-end engine should be:
- "true" R (3.5, 3.6 and 4.x), through Rserve (locally spawned automatically if necessary, fully compatible with legacy R),
- Renjin 3.5 (lower compatibility, but still very good),
- and R2js, which is on-the-fly R translation to math.js, with lower compatibility and hack-style coding, but full BSD licence.
Rsession differs from R2js, Rserve or Renjin as it is a higher level API, and it includes server side startup of Rserve. It is also easier to use as it provides a multi session R engine for all these wrappers, whatever OS.
JRI is another alternative, but it does not provide multi-sessions feature.
import static org.math.R.*;
...
public static void main(String args[]) {
//Rsession r = new RserveSession(System.out, null, null);
//Rsession r = new R2jsSession(System.out, null);
Rsession r = new RenjinSession(System.out, null);
double[] rand = (double[]) r.eval("rnorm(10)"); //create java variable from R command
//...
r.set("c", Math.random()); //create R variable from java one
r.save(new File("save.Rdata"), "c"); //save variables in .Rdata
r.rm("c"); //delete variable in R environment
r.load(new File("save.Rdata")); //load R variable from .Rdata
//...
r.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) (r.eval("df$x1[3]")); //access one value in data frame
//...
r.toJPEG(new File("plot.jpg"), 400, 400, "plot(rnorm(10))"); //create jpeg file from R graphical command (like plot)
String html = r.asHTML("summary(rnorm(100))"); //format in html using R2HTML
System.out.println(html);
String txt = r.asString("summary(rnorm(100))"); //format in text
System.out.println(txt);
//...
System.out.println(r.installPackage("sensitivity", true)); //install and load R package
System.out.println(r.installPackage("DiceKriging", true));
r.end();
}
Use this maven dependency:
<dependencies>
...
<dependency>
<groupId>com.github.yannrichet</groupId>
<artifactId>Rsession</artifactId>
<version>3.1.7</version>
</dependency>
...
</dependencies>
No 2nd order dependency required. Only based on Nashorn engine bundled in Java >8, so just add rsession.jar
in your classpath:
Then instanciate R session using:
Rsession r = new R2jsSession(System.out,null);
Add rsession.jar:renjin-jar-with-dependencies.jar
in your classpath:
- https://github.com/yannrichet/rsession/blob/master/dist/rsession.jar
- https://github.com/yannrichet/rsession/blob/master/lib/renjin-jar-with-dependencies.jar
Then instanciate R session using:
Rsession r = new RenjinSession(System.out,null);
...
Install R 3.5+ or 4.x from http://cran.r-project.org, then add rsession.jar:Rserve*.jar:REngine*.jar
in your project classpath:
- https://github.com/yannrichet/rsession/blob/master/dist/rsession.jar
- https://github.com/yannrichet/rsession/blob/master/lib/REngine-2.1.0.jar
- https://github.com/yannrichet/rsession/blob/master/lib/Rserve-1.8.1.jar
Then:
- start Rserve on localhost
/usr/bin/R CMD /usr/lib/R/library/Rserve/libs/Rserve --vanilla --RS-enable-control --RS-port 6311
, and instanciate R session using:Rsession r = new RserveSession(System.out,null,RserverConf.parse("R://localhost:6311"));
- or use the auto-spawned Rserve (may fail for exotic configuration):
Rsession r = new RserveSession(System.out,null,null);
- connect to remote Rserve (eg. previously started on 192.168.1.1 with
/usr/bin/R CMD /usr/lib/R/library/Rserve/libs/Rserve --vanilla --RS-enable-control --RS-port 6311
:Rsession r = new RserveSession(System.out,null,RserverConf.parse("R://192.168.1.1:6311"));