• Posts tagged "R"
  • (Page 19)

Blog Archives

解惑rJava R与Java的高速通道

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-rjava-java
rjava

前言
Java语言在工业界长期处于霸主地位,Java语法、JVM、JDK、Java开源库,在近10年得到了爆发式的发展,几乎覆盖了应用开发的所有领域。伴随着Java的全领域发展,问题也随之而来了。语法越来越复杂,近似的项目越来越多,学好Java变得很难。对于没有IT背景的统计人员,学用Java更是难于上青天。

R一直是统计圈内处于佼佼者的语言,语法简单,学习曲线不太长也不太陡。如果能结合Java的通用性和R的专业性,碰撞出的火花,将会缤纷绚烂。

本文将介绍R与Java连接的高速通道,rJava通信方案。另外一篇文章介绍的Rserve通信方案,请参考: Rserve与Java的跨平台通信

目录

  1. rJava介绍
  2. rJava安装
  3. rJava实现R调用Java
  4. rJava(JRI)实现Java调用R (win7)
  5. rJava(JRI)实现Java调用R (Ubuntu)

1. rJava介绍

rJava是一个R语言和Java语言的通信接口,通过底层JNI实现调用,允许在R中直接调用Java的对象和方法。

rJava还提供了Java调用R的功能,是通过JRI(Java/R Interface)实现的。JRI现在已经被嵌入到rJava的包中,我们也可以单独试用这个功能。现在rJava包,已经成为很多基于Java开发R包的基础功能组件。

正式由于rJava是底层接口,并使用JNI作为接口调用,所以效率非常高。在JRI的方案中,JVM直接通过内存直接加载RVM,调用过程性能几乎无损耗,因此是非常高效连接通道,是R和Java通信的首选开发包。

2. rJava安装

系统环境:

  • Linux Ubuntu 12.04.2 LTS 64bit server
  • R version 3.0.1 64bit
  • Java (Oracle SUN) 1.6.0_29 64bit Server VM

~ 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/.

~ java -version
java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02, mixed mode)

rJava安装


#配置rJava环境
~ sudo R CMD javareconf

#启动R
~ sudo R
> install.packages("rJava")
installing via 'install.libs.R' to /usr/local/lib/R/site-library/rJava
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (rJava)

The downloaded source packages are in
        ‘/tmp/RtmpiZyCE7/downloaded_packages’

3. rJava实现R调用Java

在R环境中,使用rJava包编程


#加载rJava包
> library(rJava)
> search()
 [1] ".GlobalEnv"        "package:rJava"     "package:stats"
 [4] "package:graphics"  "package:grDevices" "package:utils"
 [7] "package:datasets"  "package:methods"   "Autoloads"
[10] "package:base"

#启动JVM
> .jinit()

#声明并赋值到字符串
> s <- .jnew("java/lang/String", "Hello World!")
> s
[1] "Java-Object{Hello World!}"

#查看字符串长度
> .jcall(s,"I","length")
[1] 12

#索引World的位置
> .jcall(s,"I","indexOf","World")
[1] 6

#查看concat的方法声明
> .jmethods(s,"concat")
[1] "public java.lang.String java.lang.String.concat(java.lang.String)"

#使用concat方法连接字符串
> .jcall(s,"Ljava/lang/String;","concat",s)
[1] "Hello World!Hello World!"

#打印字符串对象
> print(s)
[1] "Java-Object{Hello World!}"

#打印字符串值
> .jstrVal(s)
[1] "Hello World!"

rJava优化过的方法调用,用$来调用方法


#同.jcall(s,"I","length")
> s$length()
[1] 12

#同.jcall(s,"I","indexOf","World")
> s$indexOf("World")
[1] 6

4. rJava(JRI)实现Java调用R (win7)

在win7中安装rJava

系统环境:

  • win7 64bit 旗舰版
  • R 3.0.1
  • Java 1.6.0_45

设置环境变量


PATH: C:\Program Files\R\R-3.0.1\bin\x64;D:\toolkit\java\jdk6\bin;;D:\toolkit\java\jdk6\jre\bin\server
JAVA_HOME: D:\toolkit\java\jdk6
CLASSPATH: C:\Program Files\R\R-3.0.1\library\rJava\jri

在R中安装rJava


> install.packages("rJava")

#加载rJava
> library(rJava)
> .jinit()

#R调用Java变量测试
> s <- .jnew("java/lang/String", "Hello World!")
> s
[1] "Java-Object{Hello World!}"

启动Eclipse编写程序
rjava2


package org.conan.r.rjava;

import org.rosuda.JRI.Rengine;

public class DemoRJava {

    public static void main(String[] args) {
        DemoRJava demo = new DemoRJava();
        demo.callRJava();
    }

    public void callRJava() {
        Rengine re = new Rengine(new String[] { "--vanilla" }, false, null);
        if (!re.waitForR()) {
            System.out.println("Cannot load R");
            return;
        }
        
        //打印变量
        String version = re.eval("R.version.string").asString();
        System.out.println(version);

        //循环打印数组
        double[] arr = re.eval("rnorm(10)").asDoubleArray();
        for (double a : arr) {
            System.out.print(a + ",");
        }
        re.end();
    }
}

在Eclipse启动设置VM参数:

-Djava.library.path="C:\Program Files\R\R-3.0.1\library\rJava\jri\x64"

rjava

运行结果:


R version 3.0.1 (2013-05-16)
0.04051018703700011,-0.3321596519938258,0.45642459001166913,-1.1907153494936031,1.5872266854172385,1.3639721994863943,-0.6309712627586983,-1.5226698569087498,-1.0416402147174952,0.4864034017637044,

打包DemoRJava.jar
在Eclipse中完成打包,上传到linux环境,继续测试。

5. rJava(JRI)实现Java调用R (Ubuntu)

新建目录DemoRJava,上传DemoRJava.jar到DemoRJava


~ mkdir /home/conan/R/DemoRJava
~ cd /home/conan/R/DemoRJava
~ ls -l
-rw-r--r-- 1 conan conan 1328 Aug  8  2013 DemoRJava.jar

运行Jar包


~ export R_HOME=/usr/lib/R
~ java -Djava.library.path=/usr/local/lib/R/site-library/rJava/jri -cp /usr/local/lib/R/site-library/rJava/jri/JRI.jar:/home/conan/R/DemoRJava/DemoRJava.jar org.conan.r.rjava.DemoRJava

运行结果


R version 3.0.1 (2013-05-16)
0.6374494596732511,1.3413824702002808,0.04573045670001342,-0.6885617932810327,0.14970067632722675,-0.3989493870007832,-0.6148250252955993,0.40132038323714453,-0.5385260423222166,0.3459850956295771,

我们完成了,R和Java的互调。包括了R通过rJava调用Java,Java通过JRI调用R。并演示了win和linux中的使用方法。

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

打赏作者

Rserve与Java的跨平台通信

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-java/

rserve-java

前言

现在主流的异构跨平台通信组件Apache Thrift已经火遍大江南北,支持15种编程语言,但是到目前为止还没有加入R语言。要让R实现跨平台的通信,就只能从R的社区中找方案,像rJava,RCpp,rpy都是2种语言结合的方案,这些方案类似地会把R引擎加载到其他的语言内存环境。优点是高效,缺点是紧耦合,扩展受限,接口程序无法重用。

Rserve给了我们一种新的选择,抽象R语言网络接口,基于TCP/IP协议实现与多语言之间的通信。让我们体验一下Rserve与Java的跨平台通信。

目录

  1. Rserve介绍
  2. Rserve安装
  3. Java远程连接Rserve

1. Rserve介绍

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

2. Rserve安装

系统环境:
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。接下来,我们来简单地用一下。

Java远程连接Rserve

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/

  • REngine.jar
  • RserveEngine.jar

3). 创建Java工程
在Eclipse中新建Java工程,并加载JAR包环境中。
rserve1

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/

打赏作者

在巨人的肩膀前行 催化R包开发

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-package-faster/

r-devtools

前言

开发自己的R包sayHello 一文中,我们看到了如何从底层,按照R语言的标准构建一个R语言的扩展包。但实施过程确实复杂,会让很多的统计学工作者望而却步。如果能有一种比较简单方式,简化开发过程,那该多好啊!

我们是幸运的,Hadley Wickham(ggplot2,plyr的作者)已经帮我们梳理出了一套开发流程。站在巨人的肩膀上,看得更高,走得更远。

目录

  1. 开发流程介绍
  2. 编写功能代码
  3. 调试程序
  4. 单元测试
  5. 撰写文档
  6. 程序打包
  7. 程序发布

1. 开发流程介绍

站在巨人的肩膀,开发R包我们有3个武器:devtools, roxygen2, testthat

  • devtools:让开发变得简单,各种开发小工具的合集,非常实用。
  • roxygen2:通过注释的方式,生成文档,远离Latex的烦恼。
  • testthat:单元测试,让R包稳定、健壮,减少升级的痛苦。

