Rsession making Java call R simpler
R's Geek Ideal series of articles, covering the ideas of R, use, tools, innovation and a series of key points, in my personal learning and experience to interpret the powerful R.
The R language, as a statistical language, has been shining brightly in niche areas. Until the outbreak of big data, the R language became a hot tool for data analysis. As more and more engineering background people join, the R language community is growing rapidly. Now it's not just statistics, education, banking, e-commerce, Internet ... all in the R language.
To become an ideal geek, we can not stay in the grammar, to grasp the solid mathematics, probability, statistical knowledge, but also to have the spirit of innovation, the R language to play in all fields. Let's move together and start the Geek ideal for R.
About
- Zhang Dan (Conan), programmer Java,r,php,javascript
- Weibo: @Conan_Z
- Blog:http://blog.fens.me
- Email: [Email protected]
Reprint please specify the source:
http://blog.fens.me/r-rserve-rsession/
Objective
Wrote a few articles about Rserve, rserve as the communication interface of R language, has become an important channel of R language extension. The advent of the big data era, so that the original small audience of the R language, inadvertently squeezed into the development of the language of the top 20.
At the same time, it engineers with multiple programming language backgrounds began to enter the R community and help the R language evolve rapidly. Rserve provides a communication interface that allows the R engine to be embedded in other languages through encapsulation.
Directory
- Rsession Introduction
- Rsession Download
- Building Rsession projects with Eclipse
- Rsession's API Introduction
- Rserve Server System Environment
- Rsession use
1. Rsession Introduction
Rsession provides a simple way for Java to access remote or local Rserve instances. Rsession is a Rserve package that provides a higher-level API interface, including Rserve server control, multi-session mechanism, and support for the Windows environment.
The other library Jri for R and Java communication does not support the multi-session mechanism. For additional articles on R and Java communication, please refer to: Rjava R and Java high-speed channel, Rserve cross-platform communication with Java
Rsession Project Home: 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: Unzip to directly use the
Http://rsession.googlecode.com/files/libRsession.zip
Includes 3 jar packages: 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 through Ant and we can build it ourselves and package it.
~ antbuildfile:d:\workspace\java\rsession\rsession\build.xmlclean:clean-dist:init: [mkdir] Created dir: D:\workspace\java\rsession\Rsession\build [mkdir] Created dir:d:\workspace\java\rsession\rsession\dist\ LibreSource: [Copy] Copying files to d:\workspace\java\rsession\Rsession\dist\lib [copy] Copied empty Direct Ories to 1 empty directory under D:\workspace\java\rsession\Rsession\dist\libcompile: [Javac] D:\workspace\java\rsessi On\rsession\build.xml:33:warning: ' Includeantruntime ' is not set, defaulting to Build.sysclasspath=last; Set to False for repeatable builds [Javac] compiling source files to D:\workspace\java\rsession\Rsession\builddist: [Jar] Building Jar:d:\workspace\java\rsession\rsession\dist\lib\rsession.jar [zip] Building Zip:d:\workspace\java\rsessi On\rsession\dist\librsession.zipbuild successfultotal time:2 seconds
Under directory: d:\workspace\java\rsession\Rsession\dist\, generate the release package, Librsession.zip
3. Building a rsession project with eclipse
Build the Rsession project with Eclipse, copy the rsession\dist\ directory file to the project, and load the environment variables into the project.
4. Rsession's API Introduction
View class Library: Rsession.jar
Interface class
- Busylistener:notify the state of R engine
- Evallistener:notify the evaluation of R expression
- Logger:support R Messages Printing
- Updateobjectslistener:notify The changing of R environment objects
Function class
- Rdaemon:rserve of the Guardian process
- Rlogpanel: Displays the space of the R log
- Robjectspanel: Controls that display R variables
- rserverconf: Connecting configuration files for Rserve instances
- Rsession: Connecting Rserve instances
- Startrserve: 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, allowing remote access
- Port: 6311
- Login Authentication: User name: Conan, Password: Conan
- Character encoding: Utf-8
The Rserve server environment is identical to the Rserve R language 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+",");}//日志输出[eval] rnorm(5) [email protected][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 a Data.frame object
Generate a graphics file locally
Output in HTML format
String html = s.asHTML("summary(rnorm(100))");System.out.println(html);//日志输出
Output in text Format
String txt = s.asString("summary(rnorm(100))"); // format in textSystem.out.println(txt);//日志输出 Min. 1st Qu. Median Mean 3rd Qu. Max. -3.19700 -0.65330 -0.09893 -0.07190 0.53300
Install a new class library
System.out.println(s.installPackage("sensitivity", true));//日志输出 trying to load package sensitivity package sensitivity is not installed. package sensitivity not yet installed.[eval] install.packages(‘sensitivity‘,repos=‘http://cran.cict.fr/‘,dependencies=TRUE) [email protected] request package sensitivity install... package sensitivity is not installed.! package sensitivity installation failed.Impossible to install package sensitivity !
View the full file: Rsessiondemo.java
~ VI rsessiondemo.javapackage 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 Rse Rverconf ("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 file S.save (new file ("./output/save. Rdata ")," demo "); Delete R Object Demo s.rm ("Demo"); S.eval ("LS ()"); Loads the R environment s.load from a file (the 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, 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"), 10, "Plot (Rnorm ())"); Output String HTML = s.ashtml in HTML format ("Summary (Rnorm (100))"); SYSTEM.OUT.PRINTLN (HTML); Output String txt = s.asstring in text Format ("Summary (Rnorm (100))"); SYSTEM.OUT.PRINTLN (TXT); Install the new Class library System.out.println (S.installpackage ("sensitivity", true)); S.end (); }}
Compare Rserve's Javaapi (refer to article: Rserve and Java cross-platform communication), is not feeling rsession again friendly!
When we use Rstudio, the rsession process can also be seen in the Task Manager! Guess, Rstudio is also using Rsession to do the programming interface.
Move your hands and create in your own hands.
Reprint please specify the source:
http://blog.fens.me/r-rserve-rsession/
Rsession making Java call R simpler