关于ubuntu下R的安装在上一篇博文《Ubuntu安装更新最新版本R》中已经讲过了,这篇博文主要介绍的是如何使用Java来调用R。
1. Rserve的安装与配置
Rserve是个什么东西?从这个词面的意思来理解就是R的一个服务,主要是提供远程连接的服务,当然功能是很多的,具体可参考:https://rforge.net/Rserve/
在安装Rserve之前确保R能够正常运行,并正确配置了R的环境变量(Linux与windows用户都需要注意)。
在此,强烈建议安装R 3.2.5
以上的R版本,否则可能会遇到这种奇葩错误。废话不多说,我们直接进入Rserve的安装与配置。
- 运行R,安装Rserve包
~ sudo R
> install.packages("Rserve")
- 查看Rserve配置
~ R CMD Rserve --RS-settings
Rserve v1.7-3
config file: /etc/Rserv.conf
working root: /tmp/Rserv
port: 6311
local socket: [none, TCP/IP used]
authorization required: no
plain text password: not allowed
passwords file: [none]
allow I/O: yes
allow remote access: no
control commands: no
interactive: yes
max.input buffer size: 262144 kB
config file: 本地无此文件/etc/Rserv.conf
working root: R运行时工作目录 /tmp/Rserv
port: 端口6311
local socket: TCP/IP协议
authorization: 认证未开启
plain text password: 不允许明文密码
passwords file: 密码文件,未指定
allow I/O: 允许IO操作
allow remote access: 远程访问未开启
control commands: 命令控制未开启
interactive: 允许通信
max.input buffer size: 文件上传限制262mb
- 创建配置文件
~ sudo vi /etc/Rserv.conf
workdir /tmp/Rserv
remote enable
fileio enable
auth required
plaintext enable
fileio enable
interactive yes
port 6311
maxinbuf 262144
encoding utf8
control enable
开启远程访问权限后配置信息
~ R CMD Rserve --RS-settings
Rserve v1.7-3
config file: /etc/Rserv.conf
working root: /tmp/Rserv
port: 6311
local socket: [none, TCP/IP used]
authorization required: yes
plain text password: allowed
passwords file: [none]
allow I/O: yes
allow remote access: yes
control commands: yes
interactive: yes
max.input buffer size: 262144 kB
- 指定配置文件
~ R CMD Rserve --RS-conf /etc/Rserv.conf
- 启动Rserve远程模式
~ R CMD Rserve --RS-enable-remote
- 查看启动后Rserve端口
~ netstat -nltp|grep Rserve
tcp 0 0 0.0.0.0:6311 0.0.0.0:* LISTEN 73821/Rserve
0.0.0.0:6311
代表不受限的IP访问
2. Java远程连接Rserve
首先,我们需要下载Rserve所使用到的两个Jar包:REngine.jar
和RserveEngine.jar
,下载地址:http://www.rforge.net/Rserve/files/
如果项目使用maven进行管理,那么将下面的内容放入pom.xml
文件中:
<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>
具体的Java连接代码在这就不给出了,大家可以参考官网给出的Example(https://rforge.net/Rserve/example.html).
在项目中我主要是使用Rsession来和R进行通信,我将在下一篇文章详细介绍Rsession的用法和”keng”。
参考: