Skip to content

How define global Variable?

7 messages · svenknueppel@reilich.net, Kristel Joossens, Dimitris Rizopoulos +4 more

#
Hello,

I try to define a global variable.

My example:

R> a <- "old"
R> test <- function () { a <- "new" }
R> test()
R> a # shoud be "new"

This doesn't work. I would like to modify the variable "a" in a
procedure. How can I do that.

Thank you for helping.

Sven Knüppel (Germany-Berlin)
#
The problem is that the a is within the function
You can easily solve this by

test <- function () { a <- "new"; return(a) }
a=test()

Best regards,
Kristel


(or test <- function () { return(a <- "new")})
svenknueppel at reilich.net wrote:

  
    
#
try

a <- "old"
test <- function () { assign("a", "new", envir = .GlobalEnv) }
test()
a


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: <svenknueppel at reilich.net>
To: <r-help at stat.math.ethz.ch>
Sent: Monday, November 28, 2005 3:26 PM
Subject: [R] How define global Variable?
--------------------------------------------------------------------------------
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
#
In your current definitions a can not change value to
"new" unless you type  a<-test(). If you want the
results of the test to be global then you add something like this

test <-function()a<<-"new"

This will always replace the existing value of a once
you type test()



regards
Anthony----- Original Message ----- 
From: <svenknueppel at reilich.net>
To: <r-help at stat.math.ethz.ch>
Sent: Monday, November 28, 2005 3:26 PM
Subject: [R] How define global Variable?
--------------------------------------------------------------------------------
#
svenknueppel at reilich.net wrote:

            
You may like to modify the variable, but who else wants you to?

Functions should have zero side effects whenever possible. Wanting to 
muck with global variables is a big red flag that something is wrong 
with your program. It will become hard to debug or follow what is going 
on. Imagine, in six weeks time you look at:

  a = "old"
  test()
  if (a == "new"){
    doSomething()
  }

  - well, its not obvious that 'a' could possibly have changed to "new". 
Sure you could look at test() and see, but then test() could call 
something else that calls something else and then somewhere else 'a' is 
set. It can make for very very messy code.

  The solution is to return anything that changes. Example:

  a = "old"

  test=function(){return(list(a="new"))}

  ttt = test()
  a = ttt$a

  That's probably the recommended way of returning multiple things from 
a function too - wrap them in a list and get them. Modifying global 
variables is very rarely the Right Thing.

  I'm sure someone will come up with a solution but it'll probably 
involve frames and environments and other messy magic language stuff you 
really dont want to get into. Keep It Simple, Sunshine.

Barry
#
svenknueppel at reilich.net wrote:

            
You should not do that unless you really know why you want it this way. 
You probably want to read the R Language Definition manual.

Anyway, read ?assign if you want to proceed doing dangerous things.

Uwe Ligges
#
See:

http://tolstoy.newcastle.edu.au/~rking/R/help/05/11/15737.html

and the responses.
On 11/28/05, svenknueppel at reilich.net <svenknueppel at reilich.net> wrote: