[R] changing default arguments of a function and return the modified function as a result

Miguel Bernal mbernal at marine.rutgers.edu
Fri Jun 26 18:52:11 CEST 2009


Dear R-users, 

I am trying to develop a function that takes another function as an argument, 
changes its default values and returns a list of things, among which the 
initial function with its default arguments changed. An example of what i 
will like to obtain below:

## initial function

myfun <- function(x, a=19, b=21){ return(a * x + b) }

## this is the function i will like to create 
## (does not work as it is written here)

mysecond.fun <- function(a, b, c = myfun(a=2, b=15)){
return(list(a=a, b=b c=c))
}

So I would be able to call:

mysecond.fun$c(x=12)

And this will be equivalent of calling:

myfun(x=12, a=2, b=15 ) ## i.e. i have changed the default values of myfun and
			## stored it in a new function mysecond.fun$c

Any help will be greatly appreciated!

Miguel Bernal.


----
Current address:							
Ocean Modeling group, 			
Institute of Marine and Coastal Sciences
University of Rutgers			
71 Dudley Road, New Brusnkwick, 
New Jersey 08901, USA			
email: mbernal at marine.rutgers.edu
phone: +1 732 932 3692			
Fax: +1 732 932 8578		
---------------------------------------------
Permanent address:
Instituto Español de Oceanografía	
Centro Oceanográfico de Cádiz
Puerto Pesquero, Muelle de Levante, s/n
Apdo. 2609, 11006 Cádiz, Spain
email: miguel.bernal at cd.ieo.es
phone: +34 956 294189
Fax: +34 956 294232




More information about the R-help mailing list