Google の R コーディングスタイル
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
[[RjpWiki]]
R風のコーディングスタイル
他の多くの計算機言語と同じく,RにもRらしいコーディングス...
では以下のようなスタイルが推奨されている.以下はその簡訳...
ファイル名は .R で終わり,意味を持つべきである.オブジェ...
ファイル名: 良 predict_ad_revenue.R
不可 foo.R
オブジェクト名: 良 avg.clicks
可 avgClicks
不可 avg_Clicks
関数名: 良 CalculateAvgClicks
不可 calculate_avg_clicks, calculateAvgC...
定数名: 良 kConstantName (先頭にkを付ける)
一行は半角文字で80文字以下にする.インデントは空白二文字...
良い例:
tab.prior <- table(df[df$days.from.opt < 0, "campaign.id...
total <- sum(x[, 1])
total <- sum(x[1, ])
悪い例:
tab.prior <- table(df[df$days.from.opt<0, "campaign.id"]...
tab.prior <- table(df[df$days.from.opt < 0,"campaign.id"...
tab.prior<- table(df[df$days.from.opt < 0, "campaign.id"...
tab.prior<-table(df[df$days.from.opt < 0, "campaign.id"]...
total <- sum(x[,1]) # コンマの後に空白を置く
total <- sum(x[ ,1]) # コンマの前ではなく後に空白を置く
関数呼び出しを除き,右括弧の前には空白を一つ置く.
良い例: if (debug)
悪い例: if(debug)
= や <- 位置での整列のために,余分な空白を置くことは構わ...
plot(x = x.coord,
y = data.mat[, MakeColName(metric, ptiles[1], "...
ylim = ylim,
xlab = "dates",
ylab = metric,
main = (paste(metric, " for 3 samples ", sep = "")))
括弧や鈎括弧中のコードの周りに空白を置かない.例外として...
良い例: if (debug), x[1, ]
悪い例: if ( debug ), x[1,]
左波括弧だけの行は作らない.右波括弧は必ず単独の行とする...
どちらでも良いが,混在は避ける
if (is.null(ylim)) {
ylim <- c(0, 0.06)
}
もしくは
if (is.null(ylim))
ylim <- c(0, 0.06)
一つのブロックの最初は必ず新しい行に置く.
悪い例:
if (is.null(ylim)) ylim <- c(0, 0.06)
if (is.null(ylim)) {ylim <- c(0, 0.06)}
else は波括弧で囲む.
良い例:
if (condition) {
one or more lines
} else {
one or more lines
}
悪い例:
if (condition) {
one or more lines
}
else {
one or more lines
}
悪い例:
if (condition)
one line
else
one line
変数への付値には <- を用い,= を使わない.
良い例: x <- 5
悪い例: x = 5
行の最後にセミコロンを置いたり,一行に複数の実行文を置く...
コードの構成に下記のような同一の順序を用いれば,お互いの...
著作権のコメント
著者のコメント
ファイル内容(プログラムの目的,入力,出力を含む)へのコメ...
source() や library() 実行文
関数定義
もし適用可能なら(例えば print, plot),実行文
ユニットテスト(コードの正確さや,バグ検証用のテストプログ...
コードにはコメントを付けるべきである.コメント行は # と一...
hist(df$pct.spent,
breaks = "scott", # method for choosing number of ...
main = "Histogram: fraction budget spent by campa...
xlab = "Fraction of budget spent",
ylab = "Frequency (count of campaignids)")
関数定義では,先ず既定値を持たない仮引数,次に既定値を持...
良い例:
PredictCTR <- function(query, property, num.days,
show.plot = TRUE)
悪い例:
PredictCTR <- function(query, property, num.days, show.p...
TRUE)
関数は関数定義行の直後にコメントを含むべきである.これは...
CalculateSampleCovariance <- function(x, y, verbose = TR...
# Computes the sample covariance between two vectors.
#
# Args:
# x: One of two vectors whose sample covariance is t...
# y: The other vector. x and y must have the same le...
# with no missing values.
# verbose: If TRUE, prints sample covariance; if not...
#
# Returns:
# The sample covariance between x and y.
n <- length(x)
# Error handling
if (n <= 1 || n != length(y)) {
stop("Arguments x and y have different lengths: ",
length(x), " and ", length(y), ".")
}
if (TRUE %in% is.na(x) || TRUE %in% is.na(y)) {
stop(" Arguments x and y must not have missing value...
}
covariance <- var(x, y)
if (verbose)
cat("Covariance = ", round(covariance, 4), ".\n", se...
return(covariance)
}
コードの改善,拡張に対するコメント(TODO)は,例えば以下の...
TODO(ユーザ名): どうすべきかを示す明確な既述
エラー処理は stop() 関数を使うべきである.attach 関数の使...
S言語は二つのオブジェクトシステム S3, S4 を持ち,Rでは両...
参考文献:
Thomas Lumley, "Programmer's Niche: A Simple Class, in S...
R News 4/1, 2004, pp. 33 - 36: http://cran.r-project.org...
S3メソッドはより簡易であり柔軟である.S4メソッドはより形...
上で述べたコーディングのルールは,正当な理由がなければ順...
最後に述べるべきこととして,常識を持ち,一貫性を重視すべ...
コードの書き方についてはこれぐらいでいいだろう.当然,コ...
終了行:
[[RjpWiki]]
R風のコーディングスタイル
他の多くの計算機言語と同じく,RにもRらしいコーディングス...
では以下のようなスタイルが推奨されている.以下はその簡訳...
ファイル名は .R で終わり,意味を持つべきである.オブジェ...
ファイル名: 良 predict_ad_revenue.R
不可 foo.R
オブジェクト名: 良 avg.clicks
可 avgClicks
不可 avg_Clicks
関数名: 良 CalculateAvgClicks
不可 calculate_avg_clicks, calculateAvgC...
定数名: 良 kConstantName (先頭にkを付ける)
一行は半角文字で80文字以下にする.インデントは空白二文字...
良い例:
tab.prior <- table(df[df$days.from.opt < 0, "campaign.id...
total <- sum(x[, 1])
total <- sum(x[1, ])
悪い例:
tab.prior <- table(df[df$days.from.opt<0, "campaign.id"]...
tab.prior <- table(df[df$days.from.opt < 0,"campaign.id"...
tab.prior<- table(df[df$days.from.opt < 0, "campaign.id"...
tab.prior<-table(df[df$days.from.opt < 0, "campaign.id"]...
total <- sum(x[,1]) # コンマの後に空白を置く
total <- sum(x[ ,1]) # コンマの前ではなく後に空白を置く
関数呼び出しを除き,右括弧の前には空白を一つ置く.
良い例: if (debug)
悪い例: if(debug)
= や <- 位置での整列のために,余分な空白を置くことは構わ...
plot(x = x.coord,
y = data.mat[, MakeColName(metric, ptiles[1], "...
ylim = ylim,
xlab = "dates",
ylab = metric,
main = (paste(metric, " for 3 samples ", sep = "")))
括弧や鈎括弧中のコードの周りに空白を置かない.例外として...
良い例: if (debug), x[1, ]
悪い例: if ( debug ), x[1,]
左波括弧だけの行は作らない.右波括弧は必ず単独の行とする...
どちらでも良いが,混在は避ける
if (is.null(ylim)) {
ylim <- c(0, 0.06)
}
もしくは
if (is.null(ylim))
ylim <- c(0, 0.06)
一つのブロックの最初は必ず新しい行に置く.
悪い例:
if (is.null(ylim)) ylim <- c(0, 0.06)
if (is.null(ylim)) {ylim <- c(0, 0.06)}
else は波括弧で囲む.
良い例:
if (condition) {
one or more lines
} else {
one or more lines
}
悪い例:
if (condition) {
one or more lines
}
else {
one or more lines
}
悪い例:
if (condition)
one line
else
one line
変数への付値には <- を用い,= を使わない.
良い例: x <- 5
悪い例: x = 5
行の最後にセミコロンを置いたり,一行に複数の実行文を置く...
コードの構成に下記のような同一の順序を用いれば,お互いの...
著作権のコメント
著者のコメント
ファイル内容(プログラムの目的,入力,出力を含む)へのコメ...
source() や library() 実行文
関数定義
もし適用可能なら(例えば print, plot),実行文
ユニットテスト(コードの正確さや,バグ検証用のテストプログ...
コードにはコメントを付けるべきである.コメント行は # と一...
hist(df$pct.spent,
breaks = "scott", # method for choosing number of ...
main = "Histogram: fraction budget spent by campa...
xlab = "Fraction of budget spent",
ylab = "Frequency (count of campaignids)")
関数定義では,先ず既定値を持たない仮引数,次に既定値を持...
良い例:
PredictCTR <- function(query, property, num.days,
show.plot = TRUE)
悪い例:
PredictCTR <- function(query, property, num.days, show.p...
TRUE)
関数は関数定義行の直後にコメントを含むべきである.これは...
CalculateSampleCovariance <- function(x, y, verbose = TR...
# Computes the sample covariance between two vectors.
#
# Args:
# x: One of two vectors whose sample covariance is t...
# y: The other vector. x and y must have the same le...
# with no missing values.
# verbose: If TRUE, prints sample covariance; if not...
#
# Returns:
# The sample covariance between x and y.
n <- length(x)
# Error handling
if (n <= 1 || n != length(y)) {
stop("Arguments x and y have different lengths: ",
length(x), " and ", length(y), ".")
}
if (TRUE %in% is.na(x) || TRUE %in% is.na(y)) {
stop(" Arguments x and y must not have missing value...
}
covariance <- var(x, y)
if (verbose)
cat("Covariance = ", round(covariance, 4), ".\n", se...
return(covariance)
}
コードの改善,拡張に対するコメント(TODO)は,例えば以下の...
TODO(ユーザ名): どうすべきかを示す明確な既述
エラー処理は stop() 関数を使うべきである.attach 関数の使...
S言語は二つのオブジェクトシステム S3, S4 を持ち,Rでは両...
参考文献:
Thomas Lumley, "Programmer's Niche: A Simple Class, in S...
R News 4/1, 2004, pp. 33 - 36: http://cran.r-project.org...
S3メソッドはより簡易であり柔軟である.S4メソッドはより形...
上で述べたコーディングのルールは,正当な理由がなければ順...
最後に述べるべきこととして,常識を持ち,一貫性を重視すべ...
コードの書き方についてはこれぐらいでいいだろう.当然,コ...
ページ名: