[R] (Newbie)Basic Basic global vs. local variables

Mark Wardle mark at wardle.org
Tue Apr 3 11:00:09 CEST 2007


projection83 wrote:
> My R code has got too complex to have a non-modular approach. Ive done some
> coding in other languages before, but I somehow cant figure out R's general
> rules for global and local variables. I have put a simple code below, if
> anyone can show me what i need to add to make this work, i would greatly
> appreciate it!
> 
> #----------------------------------------
> g_Means<-numeric()
>   
> defineSamples<- function()
> 	{
> 		g_Means<-5
> 	}
> 
> runit<-function()
> 	{
> 		defineSamples()
> 		g_Means #####<<This returns "numeric(0)", and not "5"
> 	}
> 
> runit()
> #----------------------------
> Basically I can not get the parameter I set from a global scale...

I don't think you quite understand variable scope.

The "Introduction to R" says:

(http://cran.r-project.org/doc/manuals/R-intro.html#Assignment-within-functions)

> 10.5 Assignments within functions
> 
> Note that any ordinary assignments done within the function are local and temporary and are lost after exit from the function. Thus the assignment X <- qr(X) does not affect the value of the argument in the calling program.
> 
> To understand completely the rules governing the scope of R assignments the reader needs to be familiar with the notion of an evaluation frame. This is a somewhat advanced, though hardly difficult, topic and is not covered further here.
> 
> If global and permanent assignments are intended within a function, then either the “superassignment” operator, <<- or the function assign() can be used. See the help document for details. S-Plus users should be aware that <<- has different semantics in R. These are discussed further in Scope.


I've struggled with keeping my global namespace relatively clutter-free.
 You should think of functions as separate pieces of work, independent
from most other things, and try to design them to be as generic as
possible.

Presumably defineSamples() does some complex calculations, although it
is impossible to tell as it appears to not take any parameters, and
returns the value of g_Means (functions return their last evaluated
expression).

Therefore your runIt() function isn't actually getting the result.

I would suggest defining functions with well-defined inputs and outputs,
and NOT using global variables. It is difficult to give examples as you
haven't said what your functions are doing, but for example:

defineSamples <- function(a,b,c=TRUE) {
	# do some complex things with our input variables
	# and return the results  - let's make it explicit with a return() call
	return(a*b)

}

This is an isolated piece of logic, and if you go and change things
elsewhere, its functionality won't have to be changed.

# note that this doesn't take any parameters.
runIt <- function() {
	my.result = defineSamples(3,4)
	return(my.results)		# superfluous, but nice to make it explicit for
this example
}


Then you can call

runIt()
=> gives 12

And if you want to save the result, use

g_Means = runIt()


You can pass arbitrarily complex data structures from and to functions
if you have the need -  see list().

Hope this helps,

Mark
-- 
Dr. Mark Wardle
Specialist registrar, Neurology
Cardiff, UK



More information about the R-help mailing list