R的极客理想系列文章,涵盖了R的思想,使用,工具,创新等的一系列要点,以我我的的学习和体验去诠释R的强大。 java
R语言做为统计学一门语言,一直在小众领域闪耀着光芒。直到大数据的爆发,R语言变成了一门煊赫一时的数据分析的利器。随着愈来愈多的工程背景的人的加入,R语言的社区在迅速扩大成长。如今已不单单是统计领域,教育,银行,电商,互联网….都在使用R语言。 linux
要成为有理想的极客,咱们不能停留在语法上,要掌握牢固的数学,几率,统计知识,同时还要有创新精神,把R语言发挥到各个领域。让咱们一块儿动起来吧,开始R的极客理想。
关于做者: 程序员
转载请注明出处:
http://blog.fens.me/r-rserve-java/ redis
前言 apache
如今主流的异构跨平台通讯组件Apache Thrift已经火遍大江南北,支持15种编程语言,可是到目前为止尚未加入R语言。要让R实现跨平台的通讯,就只能从R的社区中找方案,像rJava,RCpp,rpy都是2种语言结合的方案,这些方案相似地会把R引擎加载到其余的语言内存环境。优势是高效,缺点是紧耦合,扩展受限,接口程序没法重用。 编程
Rserve给了咱们一种新的选择,抽象R语言网络接口,基于TCP/IP协议实现与多语言之间的通讯。让咱们体验一下Rserve与Java的跨平台通讯。 网络
目录 架构
Rserve是一个基于TCP/IP协议的,容许R语言与其余语言通讯的C/S结构的程序,支持C/C++,Java,PHP,Python,Ruby,Nodejs等。 Rserve提供远程链接,认证,文件传输等功能。咱们能够设计R作为后台服务,处理统计建模,数据分析,绘图等的任务。 tcp
系统环境:
Linux Ubuntu 12.04.2 LTS 64bit server
R 3.0.1 64bit 编程语言
~ uname -a Linux conan 3.5.0-23-generic #35~precise1-Ubuntu SMP Fri Jan 25 17:13:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux ~ cat /etc/issue Ubuntu 12.04.2 LTS \n \l ~ R --version 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 the terms of the GNU General Public License versions 2 or 3. For more information about these matters see http://www.gnu.org/licenses/.
Rserve安装
#建议使用root权限安装 ~ sudo R > install.packages("Rserve") installing via 'install.libs.R' to /usr/local/lib/R/site-library/Rserve ** R ** inst ** preparing package for lazy loading ** help *** installing help indices ** building package indices ** testing if installed package can be loaded * DONE (Rserve)
启动Rserve
~ 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. Rserv started in daemon mode. #查看进程 ~ ps -aux|grep Rserve conan 7142 0.0 1.2 116296 25240 ? Ss 09:13 0:00 /usr/lib/R/bin/Rserve #查看端口 ~ netstat -nltp|grep Rserve tcp 0 0 127.0.0.1:6311 0.0.0.0:* LISTEN 7142/Rserve
这时Rserve已经启动,端口是6311。接下来,咱们来简单地用一下。
1). 远程链接Rserve
刚刚启动时,使用的本地模式,若是想运程链接须要增长参数 –RS-enable-remote
#杀掉刚才的Rserve守护进程 ~ kill -9 7142 #打开远程模式从新启动 ~ R CMD Rserve --RS-enable-remote #查看端口 ~ netstat -nltp|grep Rserve tcp 0 0 0.0.0.0:6311 0.0.0.0:* LISTEN 7173/Rserve
0 0.0.0.0:6311,表示不限IP访问了。
2). 下载Java客户端JAR包
下载Java客户端JAR包:http://www.rforge.net/Rserve/files/
3). 建立Java工程
在Eclipse中新建Java工程,并加载JAR包环境中。
4). Java编程实现
package org.conan.r.rserve; import org.rosuda.REngine.REXP; import org.rosuda.REngine.REXPMismatchException; import org.rosuda.REngine.Rserve.RConnection; import org.rosuda.REngine.Rserve.RserveException; public class Demo1 { public static void main(String[] args) throws RserveException, REXPMismatchException { Demo1 demo = new Demo1(); demo.callRserve(); } public void callRserve() throws RserveException, REXPMismatchException { RConnection c = new RConnection("192.168.1.201"); REXP x = c.eval("R.version.string"); System.out.println(x.asString());//打印变量x double[] arr = c.eval("rnorm(10)").asDoubles(); for (double a : arr) {//循环打印变量arr System.out.print(a + ","); } } }
5). 运行结果
R version 3.0.1 (2013-05-16) 1.7695224124757984,-0.29753038160770323,0.26596993631142246,1.4027325257239547,-0.30663565983302676,-0.17594309812158912,0.10071253841443684,0.9365455161259986,0.11272119436439701,0.5766373030674361,
经过Rserve很是简单地实现了,Java和R的通讯。
解决了通讯的问题,咱们就能够发挥想象,把R更普遍的用起来。
接下来,会讲到如何设计Java和R互相调用的软件架构。敬请关注….
转载请注明出处:
http://blog.fens.me/r-rserve-java/