Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call R remotely by Java program through Rserve

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how Java programs call R remotely through Rserve". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Rserve introduction

Rserve is a program based on TCP/IP protocol, which allows R language to communicate with other languages. It supports CHP PythonJournal, Ruby, Nodejs, etc. Rserve provides remote connection, authentication, file transfer and other functions. We can design R as a background service to deal with statistical modeling, data analysis, drawing and other tasks.

For more information, please refer to the official website: https://rforge.net/Rserve/

Rserve installation

Note: you need to install R before installing Rserve. I installed R-3.1.2-win.exe.

It is recommended to use root permission to install on Linux system. The following is described according to the installation process in the Windows7 system.

Enter the R language interpreter: r

Installation command: install.packages ("Rserve")

Then the installer will prompt us to choose to download the image, as follows:

-Please select a CRAN mirror for use in this session

After we select the image (China (xxx) is selected during the test), the installer downloads and installs, and finally outputs the following information (the output information under Linux may be different):

Indicates that Rserve was installed successfully and the installation path is:% R_HOME%/library/Rserve directory.

Start the Rserve service

In the Windows command window to enter the R_HOME/library/Rserve/libs/i386 directory, execute the following command (my Windows7 is 32-bit, if it is a 64-bit system corresponding to the directory is R_HOME/library/Rserve/libs/x64), be sure to enter the directory under the dos window, otherwise the following command does not recognize:

R CMD Rserve Note: if you allow IP remote connections, use R CMD Rserve-- RS-enable-remote

R will respond as follows:

This indicates that the Rserve starts successfully (port 6311) and is ready for connection testing.

Note: if an error is reported when executing the R CMD Rserve command, R does not recognize the command, and the environment variable of R needs to be configured as follows:

R_HOME R installation directory (my installation directory is: C:\ Program Files\ R\ R Mel 2.15.2)

Add to path

C:\ Program Files\ R\ R Murray 2.15.2\ bin

C:\ Program Files\ R\ Rmuri 2.15.2\ bin\ x64

Java remote connection Rserve

Download dependent jar package, address: http://www.rforge.net/Rserve/files/

There are two jar packages. After downloading, add them to the classpath of the project:

 REngine.jar

 RserveEngine.jar

You can also look for it in the Rserve installation directory. For example, there is a related jar package in the R_HOME/library/Rserve/java directory with a slightly different name, which can be used after testing:

Simple test code:

Package com.zyh.up.general.rjava

Import org.rosuda.REngine.REXPMismatchException

Import org.rosuda.REngine.Rserve.RConnection

Import org.rosuda.REngine.Rserve.RserveException

Public class RserveBegin {

Public static void main (String [] args) {

Try {

CallRserve ()

} catch (RserveException e) {

E.printStackTrace ()

} catch (REXPMismatchException e) {

E.printStackTrace ()

}

}

Static void callRserve () throws RserveException, REXPMismatchException {

RConnection rConnection = new RConnection ("192.168.101.122"); / / write IP directly. If an error is reported, it means that R CMD Rserve-- RS-enable-remote is not used to start.

String rv = rConnection.eval ("R.version.string") .asString

System.out.println (rv)

Double [] arr = rConnection.eval ("rnorm (10)") .asDoubles ()

For (double d: arr) {

System.out.println (d)

}

}

}

The test can pass, and the program will output R language version information and 10 numbers of Double type.

Java remotely executes R script

The above example just executes the R command, but how do we execute the script or call functions in it if we put an R script on the server?

There are two ways to accomplish the above problem.

If we define an R script area.R that defines a method area that takes a radius value and then calculates the corresponding area and returns, the script is very simple:

Area

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report