Rsession make Java call R simpler __rsession

Source: Internet
Author: User
Tags rand svn

If you are using MAVEN for management in your project, you can add the following in Pom.xml:

   <dependency>
       <groupId>com.github.yannrichet</groupId>
       <artifactId>Rsession< /artifactid>
       <version>1.8.3</version>
   </dependency>

Wrote a few articles about Rserve, rserve as the communication interface of R language, has become an important channel for R language expansion. The arrival of the large data age, so that the original small number of R language, inadvertently squeezed into the development of the first 20 languages.

At the same time, it engineers with a variety of programming language backgrounds began to enter the community of R to help the R language evolve rapidly. Rserve provides a communication interface that allows the R engine to be embedded in other languages by encapsulation.

Directory rsession Introduction rsession download the API for building Rsession Project Rsession with Eclipse Rserve Server system Environment rsession use 1. Rsession Introduction

Rsession provides an easy way for Java to access remote or local Rserve instances. Rsession is a rserve encapsulation that provides a higher level API interface, including Rserve server control, multiple session mechanisms, and support for Windows environments.

Another r and Java Communication Library Jri, does not support the multiple session mechanism. For additional articles on R and Java communications, please refer to: Rjava R and Java High Speed Channel, Rserve communication with Java across platforms

Rsession Project homepage: https://code.google.com/p/rsession/ 2. Rsession Download

System environment Win7 64bit r:3.0.1 x86_64-w64-mingw32/x64 b4bit

Release package Download: Decompression can be used directly

Http://rsession.googlecode.com/files/libRsession.zip

Includes 3 jar packs: Rengine.jar, Rserve.jar, Rsession.jar

Source code Download: (SVN)

http://rsession.googlecode.com/svn/trunk/Rsession/

~ CD D:\workspace\java
~ SVN checkout http://rsession.googlecode.com/svn/trunk/rsession-read-only
~ MV Rsession-read-only rsession
~ CD rsession\rsession

The project is built from ant, and we can compile and package it ourselves.

 ~ Ant buildfile:d:\workspace\java\rsession\rsession\build.xml clean:clean-dist:init: [mkdir] Created dir:

D:\workspace\java\rsession\Rsession\build [mkdir] Created Dir:d:\workspace\java\rsession\rsession\dist\lib Resource: [Copy] copying files to d:\workspace\java\rsession\Rsession\dist\lib [copy] Copied empty Directo Ries to 1 empty directory under D:\workspace\java\rsession\Rsession\dist\lib compile: [Javac] D:\workspace\java\rsess Ion\rsession\build.xml:33:warning: ' Includeantruntime ' wasn't set, defaulting to BU ild.sysclasspath=last; Set to False for repeatable builds [JAVAC] compiling-source files to D:\workspace\java\rsession\Rsession\build dis T: [jar] Building Jar:d:\workspace\java\rsession\rsession\dist\lib\rsession.jar [zip] building ZIP:D:\WORKSP Ace\java\rsession\rsession\dist\librsession.zip build successful total time:2 seconds 

Under directory: d:\workspace\java\rsession\Rsession\dist\, generate the release package, Librsession.zip 3. Build rsession project with Eclipse

Build the Rsession project with Eclipse, copy the rsession\dist\ directory files to the project, and load the environment variables into the project.

4. Introduction to Rsession API

View class Library: Rsession.jar

Interface class Busylistener:notify The state of R engine evallistener:notify the evaluation of R expression logger:support r Messa GES printing updateobjectslistener:notify The changing of R environment objects

Rdaemon:rserve Daemon Rlogpanel: Display R log Space Robjectspanel: Controls that display R variables rserverconf: Connection rserve instance configuration file Rsession: Connection Rserve instance St Artrserve: Start the local rserve 5. Rserve Server System Environment

Server system Environment Linux:ubuntu 12.04.2 LTS 64bit r:3.0.1 x86_64-pc-linux-gnu rserve:rserve v1.7-1

Rserve environment ip:192.168.1.201, allow remote access port: 6311 login Authentication: User name: Conan, Password: Conan character encoding: Utf-8

The Rserve server environment is the same as the Rserve client rsclient configuration in the article. 6. Rsession Use

Establish a remote connection

rserverconf rconf = new rserverconf ("192.168.1.201", 6311, "Conan", "Conan", New Properties ());
rsession s = rsession.newinstancetry (System.out, rconf);

Execute R Script

