114

R CMD BATCH my_script.RターミナルからRスクリプトを実行するために使用しています。私は今、コマンドに引数を渡したいところですが、それを機能させるにはいくつかの問題があります。その場合、実行中の R スクリプトで使用できる引数として解釈されるのではなく、出力ファイルになりますR CMD BATCH my_script.R blablablabla

引数として正しくRscript my_script.R blabla渡されるように見えるものを試しましたが、取得した出力ファイルを取得できません(ファイルが必要です)。への呼び出しの出力を選択したファイル名にリダイレクトすることはできますが、ファイルに含まれる R 入力コマンドを取得する方法とは異なります。 blablamy_script.RoutR CMD BATCH.RoutRscriptR CMD BATCH.Rout

したがって、理想的には、メソッドを介して実行される R スクリプトに引数を渡す方法を求めていますが、同等のファイルを生成する方法があればR CMD BATCH、使用するアプローチに満足しています。Rscript.Rout

4

6 に答える 6

125

私の印象は、それR CMD BATCHは少し遺物だということです。いずれにせよ、より新しいRscript実行可能ファイル (すべてのプラットフォームで利用可能) を使用すると、commandArgs()コマンド ライン引数の処理が非常に簡単になります。

例として、以下に小さなスクリプトを示します"myScript.R"

## myScript.R
args <- commandArgs(trailingOnly = TRUE)
rnorm(n=as.numeric(args[1]), mean=as.numeric(args[2]))

コマンドラインから呼び出すと、次のようになります。

> Rscript myScript.R 5 100
[1]  98.46435 100.04626  99.44937  98.52910 100.78853

編集:

お勧めするわけではありませんが、... と を組み合わせて使用​​すると、source()で作成されたようなファイルを作成sink()できます。1 つの方法は、Rscript で直接呼び出す小さな R スクリプトを作成することです。次のようになります。Rscript.RoutR CMD BATCH RscriptEcho.R

## RscriptEcho.R
args <- commandArgs(TRUE)
srcFile <- args[1]
outFile <- paste0(make.names(date()), ".Rout")
args <- args[-1]

sink(outFile, split = TRUE)
source(srcFile, echo = TRUE)

実際のスクリプトを実行するには、次のようにします。

Rscript RscriptEcho.R myScript.R 5 100
[1]  98.46435 100.04626  99.44937  98.52910 100.78853

これは、指定された引数で実行myScript.Rされ、インターリーブされた入力、出力、およびメッセージを一意の名前の にシンクします.Rout

Edit2:
Rscript を詳細に実行し、詳細な出力をファイルに配置できます。

Rscript --verbose myScript.R 5 100 > myScript.Rout
于 2013-01-05T01:04:58.357 に答える
11

あなたは前に引数を置き、引数にmy_script.R使用する必要があり-ます、例えば

R CMD BATCH -blabla my_script.R

commandArgs()-blablaこの場合、文字列として受け取ります。詳細については、ヘルプを参照してください。

$ R CMD BATCH --help
Usage: R CMD BATCH [options] infile [outfile]

Run R non-interactively with input from infile and place output (stdout
and stderr) to another file.  If not given, the name of the output file
is the one of the input file, with a possible '.R' extension stripped,
and '.Rout' appended.

Options:
  -h, --help        print short help message and exit
  -v, --version     print version info and exit
  --no-timing           do not report the timings
  --            end processing of options

Further arguments starting with a '-' are considered as options as long
as '--' was not encountered, and are passed on to the R process, which
by default is started with '--restore --save --no-readline'.
See also help('BATCH') inside R.
于 2013-01-05T00:42:05.230 に答える
4

1行のソリューションが常に良いと思うので、回答を追加します! ファイルの上にmyRscript.R、次の行を追加します。

eval(parse(text=paste(commandArgs(trailingOnly = TRUE), collapse=";")))

次に、次のようなスクリプトを送信します。

R CMD BATCH [options] '--args arguments you want to supply' myRscript.R &

例えば:

R CMD BATCH --vanilla '--args N=1 l=list(a=2, b="test") name="aname"' myscript.R &

それで:

> ls()
[1] "N"    "l"    "name"
于 2016-03-16T09:52:48.017 に答える
0

を使用して、コマンド ライン引数を処理する別の方法を次に示しR CMD BATCHます。ここでの以前の回答に基づいた私のアプローチでは、コマンド ラインで引数を指定し、R スクリプトでそれらの一部またはすべてに既定値を指定できます。

ここに、 test.Rという名前の R ファイルがあります。

defaults <- list(a=1, b=c(1,1,1)) ## default values of any arguments we might pass

## parse each command arg, loading it into global environment
for (arg in commandArgs(TRUE))
  eval(parse(text=arg))

## if any variable named in defaults doesn't exist, then create it
## with value from defaults
for (nm in names(defaults))
  assign(nm, mget(nm, ifnotfound=list(defaults[[nm]]))[[1]])

print(a)
print(b)

コマンド ラインで次のように入力すると、

R CMD BATCH --no-save --no-restore '--args a=2 b=c(2,5,6)' test.R

次に、R 内にa=2b=がありc(2,5,6)ます。しかし、たとえば、 を省略bして、別の引数を追加することもできcます。

R CMD BATCH --no-save --no-restore '--args a=2 c="hello"' test.R

次に、R にはa= 2b= c(1,1,1)(デフォルト)、およびc=があり"hello"ます。

最後に、便宜上、環境に注意している限り、R コードを関数でラップできます。

## defaults should be either NULL or a named list
parseCommandArgs <- function(defaults=NULL, envir=globalenv()) {
  for (arg in commandArgs(TRUE))
    eval(parse(text=arg), envir=envir)

  for (nm in names(defaults))
    assign(nm, mget(nm, ifnotfound=list(defaults[[nm]]), envir=envir)[[1]], pos=envir)
}

## example usage:
parseCommandArgs(list(a=1, b=c(1,1,1)))
于 2016-12-15T00:50:31.133 に答える