Giter VIP home page Giter VIP logo

rsession's Introduction

Linux & MacOS: Build Status Windows: AppVeyor Build Status

codecov Maven Central

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 Renjin (NEW) or Rserve, locally spawned automatically if necessary. Rsession differs from Rserve or Renjin 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 view, 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 (or work with R environments), JRI is a good solution.

Example Java code

import static org.math.R.*;
...
 
    public static void main(String args[]) {
        Rsession r = RserveSession.newInstanceTry(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("wavelets", true));

        r.end();
    }

Use it

Using Renjin:

Add lib/rsession.jar:lib/renjin*.jar in your project classpath:

Using Rserve:

Install R from http://cran.r-project.org.

Add lib/rsession.jar:lib/Rserve*.jar:lib/REngine*.jar in your project classpath:

Using a maven dependency

<dependencies>
...
    <dependency>
      <groupId>com.github.yannrichet</groupId>
      <artifactId>Rsession</artifactId>
      <version>2.0.4</version>
    </dependency>
...
</dependencies>

Then, use it in your code:

  • create new Rsession:
    • Renjin (pure Java, no R install necessary):
      Rsession r = new RenjinSession(System.out,null);
    • OR local spawning of Rserve (for Windows XP, Mac OS X, Linux 32 & 64):
      Rsession r = RserveSession.newInstanceTry(System.out,null);
    • OR connect to remote Rserve (previously started with /usr/bin/R CMD Rserve --vanilla --RS-conf Rserve.conf):
      Rsession r = RserveSession.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 r = RserveSession.newLocalInstance(System.out,null); 
  • do your work in R and get Java objects
    • create Java objects from R command using
    Object o = r.eval("...",HashMap<String,Object> vars)
    (Object o is automatically cast to double, double[], double[][],String, String[], ...)
    • OR
      • create your R objects using r.set("...",...)
      • call any R command using r.eval("...")
      • cast to Java objects using Rsession.cast(...)
      • if needed use remote R packages install & load: r.installPackage("...", true);
      • you can access R command answers as string using: r.asHTML("...") r.asString("...") , r.toJPEG(File f,"...")
  • finally close your Rsession instance: r.end();

Analytics

rsession's People

Contributors

golgauth avatar kktuax avatar yannrichet avatar

Watchers

 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.