标准化的开发流程:

  1. 编写功能代码
  2. 调试程序
  3. 单元测试
  4. 撰写文档
  5. 程序打包

2. 编写功能代码

1). 安装程序包:devtools, roxygen2, testthat


#依赖库
~ sudo apt-get install libcurl4-openssl-dev
~ sudo apt-get install libxml2-dev

#请使用root权限启动R
~ sudo R

> install.packages("devtools")
> install.packages("roxygen2")
> install.packages("testthat")

> library(devtools)
> library(roxygen2)
> library(testthat)

#查看
> search()
 [1] ".GlobalEnv"        "package:testthat"  "package:roxygen2"
 [4] "package:digest"    "package:stats"     "package:graphics"
 [7] "package:grDevices" "package:utils"     "package:datasets"
[10] "package:methods"   "Autoloads"         "package:base"

构建工程chinaWeather
创建骨架,代替package.skeleton()


> setwd("/home/conan/R")

> create("/home/conan/R/chinaWeather")
Creating package chinaWeather in /home/conan/R
No DESCRIPTION found. Creating default:

Package: chinaWeather
Title:
Description:
Version: 0.1
Authors@R: # getOptions('devtools.desc.author')
Depends: R (>= 3.0.1)
License: # getOptions('devtools.desc.license')
LazyData: true

> setwd("/home/conan/R/chinaWeather")
> dir()
[1] "DESCRIPTION" "man"         "R"

编辑DESCRIPTION文件

~ vi /home/conan/R/chinaWeather/DESCRIPTION
Package: chinaWeather
Type: Package
Title: a visualized package for china Weather
Description: a visualized package for china Weather
Author: Dan Zhang
Maintainer: Dan Zhang
Version: 0.1
Depends: R (>= 3.0.1)
License: GPL-2
LazyData: true
Date: 2013-08-05

增加函数文件chinaWeather.R


~ vi /home/conan/R/chinaWeather/R/chinaWeather.R

#define a filename from current date
filename<-function(date=Sys.time()){
  paste(format(date, "%Y%m%d"),".csv",sep="")
}

3. 调试程序

加载程序包到R中


> load_all("/home/conan/R/chinaWeather")
Loading chinaWeather

> filename
function(date=Sys.time()){
  paste(format(date, "%Y%m%d"),".csv",sep="")
}


> filename()
[1] "20130805.csv"

> day<-as.Date("20110701",format="%Y%m%d")
> filename(day)
[1] "20110701.csv"

4. 单元测试

编写单元测试代码


~ mkdir /home/conan/R/chinaWeather/inst/tests
~ vi /home/conan/R/chinaWeather/inst/tests/test.chinaWeather.R

library(testthat)
context("filename: current of date")

test_that("filename is current of date", {
  daystr<-paste(format(Sys.Date(), "%Y%m%d"),".csv",sep="")
  expect_that(filename(), equals(daystr))

  day<-as.Date("20110701",format="%Y%m%d")
  expect_that(filename(day), equals("20110701.csv"))
})

运行单元测试


#单个文件的单元测试
> source("/home/conan/R/chinaWeather/inst/tests/test.chinaWeather.R")
> test_file("/home/conan/R/chinaWeather/inst/tests/test.chinaWeather.R")
filename: current of date : ..

#对目录下所有文件的单元测试
> test_dir("/home/conan/R/chinaWeather/inst/tests/",reporter = "summary")
filename: current of date : ..

#自动单元测试
> src<-"/home/conan/R/chinaWeather/R/"
> test<-"/home/conan/R/chinaWeather/inst/tests/"
> auto_test(src,test)
filename: current of date : ..

#对package执行测试
> test("/home/conan/R/chinaWeather")
Testing chinaWeather
Loading chinaWeather
filename: current of date : ..

完成单元测试!下面开始撰写文档。

5. 撰写文档

这里撰写文档,使用源代码注释的方式,然后生成latex,再生成doc。比起原始的直接写latex要容易的多。

打开源代码文件:chinaWeather.R


~ vi /home/conan/R/chinaWeather/R/chinaWeather.R

#' Define a filename from current date.
#'
#' @param date input a date type
#' @return character a file name
#' @keywords filename 
#' @export
#' @examples
#' filename()
#' filename(as.Date("20110701",format="%Y%m%d"))
filename<-function(date=Sys.time()){
  paste(format(date, "%Y%m%d"),".csv",sep="")
}

生成latex文档


> library(roxygen2)
Loading required package: digest

> roxygenize("/home/conan/R/chinaWeather")
Updating collate directive in  /home/conan/R/chinaWeather/DESCRIPTION
Updating namespace directives
Writing chinaWeather.Rd
Writing filename.Rd

查看生成的latex文件


~ cat /home/conan/R/chinaWeather/man/filename.Rd

\name{filename}
\alias{filename}
\title{Define a filename from current date.}
\usage{
  filename(date = Sys.time())
}
\arguments{
  \item{date}{input a date type}
}
\value{
  character a file name
}
\description{
  Define a filename from current date.
}
\examples{
filename()
filename(as.Date("20110701",format="\%Y\%m\%d"))
}
\keyword{filename}

6. 程序打包

详细的打包解释,请参考:开发自己的R包sayHello

对上面程序过程,更简化操作可以用如下3条命令


> load_all("/home/conan/R/chinaWeather")
> test("/home/conan/R/chinaWeather")
> document("/home/conan/R/chinaWeather")

程序检查


> check("/home/conan/R/chinaWeather")
Updating chinaWeather documentation
Loading chinaWeather
'/usr/lib/R/bin/R' --vanilla CMD build '/home/conan/R/chinaWeather'  \
  --no-manual --no-resave-data

* checking for file '/home/conan/R/chinaWeather/DESCRIPTION' ... OK
* preparing 'chinaWeather':
* checking DESCRIPTION meta-information ... OK
* checking for LF line-endings in source and make files
* checking for empty or unneeded directories
* building 'chinaWeather_0.1.tar.gz'

'/usr/lib/R/bin/R' --vanilla CMD check  \
  '/tmp/RtmpM5NdJp/chinaWeather_0.1.tar.gz' --timings

* using log directory '/tmp/RtmpM5NdJp/chinaWeather.Rcheck'
* using R version 3.0.1 (2013-05-16)
* using platform: x86_64-pc-linux-gnu (64-bit)
* using session charset: ASCII
* checking for file 'chinaWeather/DESCRIPTION' ... OK
* checking extension type ... Package
* this is package 'chinaWeather' version '0.1'
* checking package namespace information ... OK
* checking package dependencies ... OK
* checking if this is a source package ... OK
* checking if there is a namespace ... OK
* checking for executable files ... OK
* checking for hidden files and directories ... OK
* checking for portable file names ... OK
* checking for sufficient/correct file permissions ... OK
* checking whether package 'chinaWeather' can be installed ... OK
* checking installed package size ... OK
* checking package directory ... OK
* checking DESCRIPTION meta-information ... OK
* checking top-level files ... OK
* checking for left-over files ... OK
* checking index information ... OK
* checking package subdirectories ... OK
* checking R files for non-ASCII characters ... OK
* checking R files for syntax errors ... OK
* checking whether the package can be loaded ... OK
* checking whether the package can be loaded with stated dependencies ... OK
* checking whether the package can be unloaded cleanly ... OK
* checking whether the namespace can be loaded with stated dependencies ... OK
* checking whether the namespace can be unloaded cleanly ... OK
* checking loading without being on the library search path ... OK
* checking for unstated dependencies in R code ... OK
* checking S3 generic/method consistency ... OK
* checking replacement functions ... OK
* checking foreign function calls ... OK
* checking R code for possible problems ... OK
* checking Rd files ... OK
* checking Rd metadata ... OK
* checking Rd cross-references ... OK
* checking for missing documentation entries ... OK
* checking for code/documentation mismatches ... OK
* checking Rd \usage sections ... OK
* checking Rd contents ... OK
* checking for unstated dependencies in examples ... OK
* checking examples ... OK
* checking PDF version of manual ... OK

Checking chinaWeather with devtools
Checking for any extra files in built .tar.gz file... OK

查检通过.
注:请安装

sudo apt-get install texlive-full

7. 程序发布

我们把写的程序发布到github上面,然后通过devtools,我可以方便的把程序从github下载安装。

在github创建一个新的资源库:chinaWeather
https://github.com/bsspirit/chinaWeather

提交本地代码到github


~ cd /home/conan/R/chinaWeather
~ git init
~ git add .
~ git commit -m 'init commit'
~ git remote add origin https://github.com/bsspirit/chinaWeather
~ git push -u origin master

To https://github.com/bsspirit/chinaWeather
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

