如何检查参数是否已正确传递到Windows上的Rscript [英] How to check if arguments have been correctly passed to Rscript on Windows

查看:130
本文介绍了如何检查参数是否已正确传递到Windows上的Rscript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个R脚本,它使用Rscript:输入文件名,它是否有一个标题(值是'header'或'no_header',和一个正整数因此,当我以这种方式运行时:

I'm trying to write an R script that takes in 3 arguments when run with Rscript: input file name, whether it has a header or not (values are 'header' or 'no_header', and a positive integer (the number of replacements; its for a bootstrap application). So, when I run it this way:

Rscript bootstrapWithReplacement.R survival.csv header 50

它应该在运行之前检查:
1)脚本确实取3个参数;
2)第一个参数是否为文件;
3)第二个参数是否有'header'或'no_header'值,
4)如果传递的数字是正整数。

it should, before running, check if: 1) The script indeed took in 3 parameters; 2) whether the first parameter is a file; 3) whether the second parameter has a 'header' or 'no_header' value, and 4) if the number passed is a positive integer.

这里是我的代码到目前为止:

Here is my code so far:

pcArgs      <- commandArgs()
snOffset    <- grep('--args', pcArgs)
inputFile <- pcArgs[snOffset+1]
headerSpec <- pcArgs[snOffset+2]    ## header/no_header
numberOfResamples <- pcArgs[snOffset+3] ## positive integer

check.integer <- function(N){
    !length(grep("[^[:digit:]]", as.character(N)))
}

if (!file_test("-f",inputFile)) {stop("inputFile not defined. Proper use: Rscript bootstrapWithReplacementFile.R survival.csv header 50.")}
if (!exists("headerSpec")) {stop("headerSpec not defined. Proper use: Rscript bootstrapWithReplacementFile.R survival.csv header 50.")} 
if (!exists("numberOfResamples")) {stop("numberOfResamples not defined. Proper use: Rscript bootstrapWithReplacementFile.R survival.csv header 50.")} 

if ((headerSpec != 'header') == TRUE & (headerSpec != 'no_header') == TRUE) {stop("headerSpec not properly defined. Correct values: 'header' OR 'no_header'.")}
if (check.integer(numberOfResamples) != TRUE | (numberOfResamples>0) != TRUE) {stop("numberOfResamples not properly defined. Must be an integer larger than 0.")}


if (headerSpec == 'header') {
    inputData<-read.csv(inputFile)
    for (i in 1:numberOfResamples) {write.csv(inputData[sample(nrow(inputData),replace=TRUE),], paste("./bootstrap_",i,"_",inputFile,sep=""), row.names=FALSE)}
}

if (headerSpec == 'no_header') {
    inputData<-read.table(inputFile,header=FALSE)
    for (i in 1:numberOfResamples) {write.table(inputData[sample(nrow(inputData),replace=TRUE),], paste("./bootstrap_",i,"_",inputFile,sep=""),
 sep=",", row.names=FALSE, col.names=FALSE)}
}

我的问题是,检查文件的存在是否有效, 't。

My problem is, the check for the existence of a file works, but for the header or integer don't.

此外,我如何在开始检查是否所有三个参数都已传递?

Also, how can I, in the beginning, check if all three arguments have been passed?

推荐答案

如Vincent所说,你应该使用 trailingOnly commandArgs 以简化操作。

正如Konrad所说,永远不会直接与 TRUE FALSE

此外,使用断言进行断言。

As Vincent said, you should use the trailingOnly argument to commandArgs to simplify things.
As Konrad said, never, ever, ever compare directly to TRUE and FALSE.
Also, use assertive for doing assertions.

library(assertive)
library(methods)    
cmd_args <- commandArgs(TRUE)

if(length(cmd_args) < 3)
{
  stop("Not enough arguments. Please supply 3 arguments.")
}
inputFile <- cmd_args[1]
if (!file_test("-f", inputFile)) 
{
  stop("inputFile not defined, or not correctly named."
}
headerSpec <- match.arg(cmd_args[2], c("header", "no_header"))
numberOfResamples <- as.numeric(cmd_args[3])
assert_all_numbers_are_whole_numbers(numberOfResamples)
assert_all_are_positive(numberOfResamples)

message("Success!")

这篇关于如何检查参数是否已正确传递到Windows上的Rscript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