rserve php,RServe简介

前言

Rserve作为一个R语言与其他语言的通信(TCP/IP)接口,被多个项目所依赖。Rserve服务端配置和运行都非常简单,客户端由多种语言实现,像C/C++, Java等。R也有自己的客户端实现 RSclient 项目。

Rserve介绍

Rserve是一个基于TCP/IP协议的,允许R语言与其他语言通信的C/S结构的程序,支持C/C++,Java,PHP,Python,Ruby,Nodejs等。 Rserve提供远程连接,认证,文件传输等功能。我们可以设计R做为后台服务,处理统计建模,数据分析,绘图等的任务。

Rserve安装

服务器系统环境Linux: Ubuntu 12.04.2 LTS 64bit

R: 3.0.1 x86_64-pc-linux-gnu

Rserve安装和加载~ R

> install.packages("Rserve")

> library(Rserve)

Rserve服务器1). 在程序中,启动Rserve服务器

2). 在命令行,启动Rserve服务器

Rserve的函数,只有3个。Rserve(): 单独启动一个守护进程作为Rserve实例。

run.Rserve(): 在当前的进程中,启动Rserve实例

self():

1). 在程序中,启动Rserve服务器> library(Rserve)

> Rserve()

Starting Rserve:

/usr/lib/R/bin/R CMD /home/conan/R/x86_64-pc-linux-gnu-library/3.0/Rserve/libs//Rserve

R version 3.0.1 (2013-05-16) -- "Good Sport"

Copyright (C) 2013 The R Foundation for Statistical Computing

Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.

You are welcome to redistribute it under certain conditions.

Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.

Type 'contributors()' for more information and

'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or

'help.start()' for an HTML browser interface to help.

Type 'q()' to quit R.

Rserv started in daemon mode.

查看Rserv进程~ ps -aux|grep Rserve

conan     8830  0.0  1.2 116336 25044 ?        Ss   06:46   0:00 /home/conan/R/x86_64-pc-linux-gnu-library/3.0/Rserve/libs//Rserve

~ netstat -nltp|grep Rserve

tcp        0      0 127.0.0.1:6311          0.0.0.0:*               LISTEN      8830/Rserve

2). 在命令行,启动Rserve服务器

Rserve命令帮助~ R CMD Rserve --help

Usage: R CMD Rserve []

Options: --help  this help screen

--version  prints Rserve version (also passed to R)

--RS-portlisten on the specified TCP port

--RS-socketuse specified local (unix) socket instead of TCP/IP.

--RS-workdiruse specified working directory root for connections.

--RS-encodingset default server string encoding to.

--RS-confload additional config file.

--RS-settings  dumps current settings of the Rserve

--RS-sourcesource the specified file on startup.

--RS-enable-control  enable control commands

--RS-enable-remote  enable remote connections

All other options are passed to the R engine.

在命令行启动Rserve~ R CMD Rserve --RS-enable-remote

R version 3.0.1 (2013-05-16) -- "Good Sport"

Copyright (C) 2013 The R Foundation for Statistical Computing

Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.

You are welcome to redistribute it under certain conditions.

Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.

Type 'contributors()' for more information and

'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or

'help.start()' for an HTML browser interface to help.

Type 'q()' to quit R.

Rserv started in daemon mode.

查看Rserv进程~ ps -aux|grep Rserve

conan    27639  0.0  1.2 116288 25236 ?        Ss   20:41   0:00 /usr/lib/R/bin/Rserve --RS-enable-remote

~ netstat -nltp|grep Rserve

tcp        0      0 0.0.0.0:6311            0.0.0.0:*               LISTEN      27639/Rserve

3. Rserve高级使用:Rserve配置管理

通过配置文件,管理Rserve服务器。Rserv.conf: 配置文件

source.R:初始化脚步文件

查看当前的Rserve 服务器的默认配置信息~ R CMD Rserve --RS-settings

Rserve v1.7-1

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 kBconfig 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

修改默认配置,新建文件:/etc/Rserv.conf~ sudo vi /etc/Rserv.conf

workdir /tmp/Rserv

remote enable

fileio enable

interactive yes

port 6311

maxinbuf 262144

encoding utf8

control enable

source /home/conan/R/RServe/source.R

eval xx=1

