title: R: check mandatory/required arguments are provided
body: <p>I'd like to be able to have a way to check all arguments without defaults are specified when calling a function in R. This seems like a sensible thing to do to me, as it avoids a function failing later on (which could be after a lot of processing) when a value is found to be missing. </p>
  
  <p>One way to accomplish this task would be to write a <code>if (missing(arg))</code> statement for each argument, but this requires keeping the arguments for the function and the above statements consistent with each other, so I'm looking for a better solution.</p>
  
  <p>At present I use the following function, which works in most cases, but not all. </p>
  
  <pre><code># check for required arguments by getting arguments for the
  # definition of the calling function and comparing to the arguments 
  # in the function call
  check_required_args &lt;- function () {
      def    &lt;- sys.function(-1)
      f_args &lt;- formals(def)
      f_args &lt;- f_args[sapply(f_args, is.name)]  # remove arguments with defaults
      f_args &lt;- names(f_args)
      f_args &lt;- f_args[f_args != '...'] # remove ellipsis argument if present
  
      call   &lt;- match.call(definition=def, call=sys.call(-1))
      f_name &lt;- call[1]
      c_args &lt;- names(as.list(call[-1]))
  
      for(n in f_args) {
          if (!n %in% c_args) {
              stop("Argument '", n, "' missing from call to function ", 
                  f_name, "()", call.=FALSE)
          }
      }
  }
  
  f &lt;- function(a, b, c=2) check_required_args()
  f(a=1) # should fail (missing argument b)
  f(2, 3) # should work
  f(2, c=5) # should fail (missing argument b)
  f(2, 3, 4) # should work
  
  
  f &lt;- function(a, b, ...) f2(a, b, ...)
  f2 &lt;- function(a, b, c, ...) check_required_args()
  
  f2(a=1, b=2, c=3) # should work
  f2(a=1, b=2) # should fail (missing argument c for function f2)
  f(a=1, b=2, c=3) # should work
  f(a=1, b=2) # should fail  (missing argument c for function f2)
  </code></pre>
  
  <p>Can this function be improved to work in all of these cases? If not is there a more appropriate solution to perform this check?</p>