パッケージを作る
Rから他言語利用を参考に
まずは,Mac OS X で C 言語で書いた関数を呼び出すようなパッケージを作るときの覚え書き
だいぶ前に,何段階かであれこれやったので,もう忘れた
名前は何でもよいが,説明の都合上 ~/Desktop/r-working-dir/ とする
"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 }
"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) }
> 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
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)
\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" }
関連する関数は 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{}
/* # 西暦年月日からユリウス日を得る # 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; }
以下のように,全て 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
$ 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 が最終目的のファイル
「パッケージとデータ」−>「パッケージインストーラ」−>「このコンピュータ上のソースパッケージ」−>「Install」で,7. で作ったファイル nissuu_1.0.tar.gz を選択するとインストールされる。
> 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"
そのほかは,上に準じて行えばよい