Giter Site home page Giter Site logo

rsession's Introduction

Build Status

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).

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.

Example Java code

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:

or include maven dependency:

<dependencies>
...
    <dependency>
      <groupId>com.github.yannrichet</groupId>
      <artifactId>Rsession</artifactId>
      <version>1.8.3</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 = s.eval("...",HashMap<String,Object> 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

rsession's People

Contributors

yannrichet avatar kktuax avatar golgauth avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.