On Tue, Mar 31, 2009 at 7:10 AM, <[email protected]> wrote:
> I need to allocate (using C nomenclature) a set of "global" variables, some
> integer scalars and some double vectors.
> ...
My question is: how can I have R interpreter allocate "global"variables
> visible and accessible by all the functions in the same file ?
>
You don't need to explicitly allocate variables in R. Assigning to them
will create them.
If you want to modify a global variable within a function, you need to use
the <<- assignment operator instead of the <- assignment operator. In
effect, the <- assignment operator *always* refers to a local variable, and
creates one if one does not exist yet.
I do not know how to limit the visibility of a variable to "functions in the
same file". On the other hand, you can limit the visibility of a variable to
a group of functions quite straightforwardly:
local({
global1 <- 3 # allocated within the local scope with <-
global2 <- 4
fun1 <<- function(...) ... global1 ... # lexical scoping; refers to
global1
# must assign fun1 with <<- so it is created in the global scope
})
But I suspect you don't need this level of complexity.
All that being said, programming with global variables makes certain classes
of bug much more likely....
-s
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.