I said the force was 'required' in the sense that without it
the function will fail to do what you want in some situations.
It doesn't make sense to write a function that you know will
fail sometimes when you know an easy way to make it work
in all situations.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

From: Mark Leeds [mailto:marklee...@gmail.com]
Sent: Thursday, June 06, 2013 8:57 AM
To: William Dunlap
Cc: Liviu Andronic; r-help@r-project.org Help
Subject: Re: [R] generate simple function with pre-defined constants

hi bill: I understand what you're doing but, atleast for this case, I checked 
and you don't need the force this one. it works without it. so, I think the 
force requirement applies only when you're building them up with the lapply. 
but definitely I'm opened to clarification. thanks.



On Thu, Jun 6, 2013 at 11:36 AM, William Dunlap 
<wdun...@tibco.com<mailto:wdun...@tibco.com>> wrote:
Try the following:
   generateABFunction <- function(a, b) {
      force(a)
      force(b)
      function(x) a*x + b
   }
   f12 <- generateABFunction(1, 2)
   f53 <- generateABFunction(5,6)
   f12(10:12) # get 12, 13, 14
   f53(10:12) # get 56, 61, 66

See, e.g., yesterday's discussion under the subject
"Trying to build up functions with its names by means of lapply"
on why the force() calls are required.  Read up on R's environments
to see why f12 and f53 look the same but act differently (hint:
look at ls.str(environment(f12))).

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com<http://tibco.com>


> -----Original Message-----
> From: r-help-boun...@r-project.org<mailto:r-help-boun...@r-project.org> 
> [mailto:r-help-boun...@r-project.org<mailto:r-help-boun...@r-project.org>] On 
> Behalf
> Of Liviu Andronic
> Sent: Thursday, June 06, 2013 8:00 AM
> To: r-help@r-project.org<mailto:r-help@r-project.org> Help
> Subject: Re: [R] generate simple function with pre-defined constants
>
> On Thu, Jun 6, 2013 at 4:48 PM, Liviu Andronic 
> <landronim...@gmail.com<mailto:landronim...@gmail.com>> wrote:
> > Dear all,
> > Given:
> > a <- 2
> > b <- 3
> >
> > I'd like to obtain the following function:
> > f <- function(x) 2 + 3*x
> >
> > but when I do this:
> > f <- function(x) a + b*x
> > ##f
> > ##function(x) a + b*x
> >
> > the 'a' and 'b' objects do not get evaluated to their constants. How
> > could I do that?
> >
> I found one solution:
> a <- 2
> b <- 3
> f <- eval(parse(text=paste("function(z)", a, "+ z * ", b)))
> f
> ##function(z) 2 + z *  3
>
> but I still have nightmares from:
> > fortune("parse")
>
> If the answer is parse() you should usually rethink the question.
>    -- Thomas Lumley
>       R-help (February 2005)
>
> Is there a nicer way to approach this? Thanks,
> Liviu
>
>
> > Thanks,
> > Liviu
> >
> >
> > --
> > Do you know how to read?
> > http://www.alienetworks.com/srtest.cfm
> > http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader
> > Do you know how to write?
> > http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
>
>
>
> --
> Do you know how to read?
> http://www.alienetworks.com/srtest.cfm
> http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader
> Do you know how to write?
> http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
>
> ______________________________________________
> R-help@r-project.org<mailto:R-help@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@r-project.org<mailto:R-help@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.


        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to