optparse producing NULL values in R script

604 Views Asked by At

I'm using the optparse package in R to create a script with flag arguments:

#!/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

However, when I test to check the values assigned to each variable, some are coming back NULL and I can't figure out why. This is what I'm entering it on the command line:

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"

which 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

Why are the values assigned to -s, -i, -m, -x, -t, and -y coming back NULL instead of the string values?

3

There are 3 best solutions below

1
Roman Luštrik On BEST ANSWER

You have just been bamboozled by R's partial matching of list names. Because there are two options that start with an s, you need to spell out something that is unique.

If you print out the structure of out, you'll see this:

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

Notice the sem and sem-image. If I use this code

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)

I get this

[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
1
SKyJim On

Well it seems that for those values you are setting the default value to NA. Which means no default. So then there are no values being stored for those objects. If you were to pass command line arguments to those flags then they would not be null. You could set the default value to "NA" (Notice the quotes) and then you would have a none null value.

0
Caitlin On

There seems to be an issue with the flag names, maybe they are being used elsewhere in optparse. When I change the flags, the tests pass:

#!/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

what I entered on the command line:

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

And the correct output:

[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"