LoginSignup
9
10

More than 5 years have passed since last update.

devtools で 自分用の R package を作る(最小限)

Last updated at Posted at 2016-06-08

良い説明は世の中に既にいろいろあるけれど、ここでは:

  • RStudio は使わない(IDEよりエディタ派)。
  • ドキュメントとかテストとかは置いておいて、まずはとりあえずは動く最小限のものを。
  • devtools::skelton() も使わない(羽鳥先生が使うなと言ってるから1

1. 準備

> install.packages(c("devtools", "roxygen2", "testthat", "knitr"))

2.(なにもしない)パッケージ

例として hello という名のパッケージを作る。

> library(devtools)
> create(hello)

hello がパッケージ名で hello というディレクトリが作られるので、そのディレクトリに移動する。

> setwd("hello")
> build()
> install()

これで hello というパッケージが作られ、インストールされた。

> library(hello)

として使える(何もできないけど)。

3. パッケージに関数を入れる

hello/R/hello.R というファイルを作って、例として sayhello 関数を書く。

#' @export
sayhello <- function() {
  print("Hello World!");
}

ビルド&インストール

> document()
> build()
> install()

document() によって NAMESPACE ファイルが更新されて、sayhello() 関数が外部から使える関数に登録された。

> library(hello)
> sayhello()
[1] "Hello World!"

めでたし。

この後

  • DESCRIPTION ファイルを書きましょう。
  • #' ではじまる roxygen block をもっと書きましょう。

しかし、このメモは「最小限」なので、ここでおしまいです。

9
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
10