Re: [Rd] Defining environments within functions

From: Giles Hooker <>
Date: Fri, 29 Aug 2008 06:52:10 -0400


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"

fn1 = function()

	hw = get('',envir=ProfileEnv)


and then call

 > fn1()
[1] "Hello World"

Now I want to define a wrapper function

fn2 = function()

	ProfileEnv = new.env() = "Hello World"



and if I try

 > rm(ProfileEnv)                          # Just to be safe
 > rm(
 > fn2()
Error in get("", envir = ProfileEnv) :

   object "ProfileEnv" not found

In my actual code, fn1() is really a call to


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


>> 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.
Giles Hooker
Assistant Professor:

Department of Biological Statistics and Computational Biology
Department of Statistical Science
1186 Comstock Hall
Cornell University
Ithaca, NY, 14853

Ph:  (+1 607) 255 1638
Fax: (+1 607) 255 4698


______________________________________________ mailing list
Received on Fri 29 Aug 2008 - 13:27:16 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 - 16:38:14 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive