[R] Defining functions inside loops

Dr. Matthias Kohl Matthias.Kohl at stamats.de
Tue Feb 15 17:03:27 CET 2011


Dear Eduardo,

try:
f <- function(x){}
s <- 0.2
body(f) <- substitute({x^2 + s}, list(s = s))

Best,
Matthias

On 15.02.2011 16:53, Eduardo de Oliveira Horta wrote:
> Thanks... but I guess I didn't make myself clear. What I was trying to
> do was precisely to "store" inside the function the number associated
> to s[i] rather than the call to s[i], such that I wouldn't need to
> keep that object in subsequent function calls.
>
> In other words, I wanted to use lapply to get functions equivalent to:
> s<- c( 0.2, 0.45, 0.38, 0.9)
> f<-list()
> f[[1]]<- function(x) x^2+0.2
> f[[2]]<- function(x) x^2+0.45
> f[[3]]<- function(x) x^2+0.38
> f[[4]]<- function(x) x^2+0.9
>
> Best regards,
>
> Eduardo
>
>
> On Tue, Feb 15, 2011 at 7:20 AM, Dennis Murphy<djmuser at gmail.com>  wrote:
>> Hi:
>>
>> If you look at the error message, you'll see that you removed s before
>> evaluating f, and since an element of s is called in the function....
>>
>> Try
>>> s<- c( 0.2, 0.45, 0.38, 0.9)
>>> f<- lapply(1:10, function(i)local({ force(i) ; function(x)x^2+s[i]}))
>>> f[[1]](s)
>> [1] 0.2400 0.4025 0.3444 1.0100
>>
>> f is a list with 10 components, the first of which is
>> [[1]]
>> function (x)
>> x^2 + s[i]
>> <environment: 0x0000000002a26d48>
>>
>> Each component occupies a different environment. To see what you get,
>>
>>> f[[1]](0.1)
>> [1] 0.21
>>
>>> for(i in 1:10) print(f[[i]](i))
>> [1] 1.2
>> [1] 4.45
>> [1] 9.38
>> [1] 16.9
>> [1] NA
>> [1] NA
>> [1] NA
>> [1] NA
>> [1] NA
>> [1] NA
>>
>>> for(i in 1:10) print(f[[i]](1))
>> [1] 1.2
>> [1] 1.45
>> [1] 1.38
>> [1] 1.9
>> [1] NA
>> [1] NA
>> [1] NA
>> [1] NA
>> [1] NA
>> [1] NA
>>
>> HTH,
>> Dennis
>>
>> On Mon, Feb 14, 2011 at 9:50 PM, Eduardo de Oliveira Horta
>> <eduardo.oliveirahorta at gmail.com>  wrote:
>>>
>>> Hello again.
>>>
>>> Let me try something a little more intricate. Let's say instead of
>>> forcing evaluation of 'i' I'd want to force evaluation of a vector;
>>> for example:
>>> s<- c( 0.2, 0.45, 0.38, 0.9)
>>> f<- lapply(1:10, function(i)local({ force(i) ; function(x)x^2+s[i]}))
>>> rm(s)
>>> f[[1]](0.1)
>>> Error in f[[1]](0.1) : object 's' not found
>>>
>>> Any thoughts?
>>>
>>> Best regards,
>>>
>>> Eduardo
>>>
>>>> sessionInfo()
>>> R version 2.11.1 (2010-05-31)
>>> x86_64-pc-mingw32
>>>
>>> locale:
>>> [1] LC_COLLATE=Portuguese_Brazil.1252  LC_CTYPE=Portuguese_Brazil.1252
>>> [3] LC_MONETARY=Portuguese_Brazil.1252 LC_NUMERIC=C
>>> [5] LC_TIME=Portuguese_Brazil.1252
>>>
>>> attached base packages:
>>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>>
>>> other attached packages:
>>> [1] Revobase_4.2.0   RevoScaleR_1.1-1 lattice_0.19-13
>>>
>>> loaded via a namespace (and not attached):
>>> [1] grid_2.11.1       pkgXMLBuilder_1.0 revoIpe_1.0       tools_2.11.1
>>> [5] XML_3.1-0
>>>
>>>> On Mon, Nov 15, 2010 at 7:10 PM, William Dunlap<wdunlap at tibco.com>
>>>> wrote:
>>>>> You could make f[[i]] be function(t)t^2+i for i in 1:10
>>>>> with
>>>>>      f<- lapply(1:10, function(i)local({ force(i) ; function(x)x^2+i}))
>>>>> After that we get the correct results
>>>>>     >  f[[7]](100:103)
>>>>>     [1] 10007 10208 10411 10616
>>>>> but looking at the function doesn't immdiately tell you
>>>>> what 'i' is in the function
>>>>>     >  f[[7]]
>>>>>     function (x)
>>>>>     x^2 + i
>>>>>     <environment: 0x19d7458>
>>>>> You can find it in f[[7]]'s environment
>>>>>     >  get("i", envir=environment(f[[7]]))
>>>>>     [1] 7
>>>>>
>>>>> The call to force() in the call to local() is not
>>>>> necessary in this case, although it can help in
>>>>> other situations.
>>>>>
>>>>> Bill Dunlap
>>>>> Spotfire, TIBCO Software
>>>>> wdunlap tibco.com
>>>>>
>>>>>> -----Original Message-----
>>>>>> From: r-help-bounces at r-project.org
>>>>>> [mailto:r-help-bounces at r-project.org] On Behalf Of Eduardo de
>>>>>> Oliveira Horta
>>>>>> Sent: Monday, November 15, 2010 12:50 PM
>>>>>> To: r-help at r-project.org
>>>>>> Subject: [R] Defining functions inside loops
>>>>>>
>>>>>> Hello,
>>>>>>
>>>>>> I was trying to define a set of functions inside a loop, with
>>>>>> the loop index
>>>>>> working as a parameter for each function. Below I post a
>>>>>> simpler example, as
>>>>>> to illustrate what I was intending:
>>>>>>
>>>>>> f<-list()
>>>>>> for (i in 1:10){
>>>>>>    f[[i]]<-function(t){
>>>>>>      f[[i]]<-t^2+i
>>>>>>    }
>>>>>> }
>>>>>> rm(i)
>>>>>>
>>>>>> With that, I was expecting that f[[1]] would be a function
>>>>>> defined by t^2+1,
>>>>>> f[[2]] by t^2+2 and so on. However, the index i somehow
>>>>>> doesn't "get in" the
>>>>>> function definition on each loop, that is, the functions
>>>>>> f[[1]] through
>>>>>> f[[10]] are all defined by t^2+i. Thus, if I remove the
>>>>>> object i from the
>>>>>> workspace, I get an error when evaluating these functions.
>>>>>> Otherwise, if
>>>>>> don't remove the object i, it ends the loop with value equal
>>>>>> to 10 and then
>>>>>> f[[1]](t)=f[[2]](t)=...=f[[10]](t)=t^2+10.
>>>>>>
>>>>>> I am aware that I could simply put
>>>>>>
>>>>>> f<-function(u,i){
>>>>>>    f<-t^2+i
>>>>>> }
>>>>>>
>>>>>> but that's really not what I want.
>>>>>>
>>>>>> Any help would be appreciated. Thanks in advance,
>>>>>>
>>>>>> Eduardo Horta
>>>>>>
>>>>>>        [[alternative HTML version deleted]]
>>>>>>
>>>>>> ______________________________________________
>>>>>> R-help at 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 at 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 at 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.

-- 
Dr. Matthias Kohl
www.stamats.de



More information about the R-help mailing list