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 <mber...@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: mber...@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.ber...@cd.ieo.es
> phone: +34 956 294189
> Fax: +34 956 294232
>
> ______________________________________________
> R-help@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.
>



-- 

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag
______________________________

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to