# rsession **Repository Path**: ninuxGithub/rsession ## Basic Information - **Project Name**: rsession - **Description**: No description available - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-05-20 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README [![Build Status](https://travis-ci.org/yannrichet/rsession.png)](https://travis-ci.org/yannrichet/rsession) # Rsession: R sessions wrapping for Java # 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). Other alternatives: * 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. * Renjin: a pure java R implementation, but you cannot use R libraries with C/C++/Fortran dependancies. ## Example Java code ## ```java 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(); } ``` ## Use it ## 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: ```xml ... com.github.yannrichet Rsession 1.8.3 ... ``` Then, use it in your code: * create new Rsession: * (easy) local spawning of Rserve (for Windows XP, Mac OS X, Linux 32 & 64): ```java 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): ```java 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 ```java Object o = s.eval("...",HashMap vars) ``` (Object o is automatically cast to double, double, double,String, String, ...) * OR create your R objects using `s.set("...",...)` call any R command using `s.evalR("...")` cast to Java objects using `Rsession.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(); ` ![Analytics](https://ga-beacon.appspot.com/UA-109580-20/rsession)