title: How to check if arguments have been correctly passed to Rscript on Windows
body: <p>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:</p>
  
  <pre><code>Rscript bootstrapWithReplacement.R survival.csv header 50
  </code></pre>
  
  <p>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.</p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>pcArgs      &lt;- commandArgs()
  snOffset    &lt;- grep('--args', pcArgs)
  inputFile &lt;- pcArgs[snOffset+1]
  headerSpec &lt;- pcArgs[snOffset+2]    ## header/no_header
  numberOfResamples &lt;- pcArgs[snOffset+3] ## positive integer
  
  check.integer &lt;- 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 &amp; (headerSpec != 'no_header') == TRUE) {stop("headerSpec not properly defined. Correct values: 'header' OR 'no_header'.")}
  if (check.integer(numberOfResamples) != TRUE | (numberOfResamples&gt;0) != TRUE) {stop("numberOfResamples not properly defined. Must be an integer larger than 0.")}
  
  
  if (headerSpec == 'header') {
      inputData&lt;-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&lt;-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)}
  }
  </code></pre>
  
  <p>My problem is, the check for the existence of a file works, but for the header or integer don't. </p>
  
  <p>Also, how can I, in the beginning, check if all three arguments have been passed?</p>
  
  <p>Thanks!</p>