title: Optional command line argument in R script
body: <p>I want to have an optional positional command-line argument to my R script and assumed this would be OK:</p>
  
  <pre><code>args &lt;- commandArgs(trailingOnly = TRUE)
  infile &lt;- args[1]
  outfile &lt;- tryCatch(args[2],
      error=function(cond) {
          "default.txt"
      }
  )
  </code></pre>
  
  <p>But R doesn't error when an array is accessed beyond its length:</p>
  
  <pre><code>&gt; x &lt;- c(1,2,3)
  &gt; x[4]
  [1] NA
  </code></pre>
  
  <p>I can't use <code>missing</code> for some reason:</p>
  
  <pre><code>&gt; missing(args[2])
  Error in missing(args[2]) : invalid use of 'missing'
  </code></pre>
  
  <p>So how can I test for a missing second command-line argument and insert a default if necessary?</p>