通过devtools下载,并安装代码。
现在我的chinaWeather包,已经在github上面发布了,如果其他的同学想使用可以下面命令安装。


> library(devtools)
> install_github("chinaWeather","bsspirit")
Installing github repo(s) chinaWeather/master from bsspirit
Downloading chinaWeather.zip from https://github.com/bsspirit/chinaWeather/archive/master.zip
Installing package from /tmp/RtmpSaXYcA/chinaWeather.zip
Installing chinaWeather
'/usr/lib/R/bin/R' --vanilla CMD INSTALL  \
  '/tmp/RtmpSaXYcA/chinaWeather-master'  \
  --library='/home/conan/R/x86_64-pc-linux-gnu-library/3.0'  \
  --with-keep.source --install-tests

* installing *source* package 'chinaWeather' ...
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (chinaWeather)

#测试包
> library(chinaWeather)
> filename()
[1] "20130805.csv"

#查看文档
> ?filename
filename             package:chinaWeather              R Documentation
Define a filename from current date.
Description:
     Define a filename from current date.
Usage:
       filename(date = Sys.time())
Arguments:
    date: input a date type
Value:
     character a file name
Examples:
     filename()
     filename(as.Date("20110701",format="%Y%m%d"))

我们完成了,开发R包的全部流程。依赖于devtools, roxygen2, testthat三个包,真是事半功倍,比起完全手动操作提高效率了很多!!

希望更多的朋友,可以站在巨人的肩膀前行,创造让人惊叹的成果来!!

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

打赏作者

开发自己的R包sayHello

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-build-package/

r-package

前言
R是一个世界范围开发者共同协作的产物,至2013年2月共计近5000个包可在互联网上自由下载。现在作为R的使用者,有朝一日也可以成为R的开发者,把我们自己的知识做成R包分享给世界。

今天我们简单介绍如何开发自己R包。

目录

  1. 系统环境
  2. vi中制作R包
  3. RStudio中制作R包

1. 系统环境

操作系统:Linux Ubuntu 12.04.2 LTS 64bit
R语言版本:R 3.0.1


~ uname -a
Linux conan-deskop 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/.

2. vi中制作R包

创建目录:/home/conan/R/demo


~ mkdir /home/conan/R/demo
~ cd /home/conan/R/demo

新建R脚本sayHello.R


~ vi sayHello.R
sayHello<-function(name){
    print(paste("Hello",name))
}

自定义一个sayHello的函数,将作为自定义包的第一个函数。

启动R程序


~ R

#清空变量设置工作目录
rm(list=ls())
setwd("/home/conan/R/demo")

#通过sayHello的脚本生成开发包的骨架
package.skeleton(name="sayHello",code_files="/home/conan/R/demo/sayHello.R")
Creating directories ...
Creating DESCRIPTION ...
Creating NAMESPACE ...
Creating Read-and-delete-me ...
Copying code files ...
Making help files ...
Done.
Further steps are described in './sayHello/Read-and-delete-me'.

#退出R的客户端
q()

在当前目录生成sayHello目录


~ ls -l
drwxrwxr-x 4 conan conan 4096  8月  1 15:07 sayHello
-rw-rw-r-- 1 conan conan   59  8月  1 15:04 sayHello.R

~ ls -l sayHello
-rw-rw-r-- 1 conan conan  281  8月  1 15:07 DESCRIPTION
drwxrwxr-x 2 conan conan 4096  8月  1 15:07 man
-rw-rw-r-- 1 conan conan   31  8月  1 15:07 NAMESPACE
drwxrwxr-x 2 conan conan 4096  8月  1 15:07 R
-rw-rw-r-- 1 conan conan  420  8月  1 15:07 Read-and-delete-me

~ ls -l sayHello/man
-rw-rw-r-- 1 conan conan 1043  8月  1 15:07 sayHello-package.Rd
-rw-rw-r-- 1 conan conan 1278  8月  1 15:07 sayHello.Rd

~ ls -l sayHello/R
-rw-rw-r-- 1 conan conan 59  8月  1 15:07 sayHello.R

文件及目录解释:

  • DESCRIPTION文件: 包描述文件
  • NAMESPACE文件: 包的命名空间文件
  • Read-and-delete-me文件: 说明文件,可以删除
  • man目录: 存放函数的说明文件的目录
  • R目录:存放源文件的目录
  • man/sayHello.Rd: sayHello函数的说明文件,latex语法,用来生成PDF文档
  • man/sayHello-package.Rd: sayHello包的说明文件,可以删除

编辑DESCRIPTION文件:

~ vi sayHello/DESCRIPTION
Package: sayHello
Type: Package
Title: R package demo for sayHello
LazyLoad: yes
Author: Dan Zhang
Maintainer: Dan Zhang
Description: This package provides a package demo
License: GPL
Version: 1.0
Date: 2013-07-31
Depends: R (>= 3.0.1)

编辑NAMESPACE文件:

~ vi sayHello/NAMESPACE
exportPattern("^[[:alpha:]]+")

编辑sayHello.Rd文件:

~ vi sayHello/man/sayHello.Rd
\name{sayHello}
\alias{sayHello}
\title{a sayHello function demo}
\description{
a sayHello function demo
}
\usage{
sayHello(name)
}
\arguments{
  \item{name}{a word}
}
\details{
nothing
}
\value{
no return
}
\references{
nothing
}
\author{
Dan Zhang
}
\note{
nothing
}
\seealso{
nothing
}
\examples{
sayHello("world")
}
\keyword{ sayHello }

删除文件:


~ rm sayHello/Read-and-delete-me
~ rm sayHello/man/sayHello-package.Rd

打包sayHello


~ R CMD build sayHello
* checking for file ‘sayHello/DESCRIPTION’ ... OK
* preparing ‘sayHello’:
* checking DESCRIPTION meta-information ... OK
* checking for LF line-endings in source and make files
* checking for empty or unneeded directories
* building ‘sayHello_1.0.tar.gz’

~ ls -l
drwxrwxr-x 4 conan conan 4096  8月  1 15:22 sayHello
-rw-r--r-- 1 conan conan  663  8月  1 15:24 sayHello_1.0.tar.gz
-rw-rw-r-- 1 conan conan   59  8月  1 15:04 sayHello.R

本地安装sayHello


~ R CMD INSTALL sayHello_1.0.tar.gz
* installing to library ‘/home/conan/R/x86_64-pc-linux-gnu-library/3.0’
* installing *source* package ‘sayHello’ ...
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (sayHello)

~ ls /home/conan/R/x86_64-pc-linux-gnu-library/3.0
bitops  digest  manipulate  rstudio  sayHello  XML

sayHello包,已经在当前账号下进行安装

使用sayHello函数


~ R

library(sayHello)
sayHello("Conan")
[1] "Hello Conan"

#查看sayHello文档
?sayHello

sayHello               package:sayHello                R Documentation

a sayHello function demo

Description:
     a sayHello function demo
Usage:
     sayHello(name)
Arguments:
    name: a word
Details:
     nothing
Value:
     no return
Note:
     nothing
Author(s):
     Dan Zhang
References:
     nothing

我们包已经成功制作完成!并在本地进行安装和使用!!

检查R包
如果要提交R包和CRAN,必须要执行check检查。如果有任何的error和warning都将不被通过。

退出R的客户端,回到命令行

先安装latex的依赖包(500mb+)


~ sudo apt-get install texlive
~ sudo apt-get install texlive-xetex
~ sudo apt-get install texlive-latex-base
~ sudo apt-get install texlive-fonts-extra 
~ sudo apt-get install texlive-latex-recommended
~ sudo apt-get install texlive-fonts-recommended

执行check


~ R CMD check sayHello_1.0.tar.gz

* using log directory ‘/home/conan/R/demo/sayHello.Rcheck’
* using R version 3.0.1 (2013-05-16)
* using platform: x86_64-pc-linux-gnu (64-bit)
* using session charset: UTF-8
* checking for file ‘sayHello/DESCRIPTION’ ... OK
* checking extension type ... Package
* this is package ‘sayHello’ version ‘1.0’
* checking package namespace information ... OK
* checking package dependencies ... OK
* checking if this is a source package ... OK
* checking if there is a namespace ... OK
* checking for executable files ... OK
* checking for hidden files and directories ... OK
* checking for portable file names ... OK
* checking for sufficient/correct file permissions ... OK
* checking whether package ‘sayHello’ can be installed ... OK
* checking installed package size ... OK
* checking package directory ... OK
* checking DESCRIPTION meta-information ... OK
* checking top-level files ... OK
* checking for left-over files ... OK
* checking index information ... OK
* checking package subdirectories ... OK
* checking R files for non-ASCII characters ... OK
* checking R files for syntax errors ... OK
* checking whether the package can be loaded ... OK
* checking whether the package can be loaded with stated dependencies ... OK
* checking whether the package can be unloaded cleanly ... OK
* checking whether the namespace can be loaded with stated dependencies ... OK
* checking whether the namespace can be unloaded cleanly ... OK
* checking loading without being on the library search path ... OK
* checking for unstated dependencies in R code ... OK
* checking S3 generic/method consistency ... OK
* checking replacement functions ... OK
* checking foreign function calls ... OK
* checking R code for possible problems ... OK
* checking Rd files ... OK
* checking Rd metadata ... OK
* checking Rd cross-references ... OK
* checking for missing documentation entries ... OK
* checking for code/documentation mismatches ... OK
* checking Rd \usage sections ... OK
* checking Rd contents ... OK
* checking for unstated dependencies in examples ... OK
* checking examples ... OK
* checking PDF version of manual ... WARNING
LaTeX errors when creating PDF version.
This typically indicates Rd problems.
* checking PDF version of manual without hyperrefs or index ... OK

