[R] Trying to build up functions with its names by means of lapply

William Dunlap wdunlap at tibco.com
Wed Jun 5 23:26:37 CEST 2013


You can put print statements into the arguments given to a function to more directly
see when they get evaluated.  E.g., 

> z <- f0(c={ cat("Evaluating 'c' argument\n"); "p"}, nm="norm", mean={ cat("Evaluating 'mean' argument\n"); 1.2 })
Evaluating 'c' argument
> z(1:5)
Evaluating 'mean' argument
[1] 0.4207403 0.7881446 0.9640697 0.9974449 0.9999277
>
> z <- f1(c={ cat("Evaluating 'c' argument\n"); "p"}, nm="norm", mean={ cat("Evaluating 'mean' argument\n"); 1.2 })
Evaluating 'c' argument
Evaluating 'mean' argument
> z(1:5)
[1] 0.4207403 0.7881446 0.9640697 0.9974449 0.9999277
> pnorm(1:5, mean=1.2)
[1] 0.4207403 0.7881446 0.9640697 0.9974449 0.9999277

where f0 does not call force(list(...)) and f1 does.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Julio Sergio
> Sent: Wednesday, June 05, 2013 2:11 PM
> To: r-help at stat.math.ethz.ch
> Subject: Re: [R] Trying to build up functions with its names by means of lapply
> 
> William Dunlap <wdunlap <at> tibco.com> writes:
> 
> > f1 <- function (c, nm = "gamma", ...)
> > {
> >     probFunc <- getFunction(paste0(c, nm))
> >     force(list(...))
> >     function(x) probFunc(x, ...)
> > }
> >
> > Bill Dunlap
> > Spotfire, TIBCO Software
> > wdunlap tibco.com
> >
> 
> Thanks a lot William!, this really enhances my knowledge of the language.
> 
>  -Sergio.
> 
> ______________________________________________
> 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