Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function default arguments and named values

Tags:

function

r

Let's say I have an R function in which the arguments can be a one of a few predefined named values (one of which is the default) or a custom character vector. How should I implement this without relying on magic value names or another flag?

#allow use of predefined subsets or pass their own list bratPack<-function(members='CORE',...){   if (members=='CORE')     members<-c('Emilio Estevez','Anthony Michael Hall','Rob Lowe','Andrew McCarthy','Demi Moore','Judd Nelson','Molly Ringwald','Ally Sheedy')   else if (members=='ALL')     members<-c('Emilio Estevez','Anthony Michael Hall','Rob Lowe','Andrew McCarthy','Demi Moore','Judd Nelson','Molly Ringwald','Ally Sheedy','James Spader','Robert Downey, Jr.','Jon Cryer', 'John Cusack', 'Kevin Bacon', 'Jami Gertz', 'Mary Stuart Masterson', 'Matthew Broderick', 'Sean Penn', 'Kiefer Sutherland')   ... } 
like image 637
Jeremy Leipzig Avatar asked Jan 13 '11 17:01

Jeremy Leipzig


People also ask

What are default arguments in functions?

A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn't provide a value for the argument.

Can functions have default arguments?

In C++ programming, we can provide default values for function parameters. If a function with default arguments is called without passing arguments, then the default parameters are used. However, if arguments are passed while calling the function, the default arguments are ignored.

What are default values in a function?

Default values indicate that the function argument will take that value if no argument value is passed during the function call. The default value is assigned by using the assignment(=) operator of the form keywordname=value.

What are the three types of function arguments?

5 Types of Arguments in Python Function Definition:keyword arguments. positional arguments. arbitrary positional arguments.


2 Answers

From your example we have the choice of "CORE" and "ALL". If those are the two options, then we specify them in the function definition for the argument 'members'. E.g.:

foo <- function(x, members = c("CORE", "ALL")) {     ## do something } 

That function definition sets up the allowed values for argument 'members', with a default of "CORE" as this is the first named option.

The code that one uses within the function body is match.arg(), as @Joris has already mentioned, but because we have set the function up as above, we can simply the usage to just match.arg(members).

So we can write foo as:

foo <- function(x, members = c("CORE", "ALL")) {     ## evaluate choices     members <- match.arg(members)     ## do something     print(members) } 

Which we use like this:

> foo() [1] "CORE" > foo(members = "CORE") [1] "CORE" > foo(members = "ALL") [1] "ALL" > foo(members = "3rdRate") Error in match.arg(members) : 'arg' should be one of “CORE”, “ALL” 

Notice the behaviour when we supply an string not included in the set of options. We get an intuitive error message, all because we set up the options in the function arguments.

like image 114
Gavin Simpson Avatar answered Sep 29 '22 10:09

Gavin Simpson


I'd use some constant dataframe somewhere in the package:

.mdata <- data.frame(     CORE= c(TRUE,FALSE,TRUE),     OLD = c(TRUE,TRUE,FALSE),     ALL = c(TRUE,TRUE,TRUE),     row.names=c("John Doe", "Jan Janssen", "Piet Peters") )  bratPack<-function(members='CORE',...){   m.tmp <- try(          match.arg(members,names(.mdata),several.ok=T),          silent=T)     if(!is(m.tmp,"try-error"))     members <- rownames(.mdata)[.mdata[[members]]]    print(members) }  > bratPack('CORE') [1] "John Doe"    "Piet Peters"  > bratPack('Jan Janssen') [1] "Jan Janssen"  > bratPack(c("John Doe","Dick Dickers")) [1] "John Doe"     "Dick Dickers" 
like image 33
Joris Meys Avatar answered Sep 29 '22 10:09

Joris Meys