Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All valid values of an argument of a function in R

Tags:

Suppose we have an R function whose arguments must be selected out of a finite set of elements. Like qplot(..., geom=""). And geom can take only some values, like bar or point.

How can I find out all the valid values the argument of a given function may take? Apart from docs or Internet, which often miss all possible values. Perhaps, some R function can help?

like image 298
Anton Tarasenko Avatar asked Dec 03 '13 13:12

Anton Tarasenko


People also ask

How do you find the argument of a function in R?

args() function in R Language is used to get the required arguments by a function. It takes function name as arguments and returns the arguments that are required by that function.

What are the arguments of a function in R?

Arguments are the parameters provided to a function to perform operations in a programming language. In R programming, we can use as many arguments as we want and are separated by a comma. There is no limit on the number of arguments in a function in R.

How do you program a function with default values for their arguments in R?

Adding a Default Value in R You can specify default values for any disagreements in the argument list by adding the = sign and default value after the respective argument. You can specify a default value for argument mult to avoid specifying mult=100 every time.

What does function () do in R?

In R, a function is an object so the R interpreter is able to pass control to the function, along with arguments that may be necessary for the function to accomplish the actions. The function in turn performs its task and returns control to the interpreter as well as any result which may be stored in other objects.


1 Answers

If the function of interest is defined like

f <- function(a = c("foo","bar")) {
    match.arg(a)
}

i.e. when the options are defined as a vector to be later checked with match.arg function, then you could use the formals function which would give you a list of arguments with the values like in the following example

> formals(f)
$a
c("foo", "bar")

Otherwise I don't think it is possible to get all valid argument values without RTFSing.

like image 79
Pavel Obraztcov Avatar answered Oct 05 '22 22:10

Pavel Obraztcov