Я использую пакет optparse в R для создания сценария с аргументами флага:
#!/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"
, что возвращает:
[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 вместо строковых значений?