[R] global objects not overwritten within function

bogdan romocea br44114 at yahoo.com
Wed Jan 12 02:31:42 CET 2005


Thank you to everyone who replied. I had no idea that ... means
something in R, I only wanted to make the code look simpler. I'm
pasting below the functional equivalent of what took me yesterday a
couple of hours to debug. Function f() takes several arguments (that's
why I want to have the code as a function) and creates several objects.
I then need to use those objects in another function fct(), and I want
to overwrite them to save memory (they're pretty large).

It appears that Robert's guess (dynamic/lexical scoping) explains
what's going on. I've noticed though another strange (to me) issue:
without indexing (such as obj1 <- obj1[obj1 > 0] - which I need to use
though), fct() prints the expected values even without removing the
objects after each iteration. However, after indexing is introduced,
rm() must be used to make fct() return the intended output. How would
that be explained?

Kind regards,
b.

f <- function(read,position){
obj1 <- 5 * read[position]:(read[position]+5)
obj2 <- 7 * read[position]:(read[position]+5)
assign("obj1",obj1,.GlobalEnv)
assign("obj2",obj2,.GlobalEnv)
}
fct <- function(input){
for (i in 1:5)
	{
	f(input,i)
	obj1 <- obj1[obj1 > 0]
	obj2 <- obj2[obj2 > 0]
	print(obj1)
	print(obj2)
#	rm(obj1,obj2)	#get intended results with this line
	}
}
a <- 1:10
fct(a)







--- Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:

> On Tue, 11 Jan 2005, bogdan romocea wrote:
> 
> > Dear useRs,
> >
> > I have a function that creates several global objects with
> > assign("obj",obj,.GlobalEnv), and which I need to run iteratively
> in
> > another function. The code is similar to
> >
> > f <- function(...) {
> > assign("obj",obj,.GlobalEnv)
> > }
> > fct <- function(...) {
> > for (i in 1:1000)
> > 	{
> > 	...
> > 	f(...)
> > 	...obj...
> > 	rm(obj)	#code fails without this line
> > 	}
> > }
> >
> > I don't understand why f(), when run in a for() loop inside fct(),
> does
> > not overwrite the global object 'obj'. If I don't delete 'obj'
> after I
> > use it, the code fails - the same objects created by the first
> > iteration are used by subsequent iterations.
> >
> > I checked ?assign and the Evaluation chapter in 'R Language
> Definition'
> > but still don't understand why the above happens. Can someone
> briefly
> > explain or suggest something I should read? By the way, I don't
> want to
> > use 'better' techniques (lists, functions that return values
> instead of
> > creating global objects etc) - I want to create global objects with
> f()
> > and overwrite them again and again within fct().
> 
> Since you are not using ... in the sense it is used in R, we have
> little 
> idea of what your real code looks like and so what it does.
> 
> Can you please give a small real example that fails.  Here is one
> that 
> works, yet has all the features I can deduce from your non-code:
> 
> f <- function(x) assign("obj", x, pos=.GlobalEnv)
> fct <- function()
> {
>     for(i in 1:2) {
>       x <- i+3
>       f(x)
>       print(obj)
>     }
> }
> > fct()
> [1] 4
> [1] 5
> > obj
> [1] 5
> 
> -- 
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>




More information about the R-help mailing list