R-function-only; No helps; github & R package

  • この記事の「なんちゃってRパッケージ」はこちら Here is the github containing files and directories handled in this note.
  • githubアカウントでの作業 Tasks in your github account
    • Githubアカウント(アカウント名がryamada22とする)で新しいrepositoryを作る(ry1という名前で作ったとする) Make a new repository (named "ry1" here) in your github account ("ryamada22" is the account name here).
    • "Initialize this repository with a README"というオプションを選んで作る(と、以下の手順で、進められるから) Check "initialize this repository with a README", that is just for you to follow the steps below: When you get familiar with github, you can do whatever you like.
  • ローカルPCのターミナル・コマンドラインでの作業 Tasks on your local PC's terminal.
    • ローカスPCにこのgithub上のレポジトリと連携したフォルダを作る Make a clone directory with the repository ryamada22/ry1 in github, with the command below.
git clone https://github.com/ryamada22/ry1
    • このローカルPCにできたry1フォルダが、ローカルなRパッケージ関係のファイル・フォルダ置き場となる This is working directory to prepare all files in your local PC.
  • ローカルPCのR上での作業 Tasks in R of your laptop PC.
    • 適当なところにパッケージ"ry1"のファイル・ディレクトリを一括作成する Using the command below, make a directory "ry1" somewhere of your choice, but different from the one you cloned github repository above.
library(devtools)
create("ry1")
    • 出来たファイル・ディレクトリのすべてを、先ほど作ったローカルPCのry1フォルダ(gitコマンドで作ったもの)にコピー・ペーストする Everything in the "ry1" should be copy-and-pasted to the repository clone directory.
    • その中のRディレクトリに自作のR関数を書いたファイルを"xxx.R"と拡張子Rで置く In the directory "R" of working clone, put your R function file, "xxxx.R".
    • ここで、「おまじない」がある。自作R関数をパッケージ"ry1の関数として呼び出せるようにするには、以下のように"#' @export"という行を前につける必要がある。つけないと、存在はするけれど、コマンドを打って呼び出すことができない「隠れた関数」として登録される You have to remember the line "#' @export" that should be placed before every R function in your file. With the line, the function can be called in R environment and without the line, the function exists but can not becalled, i.e., hidden.
    • 以下の例では、myfx1,myfx2,myfx4は「使える関数」として登録されるが、myfx3は「隠れた関数」となる。myfx4関数がmyfx3関数を内部で使っていることがわかる Three out of four functions below, myfx1,myfx2, and myfx4 can be called and myfx3 is hidden. myfx3 can not be called and hidden but it is used inside of myfx4.
#' @export
myfx1 <- function(n){
	runif(n)
}
#' @export
myfx2 <- function(n){
	rnorm(n)
}

myfx3 <- function(){
	sample(1:100,1)
}

#' @export
myfx4 <- function(){
	n <- myfx3()
	rnorm(n)
}
    • この仕組みは「NAMESPACE」という領分のことであり、実際、以下のdocument()という関数は@exportと指定した関数を"NAMESPACE"ファイルに書き出す仕事をしており、その結果、パッケージのインストールにおいて、「使える関数」となる This is related to "NAMESPACE". The function document() in devtools package recognizes @export and put the function's name in NAMESPACE file that enables R to call them.
    • ローカルPCの作業ディレクトリをRのワーキングディレクトリとした上で、以下のコマンドを打つ
document()
  • ローカルPCのターミナル作業に戻る Now back to terminal in your local PC.
    • これで最低限のファイル・ディレクトリ構成ができたので、これをgitでgithubに上げる The minimum requirements as sharable package, you push all to the github repository.
      • すべてのファイル・ディレクトリをすべてそのままgithub登録する、initial loadというコメントを付けて登録する All files and directories are pushed with comment "initial load".
git add . -A
git commit -m "initial load"
git push
  • githubに上がっていることを確認 Check your github repository updated.
  • Rに戻って、githubからインストールする Now come back to R and install the github package as below.
install_github("ryamada22/ry1")
library(ry1)
myfx1(10)
myfx2(10)
myfx3()
myfx4()
    • 実行結果 Result of useage of the package.
> library(ry1)
> myfx1(10)
 [1] 0.91510335 0.10974977 0.13381433 0.93149611 0.17102376 0.26383976
 [7] 0.53205206 0.43370195 0.58730543 0.04650688
> myfx2(10)
 [1] -0.05734637  1.08738546  0.79239426  0.08779522 -2.56388943  0.91326601
 [7]  0.89475244  0.10207312 -0.59848483  1.37912377
> myfx3()
 エラー:  関数 "myfx3" を見つけることができませんでした 
> myfx4()
 [1] -0.008770683 -1.421339626 -0.479789837  0.857434897 -0.480416926
 [6] -0.341444693  1.728423778 -1.239214118  1.159230815  0.632934762
[11]  0.820224667 -0.682431580  2.000072385  0.690161604  0.192848793
[16] -1.531770969  0.643237911 -0.199730936  0.115768698 -2.931729257
[21] -0.552433207 -1.084468456 -0.257777893  0.549276135  0.502923588
[26]  0.608504506 -0.453976499 -1.404513788 -0.230720142 -0.095452420
[31]  0.336500868 -0.097334794  0.581883101 -0.497125120  2.049112673
[36] -0.398490353  1.907391731 -1.355915290  0.166240283  0.093674561
[41] -1.026088339  0.381732975  0.307184955  2.071312884 -1.102166349
[46] -0.718100629  0.986155410 -0.125979386 -0.810872744 -0.266219696
[51]  0.766873007  0.751484719 -0.739978218
> 
    • ヘルプ記事はない(書いていないから)(でも、これだけ簡単なら、書き散らしたR関数が散逸しないだけでもありがたい、ともいえそうです) No help articles at all, because you did not write anything for them.