【问题标题】:Confused about passing arguments from command line (optparse) R, arguments not getting stored even with explicit 'default' option对从命令行(optparse)R传递参数感到困惑,即使使用显式“默认”选项也没有存储参数
【发布时间】:2019-05-04 18:45:22
【问题描述】:

学习如何从命令行使用 R。我遇到了“optparse”包并开始使用它。以为一切都很好,直到我注意到它的行为不像我最初预期的那样,并且无法让它在我想要的目录中写入文件。

为了简单起见,我决定用一个简短的脚本来解释发生了什么:

require(optparse)

#Parse arguments from command line
options <- list(
  make_option(c("-d", "--directory"), action = "store", default = getwd(), type = "character", help="Working directory path."),
  make_option(c("-e", "--extension"), action = "store", default = ".tsv", type = "character", help="File(s) extension."),
  make_option(c("-p", "--outputPath"), action = "store", default = getwd(), type = "character", help="Output file(s) directory to be saved at."),
  make_option(c("-o", "--outputName"), action = "store", default = "output", type = "character", help="Output file(s) base name."),
  make_option(c("-s", "--separator"), action = "store", default = NA, help="Separator to use explicitely")
)
arguments <- parse_args(OptionParser(option_list = options))

setwd(arguments$d) 

cat(arguments$d, arguments$e, arguments$s)

这很好用,它显示:

C:/Users/path/to/where/I/work .tsv NA

但是,要求 'outputPath' 和 outputName' 参数

cat(arguments$p, arguments$o)

绝对不会打印任何内容...即使我明确地为这些参数提供了默认值、类型和操作。 outputPath 选项实际上与 directory 选项相同!

使用 getwd()、as.character(getwd()) 或什至 as.character(file.path(getwd())) 对选项 'outputPath' 给出相同的结果。

从命令行传递参数(而不是使用默认值)返回完全相同的东西(你对 arguments$d、arguments$e、arguments$s 的期望,而对于 arguments$p、arguments 什么都没有$o)

我对此感到非常困惑;当然,当尝试在我的真实脚本中使用这些变量时......这是不可能的,因为它说它们的长度为零......

有趣的是,如果我这样做:

cat(unlist(arguments))

我确实得到了我期望的输出......但我还在我的参数列表的末尾得到了一个值为 FALSE 的附加逻辑变量。不知道从哪里来的……

C:/Users/path/to/where/I/work .tsv C:/Users/path/to/where/I/work output NA FALSE

我在 Windows powershell 上运行它:

Rscript.exe .\script.R

当我输入长标志(例如 --outputName 某些东西)时,标志“点亮”并在终端上变成白色,而使用短标志(例如 -o 东西)不会(它在终端上看起来是灰色的)命令行,好像它是未使用的或什么的)。不知道这是否意味着什么,因为两者都做完全相同的事情。只是想指出来。

最后一件事!当然 'C:/Users/path/to/where/I/work' 不是真正的目录路径,而我实际使用的路径上有一个空格……你认为这重要吗?

编辑:好的,现在我知道 FALSE 来自哪里,只需要打印我的“参数”列表......我觉得很愚蠢。但是,最初的问题仍然存在......现在让我更加困惑,因为这些值存储在我的“参数”列表中。

在做:

arguments

演出:

$directory
[1] "C:/Users/alang/Desktop/LabiVicenteLau D="

$extension
[1] ".tsv"

$outputPath
[1] "C:/Users/alang/Desktop/LabiVicenteLau D="

$output
[1] "output"

$separator
[1] NA

$help
[1] FALSE

【问题讨论】:

    标签: r


    【解决方案1】:

    参数的部分匹配是你的(我猜不是)朋友。

    #!/usr/bin/env Rscript
    require(optparse)
    
    #Parse arguments from command line
    options <- list(
      make_option(c("-d", "--directory"), action = "store", default = getwd(), type = "character", help="Working directory path."),
      make_option(c("-e", "--extension"), action = "store", default = ".tsv", type = "character", help="File(s) extension."),
      make_option(c("-p", "--outputPath"), action = "store", default = getwd(), type = "character", help="Output file(s) directory to be saved at."),
      make_option(c("-o", "--outputName"), action = "store", default = "output", type = "character", help="Output file(s) base name."),
      make_option(c("-s", "--separator"), action = "store", default = NA, help="Separator to use explicitely")
    )
    arguments <- parse_args(OptionParser(option_list = options))
    
    setwd(arguments$d)
    
    print("R tries to match anything starting with an o.")
    print(arguments$o)
    print("No success")
    print("R tries not to partially matchi anything. Explicitly call argument.")
    print(arguments$outputPath)
    print("Great success!")
    

    上面的脚本给了我:

    romunov@kista  ~/Documents
    $ ./test.R
    Loading required package: optparse
    Warning message:
    package 'optparse' was built under R version 3.5.3
    [1] "R tries to match anything starting with an o."
    NULL
    [1] "No success"
    [1] "R tries not to partially matchi anything. Explicitly call argument."
    [1] "C:/Users/romunov/Documents"
    [1] "Great success!"
    

    【讨论】:

    • 是的,刚刚想通了 :D 谢谢!我认为可能有办法让它按照我最初尝试匹配的方式工作,但现在我会这样做。
    • 您应该按参数的全名匹配参数。你的代码应该是可读的,而不是简短的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多