[R] Truly Global Variables

Lui ## lui.r.project at googlemail.com
Sat Jan 22 09:06:03 CET 2011


Hello everybody,

I have a problem that is bothering me for  quite a while now and I
don't know the answer... I want to create global variables (out of a
function) which I can access from all other functions... But somehow
that does not work too well. Attached some code for an example:

function_called <- function (){
	result = globalvariable*globalvariable
	print(result)
	
	}

function_calls <- function(){
	assign("globalvariable",10,pos=-1,envir=as.environment(pos))
	function_called()
	}

function_calls()

I would have assumed that I will get "100" printed on my screen, but
instead it said: "Fehler in function_called() : Objekt
'globalvariable' nicht gefunden", so the function "function_called"
did not have access to the variable "globalvariable".

I would be very thankful for some help... I want to implement it in
the genetic algorithms provided by the rgenoud package and dont know
any other way to have "changing" constraints (e.g. Portfolio
optimization and covariance matrix).
Thanks in advance! Any help greatly appreciated!

Lui



More information about the R-help mailing list