算額あれこれ

算額問題をコンピュータで解きます

R 用の indent その3

たぶん,最終的に用いているのは皆同じプログラムなんだろうけど,Sweave を使う例も挙げておこう。

<<>>=
test<-function(x,y,z) {
n<-length(x) # 日本語のコメント
result<-numeric(n)
for (i in 1:n) {
result[i]<-(x[i]+y[i])*z[i]
}
return(result)
}
@

を含む *.Rnw を \SweaveOpts{keep.source=FALSE} で Sweave すると,以下のようなチャンクを含む *.tex ファイル(もちろん最終的には,*.pdf ファイルなど)ができる。
">" や "+"  は options(prompt = " ", continue = " ") で,空白にすることができる(空にはできないようだ)。
ただしこの場合も,コメント行はすっかりとなくなる

> test <- function(x, y, z) {
+     n <- length(x)
+     result <- numeric(n)
+     for (i in 1:n) {
+         result[i] <- (x[i] + y[i]) * z[i]
+     }
+     return(result)
+ }