LoginSignup
52
34

More than 5 years have passed since last update.

Rのスクリプトをコマンドライン実行し、ついでに引数を受け取る

Posted at

Rのスクリプトをコマンドライン実行し、引数を受け取る方法

Rは通常、インタラクティブに関数を実行し、統計的処理を行うことができます。
しかし、バッチ処理などで、インタラクティブにではなく、Rのスクリプトを実行したい時があります。
その時の方法を紹介し、ついでに、引数を受け取る方法も紹介します。

以下でコマンドライン実行できます。

Rscript hoge.R

引数を受け取る方法

以下のコマンドを実行した時の引数の受け取り方

Rscript hoge.R args1 args2 args3
hoge.R
args1 = commandArgs(trailingOnly=TRUE)[1] #1番目の引数を取得する
args2 = commandArgs(trailingOnly=TRUE)[2] #2番目の引数を取得する
args3 = commandArgs(trailingOnly=TRUE)[3] #3番目の引数を取得する
52
34
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
52
34