Miguel Bernal
2009-Jun-26 16:52 UTC
[R] changing default arguments of a function and return the modified function as a result
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
David Winsemius
2009-Jun-26 21:28 UTC
[R] changing default arguments of a function and return the modified function as a result
On Jun 26, 2009, at 12:52 PM, Miguel Bernal wrote:> 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)){^^^^^^^^^^^^^^^^^^ Two things: a) I doubt that is possible in that manner and b) you couldn't possibly get myfun to work, because you are not passing it the correct number of arguments.> return(list(a=a, b=b c=c))^ Third concern, missing comma.> } > > So I would be able to call: > > mysecond.fun$c(x=12)Not in this language. Maybe in some other. Maybe you are in the wrong help list? You should instead provide a specification of the inputs and outputs you expect rather than a hashed up view of how you think R should accept functional arguments. Perhaps something like this: myfun <- function(x, a=19, b=21){ return(a * x + b) } mysecond.fun <- function(x, a, b){ c <- myfun(x, a=2, b=15) list(a=a, b=b, c=c) } > mysecond.fun(x=12, 2 , 3) $a [1] 2 $b [1] 3 $c [1] 39 > # c= 2 * 12 + 15> > 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.David Winsemius, MD Heritage Laboratories West Hartford, CT
baptiste auguie
2009-Jun-26 22:20 UTC
[R] changing default arguments of a function and return the modified function as a result
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@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@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@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]]
Gabor Grothendieck
2009-Jun-27 01:04 UTC
[R] changing default arguments of a function and return the modified function as a result
Look at the source code to the Defaults package. On Fri, Jun 26, 2009 at 12:52 PM, Miguel Bernal<mbernal at marine.rutgers.edu> wrote:> 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. >