~ vi /home/conan/R/RServe/source.R

cat("This is my Rserve!!")

print(paste("Server start at",Sys.time()))

再次查看服务器配置R CMD Rserve --RS-settings

Rserve v1.7-1

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

R version 3.0.1 (2013-05-16) -- "Good Sport"

Copyright (C) 2013 The R Foundation for Statistical Computing

Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.

You are welcome to redistribute it under certain conditions.

Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.

Type 'contributors()' for more information and

'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or

'help.start()' for an HTML browser interface to help.

Type 'q()' to quit R.

This is my Rserve!![1] "Server start at 2013-10-30 22:38:10"

Rserv started in daemon mode.

查看日志:source.R在启动时候被执行。"This is my Rserve!![1] "Server start at 2013-10-30 22:38:10""

source选项,用来配置Rserve服务器启动时加载的文件,例如:初始化系统变量,初始化系统函数等。eval选项,直接定义环境变量。

查看进程~ ps -aux|grep Rserve

conan    28339  0.0  1.2 116292 25240 ?        Ss   22:31   0:00 /usr/lib/R/bin/Rserve

~ netstat -ntlp|grep Rserve

tcp        0      0 0.0.0.0:6311            0.0.0.0:*               LISTEN      28339/Rserve

允许远程访问, 不限制IP0 0.0.0.0:6311

4. Rserve高级使用:用户登陆认证

在本地环境,无认证情况下,使用RSclient访问Rserve。RSclient的使用,请参考文章:Rserve的R语言客户端RSclient~ R

>  library(RSclient)

>  conn

>  RS.eval(conn,rnorm(10))

[1]  0.03230305  0.95710725 -0.33416069 -0.37440009 -1.95515719 -0.22895924

[7]  0.39591984  1.67898842 -0.01666688 -0.26877775

增加用户登陆认证,修改文件:/etc/Rserv.conf~ sudo vi /etc/Rserv.conf

workdir /tmp/Rserv

remote enable

fileio enable

interactive yes

port 6311

maxinbuf 262144

encoding utf8

control enable

source /home/conan/R/RServe/source.R

eval xx=1

auth required

plaintext enable

使用RSclient访问,认证报错。> library(RSclient)

> conn

> RS.eval(conn,rnorm(10))

Error in RS.eval(conn, rnorm(10)) :

command failed with status code 0x41: authentication failed

用RSclient登陆> library(RSclient)

> conn

> RS.login(conn,"conan","conan",authkey=RS.authkey(conn))

[1] TRUE

> RS.eval(conn,rnorm(5))

[1] -1.19827684  0.72164617  0.22225934  0.09901505 -1.54661436

这里用户登陆认证,是绑定的操作系统用户。我们还可以在Rserv.conf配置文件,指定uid,gid参数,从而更细粒度的控制服务器权限。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Rserve_1.8-1.tar.gz是Rserve的一个版本,可以从官网下载。你可以通过以下链接下载Rserve的源码包:http://www.rforge.net/Rserve/snapshot/Rserve_1.8-1.tar.gz。\[3\]在安装Rserve之前,你需要先安装R和可能用到的依赖包。你可以从以下链接下载R的源码包:https://mirrors.tuna.tsinghua.edu.cn/CRAN/src/base/R-3/R-3.5.1.tar.gz。\[3\]安装Rserve时,你还需要下载REngine.jar和RserveEngine.jar这两个文件,你可以从以下链接下载:http://www.rforge.net/Rserve/files/REngine.jar和http://www.rforge.net/Rserve/files/RserveEngine.jar。\[3\]在安装过程中,如果提示缺少依赖包,你可以根据提示进行安装。安装完成后,你可以使用ps命令查看Rserve进程是否成功启动。\[1\]如果需要开启Rserve的远程调用功能,你可以使用以下命令:/usr/lib64/R/bin/R CMD /home/ruser/R/x86_64-redhat-linux-gnu-library/3.3/Rserve/libs//Rserve --RS-enable-remote。\[2\] #### 引用[.reference_title] - *1* *3* [非Root用户下搭建Rserve环境](https://blog.csdn.net/Jack_Lue/article/details/85267874)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【笔记】java调用R,R调用hadoop](https://blog.csdn.net/w8998036/article/details/93178202)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值