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:> 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) > > > > ------------------------------------------------------------------------ > > ______________________________________________ > R-help at 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-- __________________________________________ Kristel Joossens Ph.D. Student Research Center ORSTAT K.U. Leuven Naamsestraat 69 Tel: +32 16 326929 3000 Leuven, Belgium Fax: +32 16 326732 E-mail: Kristel.Joossens at econ.kuleuven.be http://www.econ.kuleuven.be/public/ndbae49 Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
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?> 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) > >--------------------------------------------------------------------------------> ______________________________________________ > R-help at 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.htmlDisclaimer: 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?> 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) > >--------------------------------------------------------------------------------> ______________________________________________ > R-help at 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
svenknueppel at reilich.net wrote:> 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.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:> Hello, > > I try to define a global variable.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> 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) > > > > ------------------------------------------------------------------------ > > ______________________________________________ > R-help at 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
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:> 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) > > > > ______________________________________________ > R-help at 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 > >