關於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”。
參考: