optparse NA 默认不被命令行参数覆盖
optparse NA default not being overwritten by command-line argument
考虑以下脚本:
library(optparse)
option_list <- list(
make_option(c('--sample_p'), default=NA, dest='sample_p', help='Sample probability of inclusion if randomly sampling records')
)
args <- parse_args(OptionParser(option_list=option_list))
print(args)
if(!is.na(args$sample_p)){
print('Chose to sample')
}else{
print('Did not choose to sample')
}
我想向脚本传递一个浮点值,存储在 args$sample_p
中。我尝试了以下方法,但两种方法都没有让我存储 0.05
:
$ Rscript test.R --sample_p 0.05
$sample_p
[1] NA
$help
[1] FALSE
[1] "Did not choose to sample"
$ Rscript test.R --sample_p=0.05
Warning message:
In getopt(spec = spec, opt = args) :
long flag sample_p given a bad argument
$sample_p
[1] NA
$help
[1] FALSE
[1] "Did not choose to sample"
但是,如果我将 sample_p
的默认值改为一个数字,而不是 NA
,它会起作用:
library(optparse)
option_list <- list(
make_option(c('--sample_p'), default=0, dest='sample_p', help='Sample probability of inclusion if randomly sampling records')
)
args <- parse_args(OptionParser(option_list=option_list))
print(args)
if(!is.na(args$sample_p)){
print('Chose to sample')
}else{
print('Did not choose to sample')
}
$ Rscript test.R --sample_p 0.05
$sample_p
[1] 0.05
$help
[1] FALSE
[1] "Chose to sample"
为什么 NA
作为默认值会导致问题?
看起来您需要使用 NA_real_
来指定在此上下文中使用的 NA 类型 (https://stat.ethz.ch/R-manual/R-devel/library/base/html/NA.html)。
option_list <- list(
make_option(c('--sample_p'), default=NA_real_, dest='sample_p', help='Sample probability of inclusion if randomly sampling records')
)
这主要是因为 type
在 make_option()
中的处理方式。
考虑以下脚本:
library(optparse)
option_list <- list(
make_option(c('--sample_p'), default=NA, dest='sample_p', help='Sample probability of inclusion if randomly sampling records')
)
args <- parse_args(OptionParser(option_list=option_list))
print(args)
if(!is.na(args$sample_p)){
print('Chose to sample')
}else{
print('Did not choose to sample')
}
我想向脚本传递一个浮点值,存储在 args$sample_p
中。我尝试了以下方法,但两种方法都没有让我存储 0.05
:
$ Rscript test.R --sample_p 0.05
$sample_p
[1] NA
$help
[1] FALSE
[1] "Did not choose to sample"
$ Rscript test.R --sample_p=0.05
Warning message:
In getopt(spec = spec, opt = args) :
long flag sample_p given a bad argument
$sample_p
[1] NA
$help
[1] FALSE
[1] "Did not choose to sample"
但是,如果我将 sample_p
的默认值改为一个数字,而不是 NA
,它会起作用:
library(optparse)
option_list <- list(
make_option(c('--sample_p'), default=0, dest='sample_p', help='Sample probability of inclusion if randomly sampling records')
)
args <- parse_args(OptionParser(option_list=option_list))
print(args)
if(!is.na(args$sample_p)){
print('Chose to sample')
}else{
print('Did not choose to sample')
}
$ Rscript test.R --sample_p 0.05
$sample_p
[1] 0.05
$help
[1] FALSE
[1] "Chose to sample"
为什么 NA
作为默认值会导致问题?
看起来您需要使用 NA_real_
来指定在此上下文中使用的 NA 类型 (https://stat.ethz.ch/R-manual/R-devel/library/base/html/NA.html)。
option_list <- list(
make_option(c('--sample_p'), default=NA_real_, dest='sample_p', help='Sample probability of inclusion if randomly sampling records')
)
这主要是因为 type
在 make_option()
中的处理方式。