Bert Gunter gunter.berton at gene.com writes:
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
Sergio:
Fair question.
Unfair answer: My personal hangup. To my taste, get() makes R like a
macro language instead of doing functional programming. force() makes
me nervous about how I'm passing arguments. I won't attempt to defend
either of these claims, so feel free to dismiss.
Cheers,
Bert
On Jun 5, 2013, at 3:53, Julio Sergio julioser...@gmail.com wrote:
I want to generate specific gamma distribution functions, given fixed
parameters.
This is I have k, and theta, say
k - 32.2549 # shape
theta - 26.32809 # scale
# I have an auxiliary function that
Hello,
If in faux we ?force the return value, the bug is gone.
faux - function(c) {
f - function (x) get(paste0(c,gamma))(x,k,scale=theta)
force(f)
f
}
Hope this helps,
Rui Barradas
Em 05-06-2013 07:13, Michael Weylandt escreveu:
On Jun 5, 2013, at 3:53, Julio
Hello,
My solution works but it is incorrect. We should force the argument 'c',
not the return value. Like said in the help page for force. Which I've
only read after my first post. The following way makes much more sense
and is a bit shorter.
faux - function(c) {
force(c)
Rui Barradas ruipbarradas at sapo.pt writes:
My solution works but it is incorrect. We should force the argument 'c',
faux - function(c) {
force(c)
function (x) get(paste0(c,gamma))(x,k,scale=theta)
}
Thanks a lot, Rul. I think I have to learn a bit more about lazy evaluation
Rui et al.
Certainly correct.
However, I think the use of force() and similar should be avoided if
possible, as computing on the language can be tricky. So here is an
alternative formulation that avoids it:
faux - function(c){
f - get(paste0(c,gamma)) ## evaluation of c is forced here
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]
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 -
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
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]
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
I want to generate specific gamma distribution functions, given fixed
parameters.
This is I have k, and theta, say
k - 32.2549 # shape
theta - 26.32809 # scale
# I have an auxiliary function that produces funcions according to
# a given character (this is to have either
13 matches
Mail list logo