[R] global objects not overwritten within function

Huntsinger, Reid reid_huntsinger at merck.com
Tue Jan 11 16:33:30 CET 2005


Do you do anything with "obj" in the elided code? When you say the code
fails, do you mean that somewhere it accesses "obj" and doesn't get the
version in .GlobalEnv? The line "rm(obj)" removes the copy in the function's
environment, so I guess there is a local copy of "obj", otherwise the line
would have no effect. 

If you want the "obj" in .GlobalEnv, you have to ask for it, with "get",
unless there are no local copies.

More code, or better, a distilled working example, the R version, and
platform would be helpful.

Reid Huntsinger

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of bogdan romocea
Sent: Tuesday, January 11, 2005 9:26 AM
To: r-help at stat.math.ethz.ch
Subject: [R] global objects not overwritten within function


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.

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list