//青木繁伸 2006/06/06
COLOR(red){SIZE(30){パッケージを作る}}

[[Rから他言語利用]]を参考に

まずは,Mac OS X で C 言語で書いた関数を呼び出すようなパッケージを作るときの覚え書き

-C の関数を R で認識できるようにするための方法が今ひとつわからない。あれこれ試行錯誤の末,3. の R プログラムの最初に,library.dynam("パッケージ名") のようにすれば,とりあえずは動くようになったのだが,Writing R extensions に書いてあることが理解できなかった。 --  &new{2006-06-06 (火) 15:41:21};
- ソースパッケージはR CMD BUILD パッケージ名 でできますが,バイナリパッケージ(Windowsなら.zip形式)を作成するには,R CMD BUILD --binary パッケージ名とすることでできました.ちょっと迷ったので書き込んでおきます. --  &new{2007-07-23 (月) 13:45:14};
- 最近のWindows上でのビルド環境整備については[[Building R for Windows>http://www.murdoch-sutherland.com/Rtools/]]をみればOkです --  &new{2007-07-23 (月) 16:44:40};

#comment

----

#contents
----

*0. R をコンパイルできるような環境を整える [#q0e3909d]

だいぶ前に,何段階かであれこれやったので,もう忘れた

*1. 作業ディレクトリを作る [#x4becdd1]
名前は何でもよいが,説明の都合上 ~/Desktop/r-working-dir/ とする

*2. R と C のプログラムの仕様を決める [#zed6a89f]

- ユリウス日を求める関数 jday の引数は,年,月,日の3つ
- ユリウス日から年月日を求める関数 yadj の引数はユリウス日のみ

*3. R プログラムを書く [#i7fe4878]

**3.1 jday.R の中身 [#c926c595]

- library.dynam の引数は,パッケージ名
- .C 関数で,C で書かれたプログラムを呼び出す
- 第1引数は C プログラムの関数名(R の関数名と同じでよい)
- 第2,3,4引数は,C の関数へ渡す引数。受け取り側のタイプに合わせて,as.integer, as.double で変換して引き渡す
- 第5引数は C プログラムの関数の中で戻り値をセットするもの
- R の関数の戻り値として戻すために foo= と名前を付けて,後に続く $foo で取り出す
PACKAGE 引数はおまじない。"nissuu" はパッケージ名

 "jday" <-
 function(year, month, day)
 {
 	library.dynam("nissuu")
 	if (year <= 1752 && month <= 9 && day < 14) {
 		warning("1752/09/14 以降で定義しています")
 		return(-999999999)
 	}
 	.C("jday", as.integer(year), as.integer(month), as.integer(day), retv=as.integer(0), PACKAGE="nissuu")$retv
 }

**3.2 yadj.R の中身 [#c9c991fe]

- jday と同じだが,C プログラムの関数の複数の戻り値を書式化して R 関数の戻り値にするために .C 関数の結果を変数に付値する
- ans$year, ans$month, ans$day のように取り出し,sprintf で一つの文字列に合成する

 "yadj" <-
 function(jd)
 {
 	library.dynam("nissuu")
 	if (jd < 2361222) jd <- jd-11
 	ans <- .C("yadj", as.integer(jd), year=as.integer(0), month=as.integer(0), day=as.integer(0), PACKAGE="nissuu")
 	sprintf("%4i/%02i/%02i", ans$year, ans$month, ans$day)
 }

*4. 必要なディレクトリとファイルに,ひな形を作る [#t7094eac]

**4.1 R プログラムをコンソールで読み込んでおく [#xc61b2e9]

**4.2 package.skeleton 関数を起動する [#a7b40aef]

- 第1引数は,パッケージ名 
- 必要な R 関数を第2引数に列挙する
- path は 1 で作ったディレクトリの名前

 > package.skeleton("nissuu", c("jday", "yadj"), path="~/Desktop/r-working-dir")
 Creating directories ...
 Creating DESCRIPTION ...
 Creating Read-and-delete-mes ...
 Saving functions and data ...
 Making help files ...
 Created file named '~/Desktop/r-working-dir/nissuu/man/nissuu.package.Rd'.
 Edit the file and move it to the appropriate directory.
 Created file named '~/Desktop/r-working-dir/nissuu/man/jday.Rd'.
 Edit the file and move it to the appropriate directory.
 Created file named '~/Desktop/r-working-dir/nissuu/man/yadj.Rd'.
 Edit the file and move it to the appropriate directory.
 Done.
 Further steps are described in ~/Desktop/r-working-dir/nissuu/Read-and-delete-me 

#ref(dir.png)

**4.3 ファイルのひな形を開き,記述を完成させる [#w77bc179]

***DESCRIPTION [#p37dc4c7]

 Package: nissuu
 Type: Package
 Title: conversion between Julian day and Date
 Version: 1.0
 Date: 2006-06-02
 Author: Shigenobu AOKI
 Maintainer: Shigenobu AOKI <aoki@si.gunma-u.ac.jp>
 Description: This package contains two functions, 'jday' and 'yadj'. The funciton 'jday', which has three arguments, year, month, day, computes Julian day. The function 'yadj' is an inverse function of jday with one argument, i.e. Julian day, and returns year, month and day in a formatted string "yyyy/mm/dd".
 License: GPL (version 2 or lator)

***man ディレクトリにあるファイル [#y6932bda]

- パッケージの説明 パッケージ名.package.Rd

 \name{nissuu-package}
 \alias{nissuu}
 \docType{package}
 \title{
 	conversion between Julian day and Date
 }
 \description{
 	conversion between Julian day and Date
 }
 \details{
 	\tabular{ll}{
 		Package: \tab nissuu\cr
 		Type: \tab Package\cr
 		Version: \tab 1.0\cr
 		Date: \tab 2006-06-02\cr
 		License: \tab GPL (version 2 or lator)\cr
 	}
 	conversion between Julian day and Date
 }
 \author{
 	Shigenobu AOKI
 	Maintainer: Shigenobu AOKI <aoki@si.gunma-u.ac.jp>
 }
 \references{
 	http://aoki2.si.gunma-u.ac.jp/R/date.html
 }
 \keyword{package}
 \examples{
 	jday(1752, 9, 14) # returns 2361222
 	yadj(2361222)     # returns "1752/09/14"
 	jday(2006, 6, 6)  # returns 2453893
 	yadj(2453893)     # returns "2006/06/06"
 }

- R 関数ごとの .Rd ファイル

関連する関数は alias キーワードでまとめて,一つの .Rd ファイルで説明することができる

 \name{jday}
 \alias{jday}
 \alias{yadj}
 \title{
 	conversion between Julian day and Date
 }
 \description{
   conversion between Julian day and Date
 }
 \usage{
 	jday(year, month, day)
 	yadj(jd)
 }
 \arguments{
 	\item{year}{year(4 digits)}
 	\item{month}{month}
 	\item{day}{day}
 	\item{jd}{Julian day}
 }
 \details{
 	Conversion between Julian day and Date.
 	jday calculates Julian day, yadj is inverse function of jday.
 }
 \value{
 	jday returns Julian day.
 	yadj reuturns year, month, day as string "yyyy/mm/dd".
 }
 \references{
 	http://aoki2.si.gunma-u.ac.jp/R/date.html
 }
 \author{
 	Shigenobu AOKI
 }
 \examples{
 	jday(1752, 9, 14) # returns 2361222
 	yadj(2361222)     # returns "1752/09/14"
 	jday(2006,6,6)    # returns 2453893
 	yadj(2453893)     # returns "2006/06/06"
 }
 \keyword{}

*5. C プログラムを書く [#m6fa2d9c]

- ソースプログラムは src ディレクトリに置く
- 関数の型は void とする
- 引数は全て pointer 型とする
- ダミーの main プログラムを書いて,コンパイルと動作の確認をしておくとよい

 /*
 # 西暦年月日からユリウス日を得る
 # y 年 m 月 d 日
 */
 void jday(int *y, int *m, int *d, int *retv)
 {
 	int iy, jm, kd, tmp;
 	iy = *y;
 	jm = *m;
 	kd = *d;
 	tmp = -(jm < 3);
 	*retv = kd-32075+(1461*(iy+4800+tmp))/4+(367*(jm-2-tmp*12))/12-(3*((iy+4900+tmp)/100))/4;
 }
 /*
 # ユリウス日から西暦年月日を得る
 */
 void yadj(int *jd, int *y, int *m, int *d)
 {
 	int iy, jm, l, n;
 	l = *jd+68569;
	n = (4*l)/146097;
  	l = l-(146097*n+3)/4;
 	iy = (4000*(l+1))/1461001;
 	l = l-(1461*iy)/4+31;
 	jm = (80*l)/2447;
 	*d = l-(2447*jm)/80;
 	l = jm/11;
 	*m = jm+2-12*l;
 	*y = 100*(n-49)+iy+l;
 }

*6. パッケージのチェックを行う R CMD check パッケージ名 [#p4954059]

以下のように,全て OK になるように努力する

 $ R CMD check nissuu --no-latex
 * using log directory '/Users/aoki/Desktop/r-working-dir/nissuu.Rcheck'
 * using Version 2.3.1 (2006-06-01)
 * checking for file 'nissuu/DESCRIPTION' ... OK
 * checking extension type ... Package
 * this is package 'nissuu' version '1.0'
 * checking package dependencies ... OK
 * checking if this is a source package ... OK
 * checking whether package 'nissuu' can be installed ... OK
 * checking package directory ... OK
 * checking for portable file names ... OK
 * checking for sufficient/correct file permissions ... OK
 * checking DESCRIPTION meta-information ... OK
 * checking top-level files ... OK
 * checking index information ... OK
 * checking package subdirectories ... OK
 * checking R files for syntax errors ... OK
 * checking R files for library.dynam ... OK
 * checking S3 generic/method consistency ... OK
 * checking replacement functions ... OK
 * checking foreign function calls ... OK
 * checking Rd files ... OK
 * checking for missing documentation entries ... OK
 * checking for code/documentation mismatches ... OK
 * checking Rd \usage sections ... OK
 * checking for CRLF line endings in C/C++/Fortran sources/headers ... OK
 * creating nissuu-Ex.R ... OK
 * checking examples ... OK


*7. パッケージを作成する R CMD build パッケージ名 [#ief114b5]

 $ R CMD build NAME --no-vignettes --force
 * checking for file 'nissuu/DESCRIPTION' ... OK
 * preparing 'nissuu':
 * checking DESCRIPTION meta-information ... OK
 * cleaning src
 * removing junk files
 * checking for LF line-endings in source files
 * checking for empty or unneeded directories
 * building 'nissuu_1.0.tar.gz'

nissuu_1.0.tar.gz が最終目的のファイル

*8. インストールする [#y40892c6]

「パッケージとデータ」−>「パッケージインストーラ」−>「このコンピュータ上のソースパッケージ」−>「Install」で,7. で作ったファイル nissuu_1.0.tar.gz を選択するとインストールされる。

*9. 利用 [#qe29747b]

- library(パッケージ名) でロード
- library(help=パッケージ名) でパッケージの説明
- example(パッケージ名) で例題実行
 > example(nissuu)
 
 nissuu> jday(1752, 9, 14)
 [1] 2361222
 
 nissuu> yadj(2361222)
 [1] "1752/09/14"
 
 nissuu> jday(2006, 6, 6)
 [1] 2453893
 
 nissuu> yadj(2453893)
 [1] "2006/06/06"

- 関数名(引数) により計算
 > jday(1950,6,29)
 [1] 2433462
 > jday(2006,6,29)-jday(1950,6,29)
 [1] 20454
 > yadj(jday(1950,6,29))
 [1] "1950/06/29"

*Appendix: R の関数だけから成り立つパッケージの作成法 [#o0374693]

-上記の 0 は不要
-上記の 2 では,当然 R の関数だけ書けばよい
-上記の 4 で作られる src ディレクトリはゴミ箱へ捨てる
-上記の 5 は不要

そのほかは,上に準じて行えばよい

* 関連ページ [#z125af65]
- http://www.okada.jp.org/RWiki/index.php?cmd=read&page=%BB%E4%C5%AA%A5%D1%A5%C3%A5%B1%A1%BC%A5%B8%BA%EE%C0%AE%CB%A1&word=build%20%A5%D1%A5%C3%A5%B1%A1%BC%A5%B8

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS