Rserve 实现java与R的互通

Rserve 实现java与R的互通

服务器端:     华为弹性云服务器    服务系统:WindowsServer 2012 64bit   Rserve默认端口:6311

(华为云服务器需要在网络安全组设置入方向规则并指定端口)   

               java项目所用jar包    REngine.jar    RserveEngine.jar   Rserve.jar

一、下载安装R

https://www.r-project.org/

Windows安装:注意,3.40版安装包有问题

https://cran.r-project.org/bin/windows/base/rpatched.html

二、设置系统环境变量

1、  下载安装以后,设置R相关的环境变量,本机安装(D:\R\R-3.3.2)

R_HOME   值为:D:\R\R-3.3.2

path  末尾添加 D:\R\R-3.3.2\bin;


配置完成后运行CMD  输入   R   出现如下则R环境配置成功

三、安装并载入Rserve

在R中运行  

1
2
3
>install.packages( "Rserve" )
 
>library(Rserve)

四、启动Rserve服务 
将Rserve安装目录D:\R\R-3.3.2\library\Rserve\libs\x64下三个文件拷到D:\R\R-3.3.2\bin\x64下

方法1、在R中启动Rserve  直接使用Rserve()函数。

1
2
3
4
5
6
7
>library(Rserve)
 
>Rserve()
 
Starting Rserve..
 
"D:\R\R-3.3.2\library\Rserve\libs\x64\Rserve.exe"

方法2、在“D:\R\R-3.3.2\library\Rserve\libs\x64”目录下运行CMD  输入命令 R CMD Rserve --help  出现下图说明Rserve可以启动

输入R CMD Rserve 成功启动

需要远程连接需要输入 R CMD Rserve --RS-enable-remote

 

五、Rserve的远程配置

查看Rserve配置

CMD下输入  R CMD Rserve --RS-settings  出现如下图

1
2
3
4
5
6
7
8
9
10
11
12
config file: Rserv.cfg  配置文件名称
working root:  R运行时工作目录 /tmp/Rserv
port:  6311    通信端口
local socket: TCP/IP  TCP/IP协议
authorization: 认证未开启
plain text password: 不允许明文密码
passwords file: 密码文件,未指定
allow I/O: 允许IO操作
allow remote access: 远程访问未开启
control commands: 命令控制未开启
interactive: 允许通信
max.input buffer size: 文件上传限制262mb

 新建txt文本 内容如下

1
2
3
4
5
6
7
8
9
10
workdir /tmp/Rserv
remote enable
fileio enable
interactive yes
port  6311
maxinbuf  262144
encoding utf8
control enable
source start.R
plaintext enable

 另存为名称为Rserv后缀名为.cfg的Rserv   Rserv.cfg

上述信息包含了一个start.R脚本。新建R脚本输入

1
2
cat( "This is my Rserve!!" )
print(paste( "Server start at" ,Sys.time()))

 保存并将文件放到该目录下(此脚本用于提示Rserve启动成功)

 

建立java工程 导入三个jar包添加到构建路径

DEMO:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package  com.shengtian.test;
 
import  org.rosuda.REngine.REXPMismatchException;
import  org.rosuda.REngine.Rserve.RConnection;
import  org.rosuda.REngine.Rserve.RserveException;
 
public  class  TestRserve {
 
     public  static  void  main(String[] args) {
         try
         {
             TestRserve tr =  new  TestRserve();
             tr.callRserve();
         }
         catch (Exception e)
         {
             e.printStackTrace();
         }
     }
     
     public  void  callRserve()  throws  RserveException,REXPMismatchException
     {
         RConnection rcon =  new  RConnection( "114.115.205.210" );     //建立远程链接
         String rv = rcon.eval( "R.version.string" ).asString();      //得到R版本信息
         double [] arr = rcon.eval( "rnorm(10)" ).asDoubles();         //通过R得到10个随机数返回数组
         System.out.println(rv);                                    //输出版本信息
         for ( double  x:arr)                                          //遍历集合
         {
             System.out.println(x);
         }
     }
 
}

 运行结果:

标签:  java
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值