optparse 在 R 脚本中生成 NULL 值

optparse producing NULL values in R script

我正在使用 R 中的 optparse 包来创建带有标志参数的脚本:

#!/usr/bin/env Rscript
suppressPackageStartupMessages(require(optparse))

option_list = list(
  make_option(c("-f", "--file"), action="store", default=getwd(), type='character',
              help="Input parent directory with subdirectories of element xray data to be stitched. The element names should be abbreviated, i.e. 'Ca' for calcium."),
  make_option(c("-o", "--out"), action="store", default=getwd(), type='character',
              help="Output file directory. This is where your x-ray raster brick and output figures will be saved."),
  make_option(c("-n", "--name"), action="store", default=NA, type='character',
              help="Optional name for output files."),
  make_option(c("-s", "--sem"), action="store", default=NA, type='character',
              help="SEM image file directory."),
  make_option(c("-c", "--coords"), action="store", default=NA, type='character',
              help="Tab-delimited file of xy coordinates for each image. A third column should denote stitching positions that correspond to the file names for each image."),
  make_option(c("-t", "--ignore"), action="store", default="-?(?<![Kα1||Kα1_2])\d+", type='character',
              help="Optional regex pattern to extract position IDs from each file name that corresponds to positions in the xy file. The default searches for numbers that appear after 'Kα1' or 'Kα2'. Numbers can include signs, i.e. -1 is acceptable."),
  make_option(c("-i", "--image"), action="store", default="*", type='character',
              help="Optional regex pattern of x-ray image formats to select for stitching, i.e. '.tif'."),
  make_option(c("-m", "--sem-image"), action="store", default="*", type='character',
              help="Optional regex pattern of SEM image formats to select for stitching, i.e. '.tif'. You do not need to specify this unless you are generating a PDF output."),
  make_option(c("-x", "--exclude"), action="store", default=NA, type='character',
              help="Optional regex pattern of x-ray file directories to exclude from stitiching, i.e. the element your sample was coated with."),
  make_option(c("-d", "--drop"), action="store", default=NA, type='character',
              help="Optional regex pattern of files to exclude from x-ray data stitching."),
  make_option(c("-y", "--exclude-sem"), action="store", default=NA, type='character',
              help="Optional regex pattern of files to exclude from SEM image stitiching. You do not need to specify this unless you are generating a PDF output."),
  make_option(c("-v", "--verbose"), action="store_true", default=TRUE,
              help="Print updates to console [default %default]."),
  make_option(c("-q", "--quiet"), action="store_false", dest="verbose",
              help="Do not print anything to the console."),
  make_option(c("-p", "--pdf"), action="store", default=TRUE,
              help="Generate PDF of x-ray brick colored by element superimposed on the SEM image, default is TRUE [default %default].")  
)
opt = parse_args(OptionParser(option_list=option_list))

print("f:")
print(opt$f)
print("o:")
print(opt$o) 
print("n:")
print(opt$n)
print("c:")
print(opt$c) 
print("s:")
print(opt$s)
print("i:")
print(opt$i)
print("m:")
print(opt$m)
print("x:")
print(opt$x)
print("d:")
print(opt$d)
print("y:")
print(opt$y)
print("t:")
print(opt$t)
print("p:")
print(opt$p)

break

但是,当我测试检查分配给每个变量的值时,有些变量返回 NULL,我不明白为什么。这就是我在命令行中输入的内容:

Rscript dataStitchR.R -f "f test" -o "o test" -n "n test" -c "c test" -s "s test" -i "i test" -m "m test" -x 'x test' -d "d test" -y "y test" -p FALSE  -t "t test"

哪个returns:

[1] "f:"
[1] "f test"
[1] "o:"
[1] "o test"
[1] "n:"
[1] "n test"
[1] "c:"
[1] "c test"
[1] "s:"
NULL
[1] "i:"
NULL
[1] "m:"
NULL
[1] "x:"
NULL
[1] "d:"
[1] "d test"
[1] "y:"
NULL
[1] "t:"
NULL
[1] "p:"
[1] FALSE

为什么分配给 -s、-i、-m、-x、-t 和 -y 的值返回 NULL 而不是字符串值?

好吧,您似乎将这些值的默认值设置为 NA。这意味着没有默认值。因此,没有为这些对象存储任何值。如果您要将命令行参数传递给这些标志,那么它们就不会为空。您可以将默认值设置为 "NA"(注意引号),然后您将得到一个 none 空值。

标志名称似乎有问题,可能在 optparse 的其他地方使用了它们。当我更改标志时,测试通过:

#!/usr/bin/env Rscript
suppressPackageStartupMessages(require(optparse))

option_list = list(
  make_option(c("-f", "--file"), action="store", default=getwd(), type='character',
              help="Input parent directory with subdirectories of element xray data to be stitched. The element names should be abbreviated, i.e. 'Ca' for calcium."),
  make_option(c("-o", "--out"), action="store", default=getwd(), type='character',
              help="Output file directory. This is where your x-ray raster brick and output figures will be saved."),
  make_option(c("-n", "--name"), action="store", default=NA, type='character',
              help="Optional name for output files."),
  make_option(c("-b", "--base-images"), action="store", default=NA, type='character',
              help="SEM image file directory."),
  make_option(c("-c", "--coords"), action="store", default=NA, type='character',
              help="Tab-delimited file of xy coordinates for each image. A third column should denote stitching positions that correspond to the file names for each image."),
  make_option(c("-u", "--use-positions"), action="store", default="-?(?<![Kα1||Kα1_2])\d+", type='character',
              help="Optional regex pattern to extract position IDs from each file name that corresponds to positions in the xy file. The default searches for numbers that appear after 'Kα1' or 'Kα2'. Numbers can include signs, i.e. -1 is acceptable."),
  make_option(c("-z", "--z-format"), action="store", default="*", type='character',
              help="Optional regex pattern of x-ray image formats to select for stitching, i.e. '.tif'."),
  make_option(c("-m", "--make"), action="store", default="*", type='character',
              help="Optional regex pattern of SEM image formats to select for stitching, i.e. '.tif'. You do not need to specify this unless you are generating a PDF output."),
  make_option(c("-a", "--all-exclude"), action="store", default=NA, type='character',
              help="Optional regex pattern of x-ray file directories to exclude from stitiching, i.e. the element your sample was coated with."),
  make_option(c("-d", "--drop"), action="store", default=NA, type='character',
              help="Optional regex pattern of files to exclude from x-ray data stitching."),
  make_option(c("-y", "--y-exclude"), action="store", default=NA, type='character',
              help="Optional regex pattern of files to exclude from SEM image stitiching. You do not need to specify this unless you are generating a PDF output."),
  make_option(c("-v", "--verbose"), action="store_true", default=TRUE,
              help="Print updates to console [default %default]."),
  make_option(c("-q", "--quiet"), action="store_false", dest="verbose",
              help="Do not print anything to the console."),
  make_option(c("-p", "--pdf"), action="store", default=TRUE,
              help="Generate PDF of x-ray brick colored by element superimposed on the SEM image, default is TRUE [default %default].")  
)
opt = parse_args(OptionParser(option_list=option_list))

print("f:")
print(opt$f)
print("o:")
print(opt$o) 
print("n:")
print(opt$n)
print("b:")
print(opt$b)
print("c:")
print(opt$c) 
print("u:")
print(opt$u)
print("z:")
print(opt$z)
print("m:")
print(opt$m)
print("a:")
print(opt$a)
print("d:")
print(opt$d)
print("y:")
print(opt$y)
print("p:")
print(opt$p)
break

我在命令行输入的内容:

Rscript dataStitchR.R -f "test" -o "test" -n "test" -b "test" -c "test" -u "test" -z "test" -m "test" -a "test" -d "test" -y "test" -p FALSE

以及正确的输出:

[1] "f:"
[1] "test"
[1] "o:"
[1] "test"
[1] "n:"
[1] "test"
[1] "c:"
[1] "test"
[1] "z:"
[1] "test"
[1] "m:"
[1] "test"
[1] "x:"
[1] "test"
[1] "d:"
[1] "test"
[1] "y:"
[1] "test"
[1] "u:"
[1] "test"
[1] "p:"
[1] FALSE
[1] "b:"
[1] "test"

您刚刚被 R 的列表名称部分匹配迷惑了。因为有两个以 s 开头的选项,所以您需要拼出一些独特的东西。

如果打印出 out 的结构,您会看到:

List of 14
 $ file       : chr "f test"
 $ out        : chr "o test"
 $ name       : chr "n test"
 $ sem        : chr "s test"
 $ coords     : chr "c test"
 $ ignore     : chr "t test"
 $ image      : chr "i test"
 $ sem-image  : chr "m test"
 $ exclude    : chr "x test"
 $ drop       : chr "d test"
 $ exclude-sem: chr "y test"
 $ verbose    : logi TRUE
 $ pdf        : logi FALSE
 $ help       : logi FALSE

注意 semsem-image。如果我使用此代码

print("f:")
print(opt$f)
print("o:")
print(opt$o) 
print("n:")
print(opt$n)
print("c:")
print(opt$c) 
print("s:")
print(opt$sem)
print("i:")
print(opt$image)
print("m:")
print(opt$"sem-image")
print("x:")
print(opt$exclude)
print("d:")
print(opt$d)
print("y:")
print(opt$"exclude-sem")
print("t:")
print(opt$ignore)
print("p:")
print(opt$p)

我明白了

[1] "f:"
[1] "f test"
[1] "o:"
[1] "o test"
[1] "n:"
[1] "n test"
[1] "c:"
[1] "c test"
[1] "s:"
[1] "s test"
[1] "i:"
[1] "i test"
[1] "m:"
[1] "m test"
[1] "x:"
[1] "x test"
[1] "d:"
[1] "d test"
[1] "y:"
[1] "y test"
[1] "t:"
[1] "t test"
[1] "p:"
[1] FALSE