Rstudio + roxygen2お試し

  • Rstudioをインストール
  • roxygen2パッケージをインストール
install.packages("roxygen2")
  • Rstudioを立ち上げ、新規プロジェクトを新規ディレクトリで作る(そうしなくてもうまくいくだろうけれど、初回はまっさらで)。
  • New Projectを選ぶ

  • New directoryを選ぶ

  • R packageを選ぶ

  • testaddというパッケージ名で C:/Users/ryamada/Desktop/RsourceFolder/add.RというRコードファイルを読み込ませ、C:/Users/ryamada/Desktop/testaddというフォルダ以下に、入れ子でC:/Users/ryamada/Desktop/testadd/testadd/というフォルダを作り、その配下にパッケージ関連のもろもろを作る(testaddの入れ子にする必要はない。ちょっと都合があって…)

  • このadd.Rというファイルは以下のように2つの関数add(),add2()があって、それぞれにroxygen2様式でコメントが書いてある
#' Add together two numbers
#'
#' @param x A number
#' @param y A number
#' @return The sum of \code{x} and \code{y}
#' @examples
#' add(1, 1)
#' add(10, 1)
add <- function(x, y) {
  x + y
}
#' Add together two numbers
#'
#' @param x A number
#' @param y A number
#' @return The sum of \code{x} and \code{y}
#' @examples
#' add2(1, 1)
#' add2(10, 1)
add2 <- function(x, y) {
  x + y
}
  • Rstudioの別ウィンドウが立ち上がってもろもろのディレクトリとファイルができる

  • この段階ではまだroxygen2は働いていないので、関数add()の.Rdファイルの中身は、骨格はできているけれど、もろもろの説明は埋まっていない
\name{add}
\alias{add}
%- Also NEED an '\alias' for EACH other topic documented here.
\title{
%%  ~~function to do ... ~~
}
\description{
%%  ~~ A concise (1-5 lines) description of what the function does. ~~
}
\usage{
add(x, y)
}
%- maybe also 'usage' for other objects documented here.
\arguments{
  \item{x}{
%%     ~~Describe \code{x} here~~
}
  \item{y}{
%%     ~~Describe \code{y} here~~
}
}
\details{
%%  ~~ If necessary, more details than the description above ~~
}
\value{
%%  ~Describe the value returned
%%  If it is a LIST, use
%%  \item{comp1 }{Description of 'comp1'}
%%  \item{comp2 }{Description of 'comp2'}
%% ...
}
\references{
%% ~put references to the literature/web site here ~
}
\author{
%%  ~~who you are~~
}
\note{
%%  ~~further notes~~
}

%% ~Make other sections like Warning with \section{Warning }{....} ~

\seealso{
%% ~~objects to See Also as \code{\link{help}}, ~~~
}
\examples{
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (x, y) 
{
    x + y
  }
}
% Add one or more standard keywords, see file 'KEYWORDS' in the
% R documentation directory.
\keyword{ ~kwd1 }
\keyword{ ~kwd2 }% __ONLY ONE__ keyword per line
  • roxygen2を使うために、以下のようにconfigure build toolsでビルドオプションをroxygenにする


  • add.Rdの中身が以下のように「情報つき」に書き換わる
% Generated by roxygen2 (4.0.1): do not edit by hand
\name{add}
\alias{add}
\title{Add together two numbers}
\usage{
add(x, y)
}
\arguments{
\item{x}{A number}

\item{y}{A number}
}
\value{
The sum of \code{x} and \code{y}
}
\description{
Add together two numbers
}
\examples{
add(1, 1)
add(10, 1)
}
  • なおRstudioでパッケージ作成を始めると初めに指定したRソースファイルがプロジェクト領域にコピーされるので、そのRソースファイルにroxygen2用のコメントを書く
  • 日本語文字は不可
  • さらにいろいろ機能はありそうだけれど、これだけでappreciateしておくのが(少なくとも今の自分には)無難な感じ
  • 知られているバグ
    • 引数にデフォルト値をhoge <- function(n=3){}とすると、改行コードが入っておかしくなるらしい。英語圏のFAQでは、その問題が、「文字列を引数にしたとき」に限定して議論されている。日本語コンピュータ環境だと、普通の変数の扱いをしても「日本語フォント(マルチバイト文字)が入ってますよ」と怒られる。というわけで、日本語コンピュータ環境でやる場合には、「改行だか何だかしらないけれど、普通の変数のときにはおかしくならない文字コード」が挿入されてもよいように、UTF8とかでソースコードを作っておくとよいようだ(SFISからUTF8に変えたら解消しましたと一度は確認がとれたと思いましたが再現性がなかったです…)
    • 結局エンコーディング操作はダメそうで、こちらにあるように、デフォルト値の指定をfunction()の中でやらずに
hoge <- function(x=10){
  x^2
}
# の代わりに
hoge <- function(x){
  if(missing(x)){
    x <- 10
  }
x^2
}
    • とする作戦が対応策かもしれません
  • ValuesとかDetailsとかをリストにするには(参考)
##' @details text describing parameter inputs in more detail.
##' \itemize{
##'  \item{"parameter 1"}{Stuff}
##'  \item{"parameter 2"}{Stuff}
##' }