開發本身的R包sayHello

R語言做爲統計學一門語言,一直在小衆領域閃耀着光芒。直到大數據的爆發,R語言變成了一門煊赫一時的數據分析的利器。隨着愈來愈多的工程背景的人的加入,R語言的社區在迅速擴大成長。如今已不單單是統計領域,教育,銀行,電商,互聯網….都在使用R語言。 linux

要成爲有理想的極客,咱們不能停留在語法上,要掌握牢固的數學,機率,統計知識,同時還要有創新精神,把R語言發揮到各個領域。讓咱們一塊兒動起來吧,開始R的極客理想。 程序員

關於做者: redis

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

轉載請註明出處:
http://blog.fens.me/r-build-package/ session

r-package

前言
R是一個世界範圍開發者共同協做的產物,至2013年2月共計近5000個包可在互聯網上自由下載。如今做爲R的使用者,有朝一日也能夠成爲R的開發者,把咱們本身的知識作成R包分享給世界。 ide

今天咱們簡單介紹如何開發本身R包。 函數

目錄 大數據

  1. 系統環境
  2. vi中製做R包
  3. RStudio中製做R包

1. 系統環境

操做系統:Linux Ubuntu 12.04.2 LTS 64bit
R語言版本:R 3.0.1 ui

~ 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 this

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

新建R腳本sayHello.R spa

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

相關文章
相關標籤/搜索