Rパッケージを作ってCRANで公開する
目的
せっかくRで汎用的な関数を作ったので、他の人にも使ってもらいたい。CRAN(The Comprehensive R Archive Network)には、2013/2/16現在4338個ものRパッケージが公開されている。CRANにアップロードすると、 install.package('パッケージ名') で気軽にインストールできるので便利だ。そこで、パッケージを作成してCRANにアップロードする。
ちなみに、ドキュメント間の類似度を算出する関数をパッケージ化した。作成したパッケージは、以下から閲覧・ダウンロードすることができる。
http://cran.r-project.org/web/packages/smdc/index.html
方法
1. スケルトンの生成
Rのコマンドラインから、パッケージのスケルトンを生成する。
> # 関数が定義されていることを確認する。定義されていなければ、source("xxx.R")などで読み込む。 > ls() [1] "conv2Freq" "normalize" "simDic" "simDoc" "simSum" "simSyn" "uniform" > # スケルトンを生成する。 > package.skeleton(list=c("simDoc","simDic","simSyn","simSum","uniform","normalize","conv2Freq"),name="smdc") Creating directories ... Creating DESCRIPTION ... Creating NAMESPACE ... Creating Read-and-delete-me ... Saving functions and data ... Making help files ... Done. Further steps are described in './smdc/Read-and-delete-me'.
生成されるスケルトンのディレクトリ構造
smdc/ |--DESCRIPTION |--NAMESPACE |--Read-and-delete-me |--man/ | |--conv2Freq.Rd | | … |--R/ | |--conv2Freq.R | | …
2. ドキュメントの修正
生成されたパッケージ内のドキュメントを修正する。ドキュメントを日本語で書くとCRANのチェックテストが通らないので注意。
- DESCRIPTIONファイルを編集する。
Package: smdc
Type: Package
Title: Document Similarity
Version: 0.0.2
Date: 2013-02-16
Author: Masaaki TAKADA
Maintainer: Masaaki TAKADA
Description: This package provides similarity among documents.
License: BSD
Depends: proxy,tm
- NAMESPACEで外部に公開するAPIを定義する。
exportPattern(simDoc,simDic,simSum,simSyn)
- manフォルダ内のすべての.Rdファイルを編集する。\title{}は必須項目。不要な
(書くのが面倒な)項目は削除。
\name{smdc-package}
\alias{smdc-package}
\alias{smdc}
\docType{package}
\title{
Document Similarity
}
\description{
This package provide functions that calculate similarity among documents.
}
\details{
\tabular{ll}{
Package: \tab smdc\cr
Type: \tab Package\cr
Version: \tab 0.0.2\cr
Date: \tab 2013-02-16\cr
License: \tab BSD\cr
}
}
\author{
Masaaki TAKADAMaintainer: Masaaki TAKADA
}
\keyword{ package }
\examples{# Load text mining package 'tm' for English.
# 'RMeCab' is available for Japanese.
# install.packages('tm')
library('tm')# Read corpus data.
crudeDir <- system.file("texts", "crude", package = "tm")
crude <- Corpus(DirSource(crudeDir))
docMatrix1 <- t(as.matrix(DocumentTermMatrix(crude)))
acqDir <- system.file("texts", "acq", package = "tm")
acq <- Corpus(DirSource(acqDir))
docMatrix2 <- t(as.matrix(DocumentTermMatrix(acq)))# Create score dictionary.
words <- unique(c(rownames(docMatrix1),rownames(docMatrix2)))
scores <- runif(length(words),-1,1)
dict <- data.frame(word=words,score=scores)# Calculate similarity.
sim1 <- simDoc(docMatrix1,docMatrix2,norm=TRUE)
sim2 <- simDic(docMatrix1,docMatrix2,dict,norm=TRUE)
sim <- simSyn(list(sim1,sim2),c(0.5,0.5))
simSum(sim)}
- Read-and-delete-meファイルを読んで削除する。
3. ビルドとチェック
Windowsのコマンドプロンプトでビルドとチェックを行う。WARNINGは1つ残らず消えるまでドキュメントとコードの修正をする。
> :: パッケージのビルド >R CMD build smdc * checking for file 'smdc/DESCRIPTION' ... OK * preparing 'smdc': * checking DESCRIPTION meta-information ... OK * checking for LF line-endings in source and make files * checking for empty or unneeded directories * building 'smdc_0.0.2.tar.gz' cygwin warning: MS-DOS style path detected: C:/LyricsWorkspace/R/smdc_0 .0.2.tar.gz Preferred POSIX equivalent is: /cygdrive/c/LyricsWorksp ace/R/smdc_0.0.2.tar.gz CYGWIN environment variable option "nodosfilewarning" turns off this warning. Consult the user's guide for more details about POSIX paths: http://cygwin.com/cygwin-ug-net/using.html#using-pathnames > :: パッケージのチェック > R CMD check --as-cran smdc_0.0.2.tar.gz * using log directory 'C:/LyricsWorkspace/R/smdc.Rcheck' * using R version 2.15.2 (2012-10-26) * using platform: x86_64-w64-mingw32 (64-bit) * using session charset: CP932 * checking for file 'smdc/DESCRIPTION' ... OK * checking extension type ... Package * this is package 'smdc' version '0.0.2' * checking CRAN incoming feasibility ... NOTE Maintainer: 'Masaaki TAKADA' Days since last update: 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 whether package 'smdc' can be installed ... OK * checking installed package size ... OK * checking package directory ... OK * checking for portable file names ... 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 NOTE: There was 1 note. See 'C:/LyricsWorkspace/R/smdc.Rcheck/00check.log' for details.
4. CRANにアップロード
ビルドしたパッケージ(xxx.tar.gz)をCRANにアップロードする。
- ftp://CRAN.R-project.org/incoming/ にIEでアクセス。
- メニューの"表示">"エクスプローラーでFTPサイトを開く"を選択。
- パッケージをコピペ。
5. メールで報告
アップロードしたことをCRAN@R-project.orgにメールする。メールのタイトルは"CRAN submission 'パッケージ名' 'バージョン'"とする。
Title: CRAN submission smdc 0.0.2
Dear Sir/Madam
Hi, I'm Masaaki TAKADA.
I submitted 'smdc' package ver 0.0.2.I agree to the CRAN policies.
Sincerely,
Masaaki TAKADA
6. その後
10~20分くらいでメールの返信が来る。しかも人手で。すごい!チェックテストで引っかかるとその旨が記載される。受理されると、ここに掲載される。install.package()で使えるようになるにはもう1日くらいかかるので、ゆっくり待ちましょう。
その他注意事項
- 当初RMeCabをパッケージ内で動かすプログラムを書いていましたが、RMeCabがCRANに載っていないため、rejectされました。RMeCabのインストール方法など詳しくDESCRIPTIONに書いてみましたが、認められず。CRAN以外のパッケージと強く依存するようなパッケージは、CRANに掲載できないようです。その結果、英語のテキストマイニングパッケージであるtmを勉強する羽目に…。
- パッケージのmanでexampleを書く際に、使用するデータは誰でもアクセスできる必要があり、何を使ってどう読み込めばよいのか苦労しました。結局他のパッケージに含まれているニュースサイトのデータを使いました。
- ドキュメントに日本語はご法度です。たとえ日本語解析のパッケージであってもNGです。
- パッケージをCRANに投稿してから日が経たないうちにアップデートしようとすると怒られます。何やら投稿に伴うテストや掲載などの実作業はボランティアなので、頻繁にアップデートされると手におえないとのこと。"1~2か月ごとより頻繁にならないように"アップデートするのが目安のようです。
補足
パッケージを作成してCRANにあげる人は、以下の公式ページを熟読しましょう。