Different methods of investigation R invoke java

Comparative R java method call, determining a final suitable manner; focus on the stability of visualization (plot, ggplot2) support and efficiency;
known methods are:
. 1: rJava: HTTPS: //cran.r-project .org / Web / Packages / rJava / index.html
2: Rserve:  http://www.rforge.net/Rserve/
. 3: Renjin:  http://www.renjin.org/

There are two ways for Rserve client connections, which is better
Rconnection and Rsession: https://github.com/yannrichet/rsession ;

1.rJava

Information Address: https://cran.r-project.org/web/packages/rJava/index.html

1.1 installation:

install.packages("rJava")

1.2 Configuration

Environment Variables
 

CLASSPARH增加:D:\Program Files\R\R-3.4.1\library\rJava\jri\JRI.jar (本地的R安装目录)
PATH增加:D:\Program Files\R\R-3.4.1\bin\x64;D:\Program Files\R\R-3.4.1\library\rJava\jri(本地的R安装目录)
idea中增加: Run-Edit Configurations-VM options -> 
-Djava.library.path="D:\Program Files\R\R-3.4.1\library\rJava\jri\x64"(本地的R安装目录)

2.Rserve

Information Address: http://www.rforge.net/Rserve/

There are two ways for Rserve client connections:

RConnection: Rserve is a TCP / IP based protocol, C R Language allows communication with other programs / S structure, support C / C ++, Java, PHP, Python, Ruby, Nodejs like. Rserve provide remote connectivity, authentication, file transfer and other functions. We can design R as a background service, processing statistical modeling, data analysis tasks, such as drawing.

Rsession: Rsession provides a simple way for Java can access the remote or local Rserve instance. Rsession is a package of Rserve, providing a higher-level API interfaces, including Rserve server control, multi-session mechanisms, and supports the Windows environment.

 2.1 installation:

install.packages('Rserve')

2.2 in the program, start Rserve server:

library(Rserve)
Rserve()

2.3 Add Rserve dependence in the project

<dependency>
<groupId>org.rosuda.REngine</groupId>
<artifactId>REngine</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.rosuda.REngine</groupId>
<artifactId>Rserve</artifactId>
<version>1.8.1</version>
</dependency>

2.4 Add Rsession dependence in the project

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

3. Renjin

Information Address:  http://www.renjin.org/

 

3.1 After downloading start

java -jar renjin-studio-0.8.2413-jar-with-dependencies.jar

3.2 add dependencies in the project

<dependency>
<groupId>org.renjin</groupId>
<artifactId>renjin-script-engine</artifactId>
<version>0.8.2413</version>
</dependency>

 4. Save the plot were tested images to a local file

4.1 rJava save plot pictures

public void plot(Rengine re){
re.eval("library(leaps)");
re.eval("states <- as.data.frame(state.x77[, c(\"Murder\", \"Population\",\"Illiteracy\", \"Income\", \"Frost\")])");
re.eval("leaps <- regsubsets(Murder~Population+Illiteracy+Income+Frost,data=states,nbest=4) ");
 
//保存为图像文件
File tempFile = null;
try {
tempFile = File.createTempFile("rjava-3", ".jpg");
String filePath = tempFile.getAbsolutePath();
re.eval("jpeg('./output/rjava-3.jpg')");
re.eval("plot(leaps,scale = \"adjr2\")");
re.eval("dev.off()");
 
} catch (IOException e) {
e.printStackTrace();
} finally {
re.end();
}
 
}

4.2 Rserve save plot pictures

public void plot() throws RserveException, REXPMismatchException, IOException {
RConnection re = new RConnection("localhost");
REXP x = re.eval("R.version.string");
System.out.println(x.asString());
 
re.eval("library(leaps)");
re.eval("states <- as.data.frame(state.x77[, c(\"Murder\", \"Population\",\"Illiteracy\", \"Income\", \"Frost\")])");
re.eval("leaps <- regsubsets(Murder~Population+Illiteracy+Income+Frost,data=states,nbest=4) ");
 
//保存为图像文件
File tempFile = null;
try {
tempFile = File.createTempFile("rserve-3", ".jpg");
String filePath = tempFile.getAbsolutePath();
re.eval("jpeg('d://rserve-3.jpg')");
re.eval("plot(leaps,scale = \"adjr2\")");
re.eval("dev.off()");
} catch (IOException e) {
e.printStackTrace();
} catch (REngineException e) {
e.printStackTrace();
}
re.close();
}

4.3 Rsession save plot pictures

