Skip to content

generate simple function with pre-defined constants

11 messages · Liviu Andronic, arun, Pascal Oettli +3 more

#
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
#
On Thu, Jun 6, 2013 at 4:48 PM, Liviu Andronic <landronimirc at gmail.com> wrote:
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:
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

  
    
  
#
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 <landronimirc at gmail.com>
To: "r-help at r-project.org Help" <r-help at r-project.org>
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
#
On Thu, Jun 6, 2013 at 5:03 PM, arun <smartpink111 at yahoo.com> wrote:
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

  
    
  
#
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
#
On Thu, Jun 6, 2013 at 9:05 AM, William Dunlap <wdunlap at tibco.com> wrote:
With the Force on your side, functions always do what you want.
2 days later
#
Dear Bill,
On Thu, Jun 6, 2013 at 5:36 PM, William Dunlap <wdunlap at tibco.com> wrote:
This is exactly what I was trying to do. Thank you for the explanations,
Liviu