Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Function Arguments without Defaults

Tags:

r

How can I reliably tell if formal function arguments have a default value from outside the function?

In the below example, I'd like to ensure that the test below will not fail to find when an argument has or does not have a default value:

myfun <- function(a, b=1, ...) {}
formals(myfun)
for (n in names(formals(myfun))) {
  if (is.name(formals(myfun)[[n]])) {
    cat(n, "has no default value\n")
  } else {
    cat(n, "has a default value:", formals(myfun)[[n]], "\n")
  }
}

is.name is the best test that I've come up with, but I'd prefer to see both that it is a name and that it is empty (or better yet a more definitive test like missing would provide inside the function).

like image 874
Bill Denney Avatar asked Jan 22 '16 03:01

Bill Denney


People also ask

Which function has no default parameter?

Constructors cannot have default parameters.

Can a function argument have default value?

Any number of arguments in a function can have a default value.

How do I make function arguments optional in Python?

You can define Python function optional arguments by specifying the name of an argument followed by a default value when you declare a function. You can also use the **kwargs method to accept a variable number of arguments in a function. To learn more about coding in Python, read our How to Learn Python guide.

How do I fix non-default argument follows default argument in Python?

The Python "SyntaxError: non-default argument follows default argument" occurs when we define a function with a positional parameter that follows a default parameter. To solve the error, make sure to specify all default parameters after the positional parameters of the function.


1 Answers

You can do an empty string comparison to test if each value is empty:

myfun <- function(a, b=1, ...) {}
formals(myfun)
for (n in names(formals(myfun))) {
  if (formals(myfun)[[n]] == "") {
    cat(n, "has no default value\n")
  } else {
    cat(n, "has a default value:", formals(myfun)[[n]], "\n")
  }
}

a has no default value
b has a default value: 1
... has no default value

UPDATED FOR RARE BUT POSSIBLE CASE OF WANTING TO TREAT "" AS DEFINED DEFAULT OF EMPTY STRING:

myfun <- function(a, b=1, c="", ...) {}
formals(myfun)
for (n in names(formals(myfun))) {
  if (!nzchar(formals(myfun)[[n]]) & is.name(formals(myfun)[[n]])) {
    cat(n, "has no default value\n")
  } else {
    cat(n, "has a default value:", formals(myfun)[[n]], "\n")
  }
}

a has no default value
b has a default value: 1
c has a default value:
... has no default value

EXTRA EDIT FOR SAKE OF COMPLETENESS: To actually display the empty quote characters in the result and define formals(myfun) instead of calling it over and over:

myfun <- function(a, b=1, c="", ...) {}
myfun_args <- formals(myfun)
for (n in names(myfun_args)) {
  if (!nzchar(myfun_args[[n]]) & is.name(myfun_args[[n]])) {
    cat(n, "has no default value\n")
  } else {
        if (!nzchar(myfun_args[[n]])) 
            cat(n, "has a default value:", dQuote(myfun_args[[n]]), "\n")
        else
            cat(n, "has a default value:", myfun_args[[n]], "\n")
  }

}

a has no default value
b has a default value: 1
c has a default value: “”
... has no default value

like image 183
Mekki MacAulay Avatar answered Sep 28 '22 16:09

Mekki MacAulay