[R] global objects not overwritten within function

bogdan romocea br44114 at yahoo.com
Tue Jan 11 15:26:28 CET 2005


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().

Thank you,
b.




More information about the R-help mailing list