WARNING: There was 1 warning.
See
  ‘/home/conan/R/demo/sayHello.Rcheck/00check.log’
for details.

发现有一个警告,checking PDF version of manual ... WARNING。是latex生成pdf错误。

查检警告的debug日志


~ R CMD Rd2pdf sayHello_1.0.tar.gz
Converting Rd files to LaTeX ...
  sayHello_1.0.tar.gz
Warning in readLines(f) :
  incomplete final line found on 'sayHello_1.0.tar.gz'
Warning in parse_Rd("sayHello_1.0.tar.gz", encoding = "unknown", fragment = FALSE,  :
  sayHello_1.0.tar.gz:16: unexpected '}'
Warning: sayHello_1.0.tar.gz:1: All text must be in a section
Warning: sayHello_1.0.tar.gz:2: All text must be in a section
Warning: sayHello_1.0.tar.gz:3: All text must be in a section
Warning: sayHello_1.0.tar.gz:4: All text must be in a section
Warning: sayHello_1.0.tar.gz:5: All text must be in a section
Warning: sayHello_1.0.tar.gz:6: All text must be in a section
Warning: sayHello_1.0.tar.gz:7: All text must be in a section
Warning: sayHello_1.0.tar.gz:8: All text must be in a section
Warning: sayHello_1.0.tar.gz:9: All text must be in a section
Warning: sayHello_1.0.tar.gz:10: All text must be in a section
Warning: sayHello_1.0.tar.gz:11: All text must be in a section
Warning: sayHello_1.0.tar.gz:12: All text must be in a section
Warning: sayHello_1.0.tar.gz:15: All text must be in a section
Error : sayHello_1.0.tar.gz: Sections \title, and \name must exist and be unique in Rd files

求助:
Error : sayHello_1.0.tar.gz: Sections \title, and \name must exist and be unique in Rd files
这个问题我检查了好多次始终没有解决。请求帮助!!

问题解决:安装完整包

sudo apt-get install texlive-full

上传R包
当解决了上面的WARN就可以上传了。。。(待续)

卸载R包


remove.packages("sayHello")
Removing package from ‘/home/conan/R/x86_64-pc-linux-gnu-library/3.0’

~ ls /home/conan/R/x86_64-pc-linux-gnu-library/3.0
bitops  digest  manipulate  rstudio  XML

3. RStudio中制作R包

RStudio Server的安装及配置,请参考:多人在线协作R开发RStudio Server

RStudio已经为我们准备了一个有界面的环境,方便我们制作R包。

  1. 创建一个工程sayHello
  2. 编辑DESCRIPTION
  3. 创建sayHello.R的脚本
  4. 编辑sayHello.Rd的文档

1). 创建一个工程sayHello
project1

project2

2). 编辑rstudio/sayHello/DESCRIPTION

Package: sayHello
Type: Package
Title: R package demo for sayHell
LazyLoad: yes
Author: Dan Zhang
Maintainer: Dan Zhang
Description: This package provides a package demo
License: GPL
Version: 1.0
Date: 2013-07-31
Depends: R (>= 3.0.1)

3). 编辑rstudio/sayHello/R/sayHello.R的脚本

sayHello<-function(name){
  print(paste("Hi",name))
}

4). 编辑rstudio/sayHello/man/sayHello.Rd的文档

\name{sayHello}
\alias{sayHello}
\title{a sayHello function demo}
\description{
a sayHello function demo
}
\usage{
sayHello(name)
}
\arguments{
  \item{name}{a word}
}
\details{
nothing
}
\value{
no return
}
\references{
nothing
}
\author{
Dan Zhang
}
\note{
nothing
}
\seealso{
nothing
}
\examples{
sayHello("world")
}
\keyword{ sayHello }

