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

Gabor Grothendieck ggrothendieck at gmail.com
Sat Jun 27 03:04:15 CEST 2009


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




More information about the R-help mailing list