R语言服务器程序 Rserve详解

R的极客理想系列文章,涵盖了R的思想,使用,工具,创新等的一系列要点,以我个人的学习和体验去诠释R的强大。

R语言作为统计学一门语言,一直在小众领域闪耀着光芒。直到大数据的爆发,R语言变成了一门炙手可热的数据分析的利器。随着越来越多的工程背景的人的加入,R语言的社区在迅速扩大成长。现在已不仅仅是统计领域,教育,银行,电商,互联网….都在使用R语言。

要成为有理想的极客,我们不能停留在语法上,要掌握牢固的数学,概率,统计知识,同时还要有创新精神,把R语言发挥到各个领域。让我们一起动起来吧,开始R的极客理想。

关于作者:

  • 张丹(Conan), 程序员Java,R,PHP,Javascript
  • weibo:@Conan_Z
  • blog: http://blog.fens.me
  • email: bsspirit@gmail.com

转载请注明出处:
http://blog.fens.me/r-rserve-server/

前言

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

本文详细剖析Rserve作为服务器端应用的配置和使用。

目录

  1. Rserve介绍
  2. Rserve简单使用
  3. Rserve高级使用:配置管理
  4. Rserve高级使用:用户登陆认证

1. Rserve介绍

关于Rserve上次说的不够细,用到起来处处有坑。接下来,深入细节再学学。

上篇文章请参考:Rserve与Java的跨平台通信

Rserve介绍

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

官方介绍:http://www.rforge.net/Rserve/

2. 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-port listen on the specified TCP port
--RS-socket use specified local (unix) socket instead of TCP/IP.
--RS-workdir use specified working directory root for connections.
--RS-encoding set default server string encoding to .
--RS-conf load additional config file.
--RS-settings dumps current settings of the Rserve
--RS-source source 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 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

修改默认配置,新建文件:/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

允许远程访问, 不限制IP

0 0.0.0.0:6311

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

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


~ R > library(RSclient)
> conn<-RS.connect()
> 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.connect()
> 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.connect()
> 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参数,从而更细粒度的控制服务器权限。

本文详细地介绍了Rserve的安装,配置,使用,以及Rserve支持的功能。有了知识积累,我们就可以利用Rserve,来构建企业级的上线应用了。

R语言服务器程序 Rserve详解的更多相关文章

  1. Rserve详解,R语言客户端RSclient【转】

    R语言服务器程序 Rserve详解 http://blog.fens.me/r-rserve-server/ Rserve的R语言客户端RSclient https://blog.csdn.net/u ...

  2. 一个简单的C语言程序(详解)

    C Primer Plus之一个简单的C语言程序(详解) #include <stdio.h> int main(void) //一个简单的 C程序 { int num; //定义一个名为 ...

  3. Linux C 语言之 Hello World 详解

    目录 Linux C 语言之 Hello World 详解 第一个 C 语言程序 程序运行原理 编译,链接 运行时 链接库 编译器优化 Hello World 打印原理 stdout, stdin 和 ...

  4. 【HANA系列】SAP HANA XS使用服务器JavaScript Libraries详解

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA XS使用服务器 ...

  5. [转]C语言字节对齐问题详解

    C语言字节对齐问题详解 转载:https://www.cnblogs.com/clover-toeic/p/3853132.html 引言 考虑下面的结构体定义: typedef struct{ ch ...

  6. 【HANA系列】【第四篇】SAP HANA XS使用服务器JavaScript Libraries详解

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列][第四篇]SAP HANA XS ...

  7. 嵌入式Linux应用程序开发详解------(创建守护进程)

    嵌入式Linux应用程序开发详解 华清远见 本文只是阅读文摘. 创建一个守护进程的步骤: 1.创建一个子进程,然后退出父进程: 2.在子进程中使用创建新会话---setsid(): 3.改变当前工作目 ...

  8. Linux Bash命令关于程序调试详解

    转载:http://os.51cto.com/art/201006/207230.htm 参考:<Linux shell 脚本攻略>Page22-23 Linux bash程序在程序员的使 ...

  9. VS2010开发程序打包详解

    VS2010开发程序打包详解 转自:http://blog.sina.com.cn/s/blog_473b385101019ufr.html 首先打开已经完成的工程,如图: 下面开始制作安装程序包. ...

随机推荐

  1. [BUUCTF]REVERSE——[MRCTF2020]Xor

    [MRCTF2020]Xor 附件 步骤: 例行检查,32位程序,无壳 32位ida载入,首先检索程序里的字符串,根据字符串的提示,跳转到程序的关键函数 根据flag,跳转到sub_401090函数 ...

  2. AtCoder Beginner Contest 169 题解

    AtCoder Beginner Contest 169 题解 这场比赛比较简单,证明我没有咕咕咕的时候到了! A - Multiplication 1 没什么好说的,直接读入两个数输出乘积就好了. ...

  3. CF158A Next Round 题解

    Content 有 \(n\) 个人参加比赛,第 \(i\) 名在第一轮的分数是 \(a_i\)(保证 \(a_i\geqslant a_{i+1}\))已知下一轮预计能进 \(k\) 人,当然如果有 ...

  4. CF513B1 Permutations 题解

    Content 给定两个整数 \(n,m\).定义 \(f(p)=\sum\limits_{l=1}^n\sum\limits_{r=l}^n\min\limits_{i=l}^rp_i\),其中 \ ...

  5. LuoguB2104 矩阵加法 题解

    Content 给定两个 \(n\times m\) 的矩阵 \(A,B\),求 \(C=A+B\). 数据范围:\(1\leqslant n,m\leqslant 100\). Solution 我 ...

  6. AT4151 [ABC099B] Stone Monument 题解

    Content 一个村里有 \(999\) 个房子,第 \(i\) 个房子的高度为 \(1+2+...+i=\sum\limits_{j=1}^ij\).现在下了一场雪,给定相邻两个房子没被雪覆盖的高 ...

  7. myeclipse 安装spket

    myeclipse 安装spket为以下步骤 请注意:1.myPlugins为新建的文件夹 2.spket-1.6.23为新建的文件 如上步骤为安装spket,那么如下的步骤为让myeclipse里的 ...

  8. 「算法笔记」Splay

    一.简介 Splay(伸展树)是平衡树中的一种.它通过不断将某个节点旋转到根节点的位置,使整棵树仍满足 BST 的性质,并且保持平衡而不至于退化为链. 频繁访问的节点会被移动到离根节点较近的位置,进而 ...

  9. JSP中的九大内置对象

    JSP九大内置对象 pageContext 存东西 Request 存东西 Response Session 存东西 Application(servletContext) 存东西 config(se ...

  10. Elasticsearch(二)--集群原理及优化

    一.ES原理 1.索引结构ES是面向文档的 各种文本内容以文档的形式存储到ES中,文档可以是一封邮件.一条日志,或者一个网页的内容.一般使用 JSON 作为文档的序列化格式,文档可以有很多字段,在创建 ...