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

2013-06-06 Thread Bert Gunter
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 O

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

2013-06-06 Thread Julio Sergio
Bert Gunter 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 problem

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

2013-06-05 Thread Bert Gunter
se you intend to use the result of list(...) or c(...). > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > >> -Original Message- >> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On >> Behalf >> Of Julio Sergio

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

2013-06-05 Thread William Dunlap
)) and f1 does. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf > Of Julio Sergio > Sent: Wednesday, June 05, 2013 2:11 PM > To: r-h...@stat.math.ethz.ch

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

2013-06-05 Thread Julio Sergio
William Dunlap 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 m

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

2013-06-05 Thread William Dunlap
or c(...). Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf > Of Julio Sergio > Sent: Wednesday, June 05, 2013 1:26 PM > To: r-h...@stat.math.ethz.ch >

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

2013-06-05 Thread Julio Sergio
Bert Gunter 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

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

2013-06-05 Thread Bert Gunter
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 fu

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

2013-06-05 Thread Julio Sergio
Rui Barradas 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 of a

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

2013-06-05 Thread Rui Barradas
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) f

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

2013-06-05 Thread Rui Barradas
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

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

2013-06-04 Thread Michael Weylandt
On Jun 5, 2013, at 3:53, Julio Sergio 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 produces funcion

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

2013-06-04 Thread Julio Sergio
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 d