Trying to build up functions with its names by means of lapply
Thanks, Bill. Clearly something I missed. I appreciate your filling in
that crack.
Another equivalent way to do it?
f2 <- function(c,nm = "gamma",...)
{
probFunc <- paste0(c,nm)
more <- list(...)
function(x)do.call(probFunc,c(x,more))
}
This avoids the explicit use of get() and force(), I believe, but are
there problems here I'm missing?
-- Cheers,
Bert
On Wed, Jun 5, 2013 at 1:46 PM, William Dunlap <wdunlap at tibco.com> wrote:
You will want to force the evaluation of the ... arguments, just as you are already
forcing the evaluation of 'c', before returning your function. E.g., compare the
following two:
# your 'faux', renamed and enhanced a bit
f0 <- function (c, nm = "gamma", ...)
{
probFunc <- getFunction(paste0(c, nm))
function(x) probFunc(x, ...)
}
# the above but adding a call to force the evaluation of the ... argument before returning the function
f1 <- function (c, nm = "gamma", ...)
{
probFunc <- getFunction(paste0(c, nm))
force(list(...))
function(x) probFunc(x, ...)
}
# Now use mapply to make a list of functions with various parameters
z0 <- mapply(c=c("p","d"), nm="norm", mean=c(1,-1), FUN=f0)
z1 <- mapply(c=c("p","d"), nm="norm", mean=c(1,-1), FUN=f1)
z0[[1]](0.1) # expect same result as pnorm(0.1, mean=1)
# [1] 0.8643339 # bad
z1[[1]](0.1) # expect same result as pnorm(0.1, mean=1)
# [1] 0.1840601 # good
# for reference:
pnorm(0.1, mean=1)
# [1] 0.1840601
pnorm(0.1, mean=-1)
# [1] 0.8643339
You don't have to say 'force(list(...))' to force their evaluation. You could use
'junk <- list(...)' or 'junk <- c(...)', but using the force function should indicate
that you are only doing this to force the evaluation of the argument at this point,
not because you intend to use the result of list(...) or c(...).
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 1:26 PM To: r-help at stat.math.ethz.ch Subject: Re: [R] Trying to build up functions with its names by means of lapply Bert Gunter <gunter.berton <at> gene.com> writes:
faux <- function(c, nm = "gamma",...){
f <- get(paste0(c,nm))
function(x)f(x,...)
}
This could be called with:
xgam <- lapply(c("p","d"), faux, shape=k, scale=theta)
xgam[[1]](1000)
[1] 0.8710477
xgam[[2]](1000)
[1] 0.001265311
Excellent, Bert, and thanks a lot for your contribution! In fact, I was planning to write a separate functions producer for each distribution. This really saves me a lot of work! -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.
______________________________________________ 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.
Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm