【R】をバッチ実行する方法 その2 引数を受ける方法 [データサイエンス、統計モデル]
【R】をバッチ実行する方法
https://skellington.blog.ss-blog.jp/2021-04-26
↑
こちらの続き
引数をつける場合は、
args <- commandArgs(trailingOnly = TRUE)
で引数を受けることができます。
例えば、引数によってモデルを分岐するやり方がこちら。
Rscript iris_predict.r 1 ← 1番目のモデルの結果を出力
Rscript iris_predict.r ← それ以外(2番目のモデルの結果を出力)
# irisを使った線形回帰モデル
model <- lm(Sepal.Length~., data=iris)
out <- predict(model)
model2 <- lm(Sepal.Length~Species, data=iris)
out2 <- predict(model2)
args <- commandArgs(trailingOnly = TRUE)
mode <- 0
if ( !is.na(args[1]) ) { mode <- as.integer(args[1]); }
if ( mode == 1 ) {
write.table(out, file = "/Users/rlsuu120061/Desktop/out.txt",
sep = ',', quote=F, row.names=F, col.names=F)
} else {
write.table(out2, file = "/Users/rlsuu120061/Desktop/out.txt",
sep = ',', quote=F, row.names=F, col.names=F)
}
https://skellington.blog.ss-blog.jp/2021-04-26
↑
こちらの続き
引数をつける場合は、
args <- commandArgs(trailingOnly = TRUE)
で引数を受けることができます。
例えば、引数によってモデルを分岐するやり方がこちら。
Rscript iris_predict.r 1 ← 1番目のモデルの結果を出力
Rscript iris_predict.r ← それ以外(2番目のモデルの結果を出力)
# irisを使った線形回帰モデル
model <- lm(Sepal.Length~., data=iris)
out <- predict(model)
model2 <- lm(Sepal.Length~Species, data=iris)
out2 <- predict(model2)
args <- commandArgs(trailingOnly = TRUE)
mode <- 0
if ( !is.na(args[1]) ) { mode <- as.integer(args[1]); }
if ( mode == 1 ) {
write.table(out, file = "/Users/rlsuu120061/Desktop/out.txt",
sep = ',', quote=F, row.names=F, col.names=F)
} else {
write.table(out2, file = "/Users/rlsuu120061/Desktop/out.txt",
sep = ',', quote=F, row.names=F, col.names=F)
}