Hi, I'm working on coding some more complex things in R and have need to break much of the logic into functions. I have several "global" variables that I want to change with a given function. (The variable has a different value after the function is called.) In other languages like C, this is simple. However, in R, if a function changes a variable, that change only occurs in the frame of that function. So, when the function returns, the old value is still there. Of course, I could just have the function return the value, but some functions change 5-6 variables. So, I could have a function return a list, and then parse that list every time, but that seems like an excessive amount of overhead. (Especially as some functions may be called many many times.) How have some of you handled this? Is there a "best practices" way? Thanks! -- Noah Silverman UCLA Department of Statistics 8117 Math Sciences Building Los Angeles, CA 90095
Noah Silverman wrote:> > Hi, > > I'm working on coding some more complex things in R and have need to break > much of the logic into functions. > > I have several "global" variables that I want to change with a given > function. (The variable has a different value after the function is > called.) > > In other languages like C, this is simple. However, in R, if a function > changes a variable, that change only occurs in the frame of that function. > So, when the function returns, the old value is still there. > > Of course, I could just have the function return the value, but some > functions change 5-6 variables. So, I could have a function return a > list, and then parse that list every time, but that seems like an > excessive amount of overhead. (Especially as some functions may be called > many many times.) >Maybe this helps ?`<<-` Berend -- View this message in context: http://r.789695.n4.nabble.com/Variable-scope-in-functions-best-practices-tp3690026p3690416.html Sent from the R help mailing list archive at Nabble.com.
Hi, I'm working on coding some more complex things in R and have need to break much of the logic into functions. I have several "global" variables that I want to change with a given function. (The variable has a different value after the function is called.) In other languages like C, this is simple. However, in R, if a function changes a variable, that change only occurs in the frame of that function. So, when the function returns, the old value is still there. Of course, I could just have the function return the value, but some functions change 5-6 variables. So, I could have a function return a list, and then parse that list every time, but that seems like an excessive amount of overhead. (Especially as some functions may be called many many times.) How have some of you handled this? Is there a "best practices" way? Thanks! -- Noah Silverman UCLA Department of Statistics 8117 Math Sciences Building Los Angeles, CA 90095
Noah Silverman wrote:> > > I have several "global" variables that I want to change with a given > function. (The variable has a different value after the function is > called.) > >Berend Hasselman wrote:> > > Maybe this helps > > ?`<<-` >It helps to get the job done, but the OP asked for "best practices", which it is definitively not. You might check the docs for "assign" or search the list for this term (<<- searching is difficult) to finde a more fine-grained alternative within an explicit environment. Dieter -- View this message in context: http://r.789695.n4.nabble.com/Variable-scope-in-functions-best-practices-tp3690026p3693582.html Sent from the R help mailing list archive at Nabble.com.