5). 执行build和reload
project3

project4

6). 执行check
project5

在RStudio中可以更方便的进行R的开包,打包,检查等的过程。RStudio就是R的神器!

虽然还没有发布自己的R包,不过很快了!!有此想法已经多时了!!每一天加油!

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

打赏作者

多人在线协作R开发RStudio Server

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-rstudio-server/

rstudio-title

前言

RStudio是R语言开发中的利器,是一个IDE集成环境。RStudio Server版本提供了web的功能,可以安装远程计算机,通过web进行访问,支持多用户。如此神器,快来动手试一下吧。

目录

  • 系统环境
  • RStudio Server安装
  • RStudio Server使用
  • RStudio Server多人协作

1. 系统环境

操作系统: Linux Ubuntu 64bit 12.04.2 LTS
R语言: 3.0.1

~ uname -a
Linux conan-deskop 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

RStudio Server官方介绍:http://www.rstudio.com/ide/download/server

安装R语言


~ sudo apt-get install r-base

#R版本 3.0.1
~ 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/.

2. RStudio Server安装

下载并安装


~ sudo apt-get install gdebi-core
~ sudo apt-get install libapparmor1  # Required only for Ubuntu, not Debian
~ wget http://download2.rstudio.org/rstudio-server-0.97.551-amd64.deb
~ sudo gdebi rstudio-server-0.97.551-amd64.deb

查看rstudio-server


~ ps -aux|grep rstudio-server
998       2914  0.0  0.1 192884  2568 ?        Ssl  10:40   0:00 /usr/lib/rstudio-server/bin/rserver

rstudio-server的服务已启动, 8787端口被打开。

3. RStudio Server使用

通过浏览器,我们访问RStudio Server: http://192.168.1.13:8787

rstudio-login

通过Linux系统用户登陆: conan:conan111

rstudio-web

太帅了是不是,web版的RStudio!!

系统配置和系统管理

其他操作和单机版的RStudio一样。

4. RStudio Server多人协作

1). 增加新用户


~ sudo groupadd hadoop 
~ sudo useradd hadoop -g hadoop;
~ sudo passwd hadoop 
~ sudo adduser hadoop sudo
~ sudo mkdir /home/hadoop 
~ sudo chown -R hadoop:hadoop /home/hadoop

#测试账号设置成功
~ ssh hadoop@localhost
~ bash
~ pwd
/home/hadoop

新打开浏览器窗口通过 hadoop账号登陆
rstudio-web2

2). Git代码共享
安装git

sudo apt-get install git

#生成rsa密钥对
ssh-keygen -t rsa

#查看公钥并
cat /home/conan/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDMmnFyZe2RHpXaGmENdH9kSyDyVzRas4GtRwMNx+qQ4QsB8xVTrIbFayG2ilt+P8UUkVYO0qtUJIaLRjGy/SvQzzL7JKX12+VyYoKTfKvZZnANJ414d6oZpbDwsC0Z7JARcWsFyTW1KxOMyesmzNNdB+F3bYN9sYNiTkOeVNVYmEQ8aXywn4kcljBhVpT8PbuHl5eadSLt5zpN6bcX7tlquuTlRpLi1e4K+8jQo67H54FuDyrPLUYtVaiTNT/xWN6IU+DQ9CbfykJ0hrfDU1d1LiLQ4K2Fdg+vcKtB7Wxez2wKjsxb4Cb8TLSbXdIKEwSOFooINw25g/Aamv/nVvW1 conan@conan-deskop

把公钥并上传到github
创建一个新的Project: rstudio-demo
https://github.com/bsspirit/rstudio-demo

绑定本定项目和rstudio-demo


~ cd /home/conan/R/github
~ git init
~ git add .
~ git commit -m 'first comment'
~ git remote add origin git@github.com:bsspirit/rstudio-demo.git
~ git push -u origin master

打开RStudio设置到/home/conan/R/github目录,tools–>version control –> project setup
rstudio-git

在RStudio中修改sayHello.r的代码


sayHello<-function(name){
  print(paste("hello",name))
}

sayHello("Conan")
sayHello("World")

提交:点击tools–>version control–> commit
rstudio-git2

上传到github:点击tools–>version control–> push
rstudio-git3

RStudio有如此强大的功能,极大的降低了编程的门槛。还没有用过的同学,赶紧去体验一把吧。极客的感觉!

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

打赏作者