Skip to content

Instantly share code, notes, and snippets.

@bschilder
Last active January 12, 2023 12:53
Show Gist options
  • Save bschilder/f02a5b564977f52fd665728a22c0d005 to your computer and use it in GitHub Desktop.
Save bschilder/f02a5b564977f52fd665728a22c0d005 to your computer and use it in GitHub Desktop.

Function to check whether the user-supplied argument is the default.

is_default <- function(fun,
                       arg,
                       arg_value,
                       use_names=TRUE){
  fmls <- formals(get(fun))
  if(arg %in% names(fmls)){
    is_def <- if(use_names){
      all(arg_value == names(fmls[[arg]]))
    } else {
      all(arg_value == fmls[[arg]])
    } 
    return(is_def)
  } else {
    wrn <- paste0(arg," is not an argument in ",fun,".")
    warning(wrn)
    return(NULL)
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment