Re: [R] Very important! Global variables in R?

From: Earl F. Glynn <efg_at_stowers-institute.org>
Date: Wed 25 Jan 2006 - 07:39:59 EST

"Sean Davis" <sdavis2@mail.nih.gov> wrote in message news:BFFB9479.448A%sdavis2@mail.nih.gov...

> > I need urgently your assistance!!!
> > I need a global variable in R. The variable ought to be known for every
> > functions and subfunctions. It is only to comparison purposes of the
> > numeric algorithms. Is there a possibility?
> >
> > please answer in german if possible.

Es tut mir leid. Ich kann Deutsch nicht sprechen.

>
> a <- 1

If you make that assignment inside a function, it won't be known outside the funtion, so "a" is not really global here.

Global assignment
x <<- 5
?"<<-"

BUT, be aware of these cautions about the use of global variables:

"I wish <<- had never been invented, as it makes an esoteric and dangerous feature of the language *seem* normal and reasonable. If you want to dumb down R/S into a macro language, this is the operator for you." -- Bill.Venables, R-Help, July 30, 2001

#thomas (tlumley), R-Help, Jan 24, 2001
This is one of the acceptable uses of <<-, as shown in demo(scoping). <<- is useful for modifying a variable that you know exists in the enclosing environment. Most of the problems come from people trying to use it to modify things in a parent environment or in the global environment.

Also see Section 13.3 "Global Data" in "Code Complete 2" (http://www.cc2e.com/) including:

- Common Problems with Global Data
- Use Global Data Only as a Last Resort
- How to Reduce the Risks of Using Global Data

efg



R-help@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 Received on Wed Jan 25 10:11:09 2006

This archive was generated by hypermail 2.1.8 : Wed 25 Jan 2006 - 14:08:01 EST