[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 22:55:51 CEST 2009


That was indeed what i was looking for (thanks also for the currying cite). I 
also wanted flexibility on the number of arguments you can pass to the 
function, which can be achieved by:

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

mysecond.fun <-
  function(dumb1, dumb2, cc=myfun, cc.args=list(a=2, b=5)){
    fun.args <- formals(cc)
    fun.args[match(names(cc.args), names(fun.args))] <- cc.args
    formals(cc) <- fun.args
    list(a=dumb1, b=dumb2, cc = cc)
  }

test <- mysecond.fun(1, 2)

myfun(2) ## 59
test$cc(2) ## 9


Thanks both of you for your replies, 

Miguel.


On Friday 26 June 2009 18:20:54 baptiste auguie wrote:
> Is this what you want?
>
> myfun <- function(x, a=19, b=21){ return(a * x + b) }
>
> mysecond.fun <- function(a, b, cc=myfun, cc.args=list(a=2,b=15) ){
> list(a=a, b=b, cc = function(x) cc(x, cc.args$a, cc.args$b))
> }
>
> mysecond.fun(a=1,b=2)$cc(x=12)
>
> It may be that you're after a Curry (*) function, as in,
>
> Curry <-  # from roxygen
> function (f, ..., .left=TRUE)
> {
>     .orig = list(...)
>     function(...){
>     if(.left) {args <- c(.orig, list(...))} else {args <- c(list(...),
> .orig)}
>      do.call(f, args)
>     }
> }
>
> I believe there are some recent discussions on currying in the archives.
>
> (*): http://en.wikipedia.org/wiki/Currying
>
>
> HTH,
>
> baptiste
>
>
>
>
>
> 2009/6/26 Miguel Bernal <mbernal at marine.rutgers.edu>
>
> > 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
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.



-- 
----
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