double[] Rand = S.eval ("Rnorm (5)"). Asdoubles ();
for (double ran:rand) {
    System.out.print (ran+ ",");
}

Log output
[eval] rnorm (5)
  org.rosuda.rengine.rexpdouble@5f934ad[5]{ 0.08779203903807914,0.039929482749452114,-0.8788534039223883,-0.8875740206608903,-0.8493446334021442}
0.08779203903807914,0.039929482749452114,-0.8788534039223883,-0.8875740206608903,-0.8493446334021442

R create an object and save the environment

Create an R object
s.set ("Demo", Math.random ());
S.eval ("LS ()");

Save the R environment to the local to file
s.save (new file ("./output/save. Rdata ")," demo ");

Delete R Object Demo
s.rm ("demo");
S.eval ("LS ()");

Load r environment s.load from file
(new file ("./output/save. Rdata "));
S.eval ("LS ()");
S.eval ("Print (demo)");

Log output

Create a Data.frame object

S.set ("DF", new double[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {Ten, One,}}, "x1", "X2", "x3");
Double df$x1_3 = S.eval ("df$x1[3]"). Asdouble ();
System.out.println (df$x1_3);
S.RM ("DF");

Log output

To generate a graphics file locally

S.tojpeg (New File ("./output/plot.png"), N, "plot (Rnorm)");

Log output

Output in HTML format

String html = s.ashtml ("Summary (Rnorm)");
SYSTEM.OUT.PRINTLN (HTML);

Log output

Output in text Format

String txt = s.asstring ("Summary (Rnorm (100))"); Format in text
System.out.println (TXT);

Log output
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
-3.19700-0.65330-0.09893-0.07190  0.53300  2.29000 

Installing a new Class library

System.out.println (S.installpackage ("sensitivity", true));

Log output
  trying to load package sensitivity
  Package sensitivity are not installed.
  Package sensitivity not yet installed.
[Eval] install.packages (' sensitivity ', repos= ' http://cran.cict.fr/', dependencies=true)
  ORG.ROSUDA.RENGINE.REXPNULL@4D47C5FC
  Request Package sensitivity install ...
  Package sensitivity is not installed.
!   Package sensitivity installation failed.
Impossible to install package sensitivity!

To view the complete file: Rsessiondemo.java

~ VI Rsessiondemo.java Package org.conan.r.rsession;
Import Java.io.File;

Import java.util.Properties;
Import org.math.r.rserverconf;
Import org.math.r.rsession;

Import org.rosuda.REngine.REXPMismatchException;  public class Rsessiondemo {public static void main (String args[]) throws Rexpmismatchexception {rserverconf
        rconf = new Rserverconf ("192.168.1.201", 6311, "Conan", "Conan", New Properties ());

        rsession s = rsession.newinstancetry (System.out, rconf);
        Execute R script double[] Rand = S.eval ("Rnorm (5)"). Asdoubles ();

        System.out.println (RAND);
        Create an R object S.set ("Demo", Math.random ());

        S.eval ("LS ()"); Save the R run-time state to the file S.save (new file ("./output/save.

        Rdata ")," demo ");
        Delete R Object Demo s.rm ("Demo");

        S.eval ("LS ()"); Load r environment s.load from file (new file ("./output/save.
        Rdata "));
        S.eval ("LS ()");

        S.eval ("Print (demo)"); Create a Data.frame object S.set ("DF"), New double[][] {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {Ten, One,}}, "x1", "X2", "x3");
        Double df$x1_3 = S.eval ("df$x1[3]"). Asdouble ();
        System.out.println (df$x1_3);

        S.RM ("DF");
        Generate a graphics file S.eval ("GETWD ()");

        S.tojpeg (New File ("./output/plot.png"), N, "Plot (Rnorm (10))"); 
        Output String html = s.ashtml ("Summary (Rnorm (100))" in HTML format;

        SYSTEM.OUT.PRINTLN (HTML);
        Output String txt = s.asstring ("Summary (Rnorm (100))" in text format;

        SYSTEM.OUT.PRINTLN (TXT);

        Install the new Class library System.out.println (S.installpackage ("sensitivity", true);
    S.end ();
 }
}

Compare Rserve Javaapi (Reference article: Rserve communication with Java), is not feel rsession again friendly.

When we use Rstudio, we can also see the rsession process in Task Manager. Guess what, Rstudio is also using Rsession as a program interface.

Move your hands and create them in your own hands.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.