R/MatchArgs.R

#' @export
MatchArgs <- function (arg, choices, several.ok = FALSE, useFirstAsDefault = FALSE) {

  # Added this check to base version
  if (missing(arg) && useFirstAsDefault){
    return(choices[1L])
  }


  # The rest of this is just a repeat of the base function, though I will
  # want to add better errror messaging

  if (missing(choices)) {
    formal.args <- formals(sys.function(sys.parent()))
    choices <- eval(formal.args[[as.character(substitute(arg))]])
  }
  if (is.null(arg))
    return(choices[1L])
  else if (!is.character(arg))
    stop("'arg' must be NULL or a character vector")
  if (!several.ok) {
    if (identical(arg, choices))
      return(arg[1L])
    if (length(arg) > 1L)
      stop("'arg' must be of length 1")
  }
  else if (length(arg) == 0L)
    stop("'arg' must be of length >= 1")
  i <- pmatch(arg, choices, nomatch = 0L, duplicates.ok = TRUE)
  if (all(i == 0L))
    stop(gettextf("'arg' should be one of %s", paste(dQuote(choices),
                                                     collapse = ", ")), domain = NA)
  i <- i[i > 0L]
  if (!several.ok && length(i) > 1)
    stop("there is more than one match in 'match.arg'")
  choices[i]
}
mikebesso/two.laws.big.bang documentation built on May 29, 2019, 5:42 a.m.