4

コマンドラインを介して複数のファイルパス引数をRscriptに渡そうとしています。これは、引数パーサーを使用して処理できます。最終的にはこのようなものが欲しい

Rscript test.R --inputfiles fileA.txt fileB.txt fileC.txt --printvar yes --size 10 --anotheroption helloworld -- etc...

コマンドラインを通過し、解析時にRの配列として結果を取得します

args$inputfiles =  "fileA.txt", "fileB.txt", "fileC.txt"

optparseやgetoptを含むいくつかのパーサーを試しましたが、どちらもこの機能をサポートしていないようです。argparseは知っていますが、現在Rバージョン2.15.2では使用できません。

何か案は?

ありがとう

4

5 に答える 5

6

この質問が尋ねられたとき、それはCRANでリリースされていませんでしたが、これを行うことができるargparseモジュールのベータ版が現在そこにあります。これは基本的に同じ名前の人気のあるpythonモジュールのラッパーであるため、使用するには最新バージョンのpythonをインストールする必要があります。詳細については、インストールノートを参照してください。含まれている基本的な例は、任意の長さの入力ファイルのリストを取得できるように、変更するのが難しいことではない任意の長さの数値のリストを合計します。

> install.packages("argparse")
> library("argparse")
> example("ArgumentParser")
于 2013-03-01T02:13:52.230 に答える
6

コマンドラインオプションを説明する方法は、ほとんどの人がそれらが使用されることを期待する方法とは異なります。通常、コマンドラインオプションは単一のパラメーターを取り、先行するオプションのないパラメーターは引数として渡されます。引数が複数の項目(ファイルのリストなど)を取る場合は、strsplit()を使用して文字列を解析することをお勧めします。

optparseを使用した例を次に示します。

library (optparse)
option_list <- list ( make_option (c("-f","--filelist"),default="blah.txt", 
                                   help="comma separated list of files (default %default)")
                     )

parser <-OptionParser(option_list=option_list)
arguments <- parse_args (parser, positional_arguments=TRUE)
opt <- arguments$options
args <- arguments$args

myfilelist <- strsplit(opt$filelist, ",")

print (myfilelist)
print (args)

いくつかの実行例を次に示します。

$ Rscript blah.r -h
Usage: blah.r [options]


Options:
    -f FILELIST, --filelist=FILELIST
        comma separated list of files (default blah.txt)

    -h, --help
        Show this help message and exit


$ Rscript blah.r -f hello.txt
[[1]]
[1] "hello.txt"

character(0)
$ Rscript blah.r -f hello.txt world.txt
[[1]]
[1] "hello.txt"

[1] "world.txt"
$ Rscript blah.r -f hello.txt,world.txt another_argument and_another
[[1]]
[1] "hello.txt" "world.txt"

[1] "another_argument" "and_another"
$ Rscript blah.r an_argument -f hello.txt,world.txt,blah another_argument and_another
[[1]]
[1] "hello.txt" "world.txt" "blah"     

[1] "an_argument"      "another_argument" "and_another"     

strsplitの場合、正規表現を使用して区切り文字を決定できることに注意してください。次のようなものをお勧めします。これにより、コンマまたはコロンを使用してリストを区切ることができます。

myfilelist <- strsplit (opt$filelist,"[,:]")
于 2016-08-22T20:40:07.293 に答える
4

スクリプトtest.Rの前に、次のように配置します。

args <- commandArgs(trailingOnly = TRUE)

hh <- paste(unlist(args),collapse=' ')
listoptions <- unlist(strsplit(hh,'--'))[-1]
options.args <- sapply(listoptions,function(x){
         unlist(strsplit(x, ' '))[-1]
        })
options.names <- sapply(listoptions,function(x){
  option <-  unlist(strsplit(x, ' '))[1]
})
names(options.args) <- unlist(options.names)
print(options.args)

取得するため :

$inputfiles
[1] "fileA.txt" "fileB.txt" "fileC.txt"

$printvar
[1] "yes"

$size
[1] "10"

$anotheroption
[1] "helloworld"
于 2012-12-09T19:01:34.497 に答える
1

周りを検索し、新しいパッケージを下から上に書くことを避けた後、パッケージoptparseを使用して複数の引数を入力する最良の方法は、ファイル名に含めることが違法である可能性が最も高い文字で入力ファイルを区切ることであると考えました(たとえば、コロン)

Rscript test.R --inputfiles fileA.txt:fileB.txt:fileC.txt etc...

スペースがエスケープされている限り、ファイル名にスペースを含めることもできます(optparseがこれを処理します)

Rscript test.R --inputfiles file\ A.txt:file\ B.txt:fileC.txt etc...

Ultimatley、質問と以下で言及されているような複数の引数をサポートするパッケージ(おそらくoptparseの修正バージョン)があると便利です

Rscript test.R --inputfiles fileA.txt fileB.txt fileC.txt

このような些細な機能は、optparseなどの広く使用されているパッケージに実装されると思います。

乾杯

于 2012-12-10T23:57:18.437 に答える
0

入力引数が同じ長さのリストである場合、@agstudyのソリューションは正しく機能しません。デフォルトでは、sapplyは同じ長さの入力をリストではなく行列に折りたたみます。修正は非常に簡単です。引数を解析するsapplyでsimplifyをfalseに明示的に設定するだけです。

args <- commandArgs(trailingOnly = TRUE)

hh <- paste(unlist(args),collapse=' ')
listoptions <- unlist(strsplit(hh,'--'))[-1]
options.args <- sapply(listoptions,function(x){
         unlist(strsplit(x, ' '))[-1]
        }, simplify=FALSE)
options.names <- sapply(listoptions,function(x){
  option <-  unlist(strsplit(x, ' '))[1]
})
names(options.args) <- unlist(options.names)
print(options.args)
于 2014-12-11T20:23:07.230 に答える