[R] Very important! Global variables in R?

Earl F. Glynn efg at stowers-institute.org
Tue Jan 24 21:39:59 CET 2006


"Sean Davis" <sdavis2 at mail.nih.gov> wrote in message
news:BFFB9479.448A%sdavis2 at 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




More information about the R-help mailing list