generate simple function with pre-defined constants

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

generate simple function with pre-defined constants

Liviu Andronic
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?

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

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: generate simple function with pre-defined constants

Liviu Andronic
On Thu, Jun 6, 2013 at 4:48 PM, Liviu Andronic <[hidden email]> 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




--
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

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: generate simple function with pre-defined constants

arun kirshna
In reply to this post by Liviu Andronic
HI,
Not sure I understand your question:
 a <- 2
 b <- 3
 f1<- function(x) a+b*x
 f1(2)
#[1] 8
 f1(3)
#[1] 11
 f<- function(x) 2+3*x
 f(2)
#[1] 8
 f(3)
#[1] 11


A.K.

  sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C             
 [3] LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8   
 [5] LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8  
 [7] LC_PAPER=C                 LC_NAME=C                
 [9] LC_ADDRESS=C               LC_TELEPHONE=C           
[11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C      

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base    

other attached packages:
[1] arrayhelpers_0.76-20120816 abind_1.4-0              
[3] plyr_1.8                   stringr_0.6.2            
[5] reshape2_1.2.2           

loaded via a namespace (and not attached):
[1] tools_3.0.0


----- Original Message -----
From: Liviu Andronic <[hidden email]>
To: "[hidden email] Help" <[hidden email]>
Cc:
Sent: Thursday, June 6, 2013 10:48 AM
Subject: [R] generate simple function with pre-defined constants

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?

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

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: generate simple function with pre-defined constants

Liviu Andronic
On Thu, Jun 6, 2013 at 5:03 PM, arun <[hidden email]> wrote:
> HI,
> Not sure I understand your question:
>  a <- 2
>  b <- 3
>  f1<- function(x) a+b*x
>
I don't want the function to depend on the objects a and b, but
instead use the values of those objects (I do this within a function).

Liviu


>  f1(2)
> #[1] 8
>  f1(3)
> #[1] 11
>  f<- function(x) 2+3*x
>  f(2)
> #[1] 8
>  f(3)
> #[1] 11
>
>
> A.K.
>
>   sessionInfo()
> R version 3.0.0 (2013-04-03)
> Platform: x86_64-unknown-linux-gnu (64-bit)
>
> locale:
>  [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C
>  [3] LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8
>  [5] LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8
>  [7] LC_PAPER=C                 LC_NAME=C
>  [9] LC_ADDRESS=C               LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] arrayhelpers_0.76-20120816 abind_1.4-0
> [3] plyr_1.8                   stringr_0.6.2
> [5] reshape2_1.2.2
>
> loaded via a namespace (and not attached):
> [1] tools_3.0.0
>
>
> ----- Original Message -----
> From: Liviu Andronic <[hidden email]>
> To: "[hidden email] Help" <[hidden email]>
> Cc:
> Sent: Thursday, June 6, 2013 10:48 AM
> Subject: [R] generate simple function with pre-defined constants
>
> 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?
>
> 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
>
> ______________________________________________
> [hidden email] 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.
>



--
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

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: generate simple function with pre-defined constants

Pascal Oettli-2
In reply to this post by Liviu Andronic
Hi,

I am not sure what you are looking for. Here are some examples:

foo <- function(a,b,x) a + b*x

> foo
function(a,b,x) a + b*x

a <- 2
b <- 3
x <- 0:10

> foo(a,b,x)
 [1]  2  5  8 11 14 17 20 23 26 29 32

Or

library(polynom)

p1 <- polynomial(c(a,b))
> p1
2 + 3*x

f1 <- as.function(p1)

> f1(x)
 [1]  2  5  8 11 14 17 20 23 26 29 32


Regards,
Pascal



2013/6/6 Liviu Andronic <[hidden email]>

> 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?
>
> 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
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: generate simple function with pre-defined constants

William Dunlap
In reply to this post by Liviu Andronic
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


> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf
> Of Liviu Andronic
> Sent: Thursday, June 06, 2013 8:00 AM
> To: [hidden email] Help
> Subject: Re: [R] generate simple function with pre-defined constants
>
> On Thu, Jun 6, 2013 at 4:48 PM, Liviu Andronic <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: generate simple function with pre-defined constants

mark leeds
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 <[hidden email]> 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
>
>
> > -----Original Message-----
> > From: [hidden email] [mailto:[hidden email]]
> On Behalf
> > Of Liviu Andronic
> > Sent: Thursday, June 06, 2013 8:00 AM
> > To: [hidden email] Help
> > Subject: Re: [R] generate simple function with pre-defined constants
> >
> > On Thu, Jun 6, 2013 at 4:48 PM, Liviu Andronic <[hidden email]>
> 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
> >
> > ______________________________________________
> > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: generate simple function with pre-defined constants

William Dunlap
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:[hidden email]]
Sent: Thursday, June 06, 2013 8:57 AM
To: William Dunlap
Cc: Liviu Andronic; [hidden email] 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 <[hidden email]<mailto:[hidden email]>> 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: [hidden email]<mailto:[hidden email]> [mailto:[hidden email]<mailto:[hidden email]>] On Behalf
> Of Liviu Andronic
> Sent: Thursday, June 06, 2013 8:00 AM
> To: [hidden email]<mailto:[hidden email]> Help
> Subject: Re: [R] generate simple function with pre-defined constants
>
> On Thu, Jun 6, 2013 at 4:48 PM, Liviu Andronic <[hidden email]<mailto:[hidden email]>> 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
>
> ______________________________________________
> [hidden email]<mailto:[hidden email]> 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.

______________________________________________
[hidden email]<mailto:[hidden email]> 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]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: generate simple function with pre-defined constants

mark leeds
gotcha. thanks.


On Thu, Jun 6, 2013 at 12:05 PM, William Dunlap <[hidden email]> wrote:

>  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:[hidden email]]
> *Sent:* Thursday, June 06, 2013 8:57 AM
> *To:* William Dunlap
> *Cc:* Liviu Andronic; [hidden email] 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 <[hidden email]> 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****
>
>
>
> > -----Original Message-----
> > From: [hidden email] [mailto:[hidden email]]
> On Behalf
> > Of Liviu Andronic
> > Sent: Thursday, June 06, 2013 8:00 AM
> > To: [hidden email] Help
> > Subject: Re: [R] generate simple function with pre-defined constants
> >
> > On Thu, Jun 6, 2013 at 4:48 PM, Liviu Andronic <[hidden email]>
> 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
> >
> > ______________________________________________
> > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: generate simple function with pre-defined constants

Joshua Wiley-2
In reply to this post by William Dunlap
On Thu, Jun 6, 2013 at 9:05 AM, William Dunlap <[hidden email]> wrote:
>
> I said the force was 'required' in the sense that without it
> the function will fail to do what you want in some situations.

With the Force on your side, functions always do what you want.

>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: generate simple function with pre-defined constants

Liviu Andronic
In reply to this post by William Dunlap
Dear Bill,


On Thu, Jun 6, 2013 at 5:36 PM, William Dunlap <[hidden email]> 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))).
>
This is exactly what I was trying to do. Thank you for the explanations,
Liviu


> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
>
>> -----Original Message-----
>> From: [hidden email] [mailto:[hidden email]] On Behalf
>> Of Liviu Andronic
>> Sent: Thursday, June 06, 2013 8:00 AM
>> To: [hidden email] Help
>> Subject: Re: [R] generate simple function with pre-defined constants
>>
>> On Thu, Jun 6, 2013 at 4:48 PM, Liviu Andronic <[hidden email]> 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
>>
>> ______________________________________________
>> [hidden email] 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.



--
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

______________________________________________
[hidden email] 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.