Re: [Rd] Defining environments within functions

From: Luke Tierney <luke_at_stat.uiowa.edu>
Date: Fri, 29 Aug 2008 08:44:29 -0500 (CDT)

If you want to use this pattern repeatedly you can define something like

     makeFunWithCounter <- function(fun) {
 	counter <- 0
 	list(count = function() counter,
 	     fun = function(...) { counter <<- counter + 1; fun(...)})
     }

and then do

     > fwc <- makeFunWithCounter(function() print("Hello"))
     > f2 <- fwc$fun
     > f2()
     [1] "Hello"
     > f2()
     [1] "Hello"
     > f2()
     [1] "Hello"
     > fwc$count()
     [1] 3

If you only want to do it once you can use local,

     fwc <- local({
 	counter <- 0
 	fun <- function() { counter <<- counter + 1; print("Hello") }
 	list(count = function() counter, fun = fun)
     })

Best,

luke

On Fri, 29 Aug 2008, Giles Hooker wrote:

>
> Thanks,
>
> I think I over-emphasized the secondary function, but I can generate the
> scoping problem as follows. First, at the command line, I can get a function
> to access objects that were not in its arguments by
>
> ProfileEnv = new.env()
> hello.world = "Hello World"
> assign('hello.world',hello.world,3,envir=ProfileEnv)
>
> fn1 = function()
> {
> hw = get('hello.world',envir=ProfileEnv)
> print(hw)
> }
>
> and then call
>
>> fn1()
> [1] "Hello World"
>
>
> Now I want to define a wrapper function
>
> fn2 = function()
> {
> ProfileEnv = new.env()
> hello.world = "Hello World"
> assign('hello.world',hello.world,3,envir=ProfileEnv)
>
> fn1()
> }
>
> and if I try
>
>> rm(ProfileEnv) # Just to be safe
>> rm(hello.world)
>> fn2()
> Error in get("hello.world", envir = ProfileEnv) :
> object "ProfileEnv" not found
>
> In my actual code, fn1() is really a call to
>
> optim(pars,ProfileErr,....)
>
> and hello.world are quantities that were calculated the last time that
> ProfileErr was called and that I want to keep track of.
>
> As an alternative simple example, how would I keep a counter for the number
> of times that optim (or any other generic optimizer) has called ProfileErr?
>
> giles
>
>>> How can I define environments within a function so that they are visible
>>> to calls to a sub-function?
>>>
>>
>> I think you need to give a simplified, runnable example. (Or at least
>> runnable until it hits the scoping problem you've got.) "Sub-function"
>> isn't R terminology, and it's not clear what you mean by it.
>
>

-- 
Luke Tierney
Chair, Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:      luke_at_stat.uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Fri 29 Aug 2008 - 13:45:43 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 29 Aug 2008 - 14:38:14 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive