パッケージを作る
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
//青木繁伸 2006/06/06
COLOR(red){SIZE(30){パッケージを作る}}
[[Rから他言語利用]]を参考に
まずは,Mac OS X で C 言語で書いた関数を呼び出すようなパ...
-C の関数を R で認識できるようにするための方法が今ひとつ...
- ソースパッケージはR CMD BUILD パッケージ名 でできますが...
- 最近のWindows上でのビルド環境整備については[[Building R...
#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 の関数へ渡す引数。受け取り側のタイプに...
- 第5引数は C プログラムの関数の中で戻り値をセットするもの
- R の関数の戻り値として戻すために 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.inte...
}
**3.2 yadj.R の中身 [#c9c991fe]
- jday と同じだが,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), m...
sprintf("%4i/%02i/%02i", ans$year, ans$month, ans$day)
}
*4. 必要なディレクトリとファイルに,ひな形を作る [#t7094e...
**4.1 R プログラムをコンソールで読み込んでおく [#xc61b2e9]
**4.2 package.skeleton 関数を起動する [#a7b40aef]
- 第1引数は,パッケージ名
- 必要な R 関数を第2引数に列挙する
- path は 1 で作ったディレクトリの名前
> package.skeleton("nissuu", c("jday", "yadj"), path="~/...
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/n...
Edit the file and move it to the appropriate directory.
Created file named '~/Desktop/r-working-dir/nissuu/man/j...
Edit the file and move it to the appropriate directory.
Created file named '~/Desktop/r-working-dir/nissuu/man/y...
Edit the file and move it to the appropriate directory.
Done.
Further steps are described in ~/Desktop/r-working-dir/n...
#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'...
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...
}
\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*...
}
/*
# ユリウス日から西暦年月日を得る
*/
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 パッケージ名 [...
以下のように,全て OK になるように努力する
$ R CMD check nissuu --no-latex
* using log directory '/Users/aoki/Desktop/r-working-dir...
* 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 ......
* 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 source...
* creating nissuu-Ex.R ... OK
* checking examples ... OK
*7. パッケージを作成する R CMD build パッケージ名 [#ief1...
$ 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]
「パッケージとデータ」−>「パッケージインストーラ」−>「...
*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 の関数だけから成り立つパッケージの作成法 [#o...
-上記の 0 は不要
-上記の 2 では,当然 R の関数だけ書けばよい
-上記の 4 で作られる src ディレクトリはゴミ箱へ捨てる
-上記の 5 は不要
そのほかは,上に準じて行えばよい
* 関連ページ [#z125af65]
- http://www.okada.jp.org/RWiki/index.php?cmd=read&page=%...
終了行:
//青木繁伸 2006/06/06
COLOR(red){SIZE(30){パッケージを作る}}
[[Rから他言語利用]]を参考に
まずは,Mac OS X で C 言語で書いた関数を呼び出すようなパ...
-C の関数を R で認識できるようにするための方法が今ひとつ...
- ソースパッケージはR CMD BUILD パッケージ名 でできますが...
- 最近のWindows上でのビルド環境整備については[[Building R...
#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 の関数へ渡す引数。受け取り側のタイプに...
- 第5引数は C プログラムの関数の中で戻り値をセットするもの
- R の関数の戻り値として戻すために 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.inte...
}
**3.2 yadj.R の中身 [#c9c991fe]
- jday と同じだが,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), m...
sprintf("%4i/%02i/%02i", ans$year, ans$month, ans$day)
}
*4. 必要なディレクトリとファイルに,ひな形を作る [#t7094e...
**4.1 R プログラムをコンソールで読み込んでおく [#xc61b2e9]
**4.2 package.skeleton 関数を起動する [#a7b40aef]
- 第1引数は,パッケージ名
- 必要な R 関数を第2引数に列挙する
- path は 1 で作ったディレクトリの名前
> package.skeleton("nissuu", c("jday", "yadj"), path="~/...
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/n...
Edit the file and move it to the appropriate directory.
Created file named '~/Desktop/r-working-dir/nissuu/man/j...
Edit the file and move it to the appropriate directory.
Created file named '~/Desktop/r-working-dir/nissuu/man/y...
Edit the file and move it to the appropriate directory.
Done.
Further steps are described in ~/Desktop/r-working-dir/n...
#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'...
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...
}
\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*...
}
/*
# ユリウス日から西暦年月日を得る
*/
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 パッケージ名 [...
以下のように,全て OK になるように努力する
$ R CMD check nissuu --no-latex
* using log directory '/Users/aoki/Desktop/r-working-dir...
* 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 ......
* 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 source...
* creating nissuu-Ex.R ... OK
* checking examples ... OK
*7. パッケージを作成する R CMD build パッケージ名 [#ief1...
$ 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]
「パッケージとデータ」−>「パッケージインストーラ」−>「...
*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 の関数だけから成り立つパッケージの作成法 [#o...
-上記の 0 は不要
-上記の 2 では,当然 R の関数だけ書けばよい
-上記の 4 で作られる src ディレクトリはゴミ箱へ捨てる
-上記の 5 は不要
そのほかは,上に準じて行えばよい
* 関連ページ [#z125af65]
- http://www.okada.jp.org/RWiki/index.php?cmd=read&page=%...
ページ名: