I need to allocate (using C nomenclature) a set of "global" variables,
some integer scalars and some double vectors.
I have placed the name of such variables in the file containing the main script
and the called functions:
# ----------------------------------------------------------------------------
# --------------------------- GLOBAL DATA & CONSTANTS
----------------------
EntrThreshold <- 1.e+35
NCMAX <- 50
ncof <- 0
ioff <- 0
joff <- 0
sig <- 0.0
cc <- vector(length=NCMAX)
cr <- vector(length=NCMAX)
Function "pwtset", called by function "ford", changes the
content of variable 'ncof' as confirmed by the debigging printed values.
But when function "pwset" exits and the flow control returns to the
caller "ford" then variable 'ncof' has not kept the value
assigned by "pwset", as if function "pwset" had allocated
its own variable 'ncof'.
My question is: how can I have R interpreter allocate
"global"variables visible and accessible by all the functions in the
same file ?
"pwtset" <- function(n) {
cat("\n BEGIN 'pwtset' \n")
if(n < 100){
ncof <- n
}else{
ncof <- n -100
}
........
}
BEGIN 'pwtset'
function 'pwtset': ncof = 4
END 'pwtset'
function 'ford': BEFORE calling wt1, ncof: 0
Thank you very much for removing my confusion.
Maura
tutti i telefonini TIM!
[[alternative HTML version deleted]]
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
A) you should give and return the variables through input/output
parameter of the functions, e.g.
pwtset <- function(n, inout) {
if (n < 100){
inout$ncof <- n
} else {
inout$ncof <- n -100
}
...
return(inout)
}
ford <- function(inout) {
...
n <- 104
inout <- pwtset(n, inout)
print(inout$ncof)
...
return (inout)
}
inout <- list(ncof=0, ioff=0, joff=0, sig=0.0)
inout <- ford(inout)
...
B) If you really need access to the global variables then use "assign"
and "get"
pwtset <- function(n) {
if (n < 100){
ncof <- n
} else {
ncof <- n -100
}
assign ("ncof", ncof, envir=.GlobalEnv)
...
return(inout)
}
ford <- function(inout) {
...
n <- 104
inout <- pwtset(n, inout)
ncof <- get("ncof", envir=.GlobalEnv)
print(ncof)
...
}
...
ford()
...
This programing style has a serious disadvantage: Debugging can become
very difficult, since the value of ncof in .GlobalEnv might not be what
you expect.
Yours sincerely
Sigbert
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFJ0g4jWvYUYQkj1zkRAiopAJ49Vo+NriS+R7/24QGwtPB43wI/HQCdGvYE
PjgZ+Vn0P30D5S3WvJrMX9s=e9rL
-----END PGP SIGNATURE-----
On Tue, Mar 31, 2009 at 7:10 AM, <mauede@alice.it> 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]]