【问题标题】:Failed to load file through getopt package in R无法通过 R 中的 getopt 包加载文件
【发布时间】:2019-02-18 00:33:27
【问题描述】:

我正在尝试使用 getopt 包打开我的文件,但代码似乎不起作用:

> library(getopt)
args <- commandArgs(trailingOnly = FALSE)

spec = matrix(c(
'help'   , 'h', 0, "character",
'input'  , 'i', 1, "file",
'output' , 'o', 1, "character"), byrow=TRUE, ncol=4)
opt = getopt(spec)
if(opt$input){
file <- read.table(args[1])
}
print(file)

我正在尝试使用命令行来运行以下代码:

Rscript --slave filename.R -i file.txt 

错误信息是: storage.mode(peek.optstring) tryCatchList -> tryCatchOne -> doTryCatch 执行停止 有人可以帮帮我吗?

【问题讨论】:

    标签: r getopt


    【解决方案1】:

    您需要测试组件,如果给出了-i filename.txt,那么您使用opt$file 来访问它。

    修复后的版本是

    #!/usr/bin/Rscript
    
    library(getopt)
    spec <- matrix(c(
        'help'   , 'h', 0, "character",
        'input'  , 'i', 1, "character",
        'output' , 'o', 1, "character"),
        byrow=TRUE, ncol=4)
    opt <- getopt(spec)
    
    if ( !is.null(opt$input) ) {
        file <- read.table(opt$input)
    }
    
    print(file)
    

    这个包我用的比较多,但最近我更喜欢docopt,它更容易使用,功能也更多。

    【讨论】:

      【解决方案2】:

      如果其他人遇到同样的问题,我会收到同样的错误消息

      Error in getopt_options(object, args) : 
        Error in storage.mode(this.argument) <- mode : invalid value
      Calls: parse_args -> parse_options -> getopt_options
      

      我的错误原来是我的类型参数不小心拼错了:

      make_option(c("--chr_len"), type = 'chaqracter', default = NULL, help = "file path: length of all chromosomes")
      

      这个错误在我修正拼写后得到解决

        make_option(c("--chr_len"), type = 'character', default = NULL, help = "file path: length of all chromosomes")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-18
        • 2020-04-03
        • 2018-10-23
        • 1970-01-01
        • 2016-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多