public void plot(){
RserverConf rconf = new RserverConf("localhost", 6311, "conan", "conan", new Properties());
Rsession re = Rsession.newInstanceTry(System.out,rconf);
 
re.eval("library(leaps)");
re.eval("states <- as.data.frame(state.x77[, c(\"Murder\", \"Population\",\"Illiteracy\", \"Income\", \"Frost\")])");
re.eval("leaps <- regsubsets(Murder~Population+Illiteracy+Income+Frost,data=states,nbest=4) ");
 
re.eval("getwd()");
re.toJPEG(new File("./output/rsession-3.png"), 400, 400, "print(plot(leaps,scale = \"adjr2\"))");
re.end();
 
}

5. Save ggplot results Pictures

   Note: need to add print method, otherwise it will be blank.

Rsession:
re.toJPEG(new File("./output/rsession-1.png"), 400, 400, "print(ggplot(data=mpg,aes(x=displ,y=hwy,colour=factor(cyl)))+ geom_point() + geom_smooth())");
Rserve:
re.eval("print(ggplot(plot.data, aes(x=x,y=p,color=factor(delall$delevery_Type)))+geom_point()+ggtitle(\"对分娩方式的预测概率结果与其真实类别图\")+labs(x=\"观测点\",y=\"预测概率\",color=\"实际分娩方式\")+\n" +
" guides(color=guide_legend(title=\"实际分娩方式\",title.theme = element_text(size = 15,face = \"bold\",colour = \"red\",angle = 0)))+mytheme)");
rJava:
re.eval("print(ggplot(data=mpg,aes(x=displ,y=hwy,colour=factor(cyl)))+ geom_point() + geom_smooth())");
 

 6. Comparison and Selection

For more information listed in the table below, combined with the actual needs of our project, and ultimately determine the use of Rserve-Rsession.

The reason: rJava need to bind together the R environment and the Java Runtime Environment, it is not considered a

          R Renjin not rely environment, but finite outer cladding provided R, but does not currently support the drawing, it is not considered

          Rsession is Rserve upper package, providing higher-level API, using more friendly

 

name

brown

Rserve-
Rconnection

Rserve-
Rsession

Renjin

installation JDK
configuration environment variable
install.packages("Rserve")
library(Rserve)
Rserve()
install.packages("Rserve")
library(Rserve)
Rserve()
Dependencies JRI.jar Rengine.j is
Rserve.j is
Rsession.jar
REngine.jar,
Rserve.jar
renjin-script-engine.jar
principle rJava R is a communication interface language and the Java language, by the underlying implementation calls the JNI allows Java objects, and the direct method calls in R. rJava also provides the functionality of Java call R, is achieved by JRI (Java / R Interface). JRI now been embedded rJava package, we can also use this feature alone. Now rJava package, has become the basis for many of the functional components based on Java development R package. Rserve client and server mode, you need to start Rserve service and client TCP / IP to communicate through.
Communication between the TCP / IP protocol-based and multi-language, C R Language allows communication with other programs / S structure, support C / C ++, Java, PHP , Python, Ruby, Nodejs etc.
Rsession is a package of Rserve, providing a higher-level API interfaces, including Rserve server control, multi-session mechanisms, and supports the Windows environment. Renjin, the R can execute code in Java JVM-based interface, Java code may be executed in R.
When Renjin Script Engine, R bag exactly like Java and Scala dependencies, must by Maven or other build tools, the R dependencies on the classpath
Select online comments rJava or provide an underlying communication mechanism R and system level.
If you need to embed substantially R java code snippet, use rJava
If you need to create an R server accepts requests from Java, and the results returned java, it is to use Rserve Contrast Rserve of JavaAPI, Rsession feel more friendly. This will open without compiler R, R of a packet service using the bottom. More suitable for packaging R. http://packages.renjin.org.
Shortcoming Code execution environment needs and R in the same machine, comprising a JDK

different configuration different systems, different Windows and Linux configuration

rJava achieving R and Java communications via JNI (Java Native Interface), and therefore R and Java execution environment We need to coexist on the same machine.

It does not support multi-session mechanism
Rserve need to start the service in R, you can start Rserve the service code Rserve need to start the service in R, you can start Rserve the service code Ggplot not currently supported plot and other drawing operations, finite support R Package
advantage rJava can be used as API to use. It does not involve any client - server-side communication. Code for direct execution by R rJava.
Since rJava is a low-level interface, as an interface and uses JNI calls, so the efficiency is very high. In JRI scheme, the JVM loaded directly by the RVM direct memory, the process calls almost no performance loss, and therefore is very efficient connecting channel, and R is the preferred Java Development Kit communication.
Compared with rJava, Rserve distributed with advantages and low coupling.

Rserve provide remote connectivity, authentication, file transfer and other functions. We can design R as a background service, processing statistical modeling, data analysis, mapping and other tasks

a bit like JDBC, establishing a connection to
Rsession provides a simple way for Java can access the remote or local Rserve instance. Rsession is a package of Rserve, providing a higher-level API interfaces, including Rserve server control, multi-session mechanisms, and supports the Windows environment.  Environment does not depend on R

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/zwahut/article/details/90602783