[R] global objects not overwritten within function

McGehee, Robert Robert.McGehee at geodecapital.com
Tue Jan 11 16:49:51 CET 2005


I would suggest reading the posting guide,
(http://www.r-project.org/posting-guide.html) and give a reproducible
example, with the error message that you received. As is, I have no idea
what you are doing here, and certainly cannot run this code. You use
"..." as an argument to your functions (why I have no idea), but then
use "..." within your function seemingly to mean code was omitted rather
than using your function argument "...". What "...obj..." means I have
no idea.

One point though. If the f() function does not take any arguments, then
why are you using the special R object  "..." as an argument?
Furthermore, why not just do the assignment inside of fct() instead of
calling another function that just runs code? Please reference the
_rest_ of the R Language guide for information on correct usage of
"...", especially the chapter on functions, and include a script that
can be run from start to finish by anyone at an R prompt without having
to decipher what the missing code does, or what the ellipsis is doing in
your context.

My guess to what I think is going on here is that you are trying to use
dynamic scoping, when R uses lexical scoping. If you are an S user, this
will be a change. The f() function is stored in .GlobalEnv, so is not
aware of any objects stored in the fct() environment. When you run f(),
it's looking for an "obj" object in its environment, probably can't find
one, and then looking for the "obj" object in the global environment. If
it finds it there, it assigns it to itself, basically doing nothing.
Once again, the R language guide will explain this. You could solve this
by either imbedding the f() function inside of fct(), passing in the obj
object, instead of relying on dynamic scoping (which R doesn't use), or
probably preferably, not have an f() function at all, as all it does is
call another function.

Also, I'd reference ?"<<-" for perhaps a cleaner way of doing global
assignments. Using this alone may solve your problems, as it may force
you to scope your code correctly.


-----Original Message-----
From: bogdan romocea [mailto:br44114 at yahoo.com] 
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