Re: [R] Global variables
Thanks to all!!! -- View this message in context: http://r.789695.n4.nabble.com/Global-variables-tp4710473p4710483.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Global variables
normally that works, BUT - is BAD and not accepted in some repositories as Bioconductor. one-function(){ a-variable passed return(a) } x - one() two-function(x){ print(x) } On Tue, Jul 28, 2015 at 1:22 PM, jpara3 j.para.fernan...@hotmail.com wrote: Hi, I want to pass a variable value from one function to another, but not as a function argument. For this propose I have put -, but it doesn´t work. My code: one-function(){ a-variable passed } two-function(){ print(a) } dos() If I execute dos(), then the error message is: Error in print(a) : object 'a' not found Thanks! -- View this message in context: http://r.789695.n4.nabble.com/Global-variables-tp4710472.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Global variables
Please don't. Function arguments are good... global variables are bad. one - function(){ result - list( a=variable passed ) result } two - function( v ){ print( v$a ) } x - one() two( x ) --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. On July 28, 2015 8:22:41 AM EDT, jpara3 j.para.fernan...@hotmail.com wrote: Hi, I want to pass a variable value from one function to another, but not as a function argument. For this propose I have put -, but it doesn´t work. My code: one-function(){ a-variable passed } two-function(){ print(a) } dos() If I execute dos(), then the error message is: Error in print(a) : object 'a' not found Thanks! -- View this message in context: http://r.789695.n4.nabble.com/Global-variables-tp4710472.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Global variables
In line comments On 28/07/2015 13:22, jpara3 wrote: Hi, I want to pass a variable value from one function to another, but not as a function argument. For this propose I have put -, but it doesn´t work. My code: one-function(){ a-variable passed } So you have to execute one() first? two-function(){ print(a) } So go one() here dos() I suppose you meant two() pero sin problema. If I execute dos(), then the error message is: Error in print(a) : object 'a' not found Thanks! -- View this message in context: http://r.789695.n4.nabble.com/Global-variables-tp4710472.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. -- Michael http://www.dewey.myzen.co.uk/home.html __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Global variables
On Fri, May 4, 2012 at 3:06 PM, Luis Goncalves lgoncal...@gmail.com wrote: On May 2 2011, 3:02 pm, Kenn Konstabel lebats...@gmail.com wrote: On Mon, May 2, 2011 at 2:19 PM, abhagwat bhagwatadi...@gmail.com wrote: Well, what would be really helpful is to restrict the scope of all non-function variables, but keep a global for scope of all function variables. Then, you still have access to all loaded functions, but you don't mix upvariables. How would one do that? But what's the real motivation for this? It could be useful for ensuring that there are no unexpectedglobalvariablesin your code but you can do it using findGlobals in codetools package. fun - function() mean(x) findGlobals(fun, merge=FALSE) Kenn Kenn, I tried your method : library(codetools) square - function (x) { y^2 } y - c(1, 3, 5, 9) square_without_globals - function (x) { y^2 } findGlobals(square_without_globals, merge=FALSE) but still get global variables in square_without_globals(): source('R_test_block_global_variables.R') y [1] 1 3 5 9 square(7) [1] 1 9 25 81 square_without_globals(7) [1] 1 9 25 81 What have I done wrong? findGlobals helps you find the global variables in a function but it does nothing with them. That is, it shows you something *about* a function but does nothing *with* a function. findGlobals(square_without_globals, merge=FALSE) shows you that you use 3 global variables in your function: $functions [1] ^ { $variables [1] y Now it's up 2 you how you use this information. You would probably like to use ^ and { (global variables) in your function but maybe not y. So you can edit your function and leave y out or add y as an argument. (PS: I am an R novice, coming from the Matlab world. In Matlab, you have to declare global variables to be used in a function explicitly. Isn't it good programming practice (in ANY language) to NOT allow global variable visibility as a default? Leads to a lot less hard-to- find bugs!!!) Yes but ... in R, functions are also variables, and you would probably like some global functions (`+` or `(`) to be visible. You can modify your function's environment so that it would access only functions from certain packages (e.g base) and/or nothing from the global workspace; that is what is done when functions are included in packages. Otherwise, I agree that it is best to avoid global variables and when you use them, they should be declared: square - function (x) { # beware! y is used here as a global variable y^2 } __ R-help@r-project.org 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.
Re: [R] Global variables
Well, what would be really helpful is to restrict the scope of all non-function variables, but keep a global for scope of all function variables. Then, you still have access to all loaded functions, but you don't mix up variables. How would one do that? Adi Is there a way I can prevent global variables to be visible within my functions? Yes, but you probably shouldn't. You would do it by setting the environment of the function to something that doesn't have the global environment as a parent, or grandparent, etc. The only common examples of that are baseenv() and emptyenv(). For example, x - 1 f - function() print(x) -- View this message in context: http://r.789695.n4.nabble.com/Global-variables-tp3178242p3489796.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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.
Re: [R] Global variables
On 02/05/2011 7:19 AM, abhagwat wrote: Well, what would be really helpful is to restrict the scope of all non-function variables, but keep a global for scope of all function variables. Then, you still have access to all loaded functions, but you don't mix up variables. How would one do that? You can't without low level modifications. Before R has done the lookup, it doesn't know if an object is a function or not. It can guess by usage, e.g. it can recognize that print should be a function in print(1) and it will ignore non-functions named print, but it is very common in R code to do things like fn - print fn(1) and that would fail. But if you want to experiment with the change, you can, because R is open source. I doubt if you'll get much help unless you give a really convincing argument (on the R-devel list, not on this list) why to make the change. Duncan Murdoch Adi Is there a way I can prevent global variables to be visible within my functions? Yes, but you probably shouldn't. You would do it by setting the environment of the function to something that doesn't have the global environment as a parent, or grandparent, etc. The only common examples of that are baseenv() and emptyenv(). For example, x- 1 f- function() print(x) -- View this message in context: http://r.789695.n4.nabble.com/Global-variables-tp3178242p3489796.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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. __ R-help@r-project.org 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.
Re: [R] Global variables
On Mon, May 2, 2011 at 2:19 PM, abhagwat bhagwatadi...@gmail.com wrote: Well, what would be really helpful is to restrict the scope of all non-function variables, but keep a global for scope of all function variables. Then, you still have access to all loaded functions, but you don't mix up variables. How would one do that? But what's the real motivation for this? It could be useful for ensuring that there are no unexpected global variables in your code but you can do it using findGlobals in codetools package. fun - function() mean(x) findGlobals(fun, merge=FALSE) Kenn Is there a way I can prevent global variables to be visible within my functions? Yes, but you probably shouldn't. You would do it by setting the environment of the function to something that doesn't have the global environment as a parent, or grandparent, etc. The only common examples of that are baseenv() and emptyenv(). For example, x - 1 f - function() print(x) -- View this message in context: http://r.789695.n4.nabble.com/Global-variables-tp3178242p3489796.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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. __ R-help@r-project.org 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.
Re: [R] Global variables
Thanks, I will have a look at it. Sebastien Michael Bedward wrote: Hi Sebastian, You might also find the proto package useful as a way of restricting the scope of variables. It provides a more intuitive (at least to me) way of packaging variables and functions up into environments that can be related in a hierarchy. Michael On 10 January 2011 23:48, Sebastien Bihorel sebastien.biho...@cognigencorp.com wrote: Thank Gabor and Duncan, That will be helpful. Gabor Grothendieck wrote: On Thu, Jan 6, 2011 at 4:59 PM, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 06/01/2011 4:45 PM, Sebastien Bihorel wrote: Dear R-users, Is there a way I can prevent global variables to be visible within my functions? Yes, but you probably shouldn't. You would do it by setting the environment of the function to something that doesn't have the global environment as a parent, or grandparent, etc. The only common examples of that are baseenv() and emptyenv(). For example, x - 1 f - function() print(x) Then f() will work, and print the 1. But if I do environment(f) - baseenv() then it won't work: f() Error in print(x) : object 'x' not found The problem with doing this is that it is not the way users expect functions to work, and it will probably have weird side effects. It is not the way things work in packages (even packages with namespaces will eventually search the global environment, the namespace just comes first). There's no simple way to do it and yet get access to functions in other packages besides base without explicitly specifying them (e.g. you'd need to use stats::lm(), not just lm(), etc.) A variation of this would be: environment(f) - as.environment(2) which would skip over the global environment, .GlobEnv, but would still search the loaded packages. In the example above x would not be found but it still could find lm, etc. [[alternative HTML version deleted]] __ R-help@r-project.org 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. __ R-help@r-project.org 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.
Re: [R] Global variables
Thank Gabor and Duncan, That will be helpful. Gabor Grothendieck wrote: On Thu, Jan 6, 2011 at 4:59 PM, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 06/01/2011 4:45 PM, Sebastien Bihorel wrote: Dear R-users, Is there a way I can prevent global variables to be visible within my functions? Yes, but you probably shouldn't. You would do it by setting the environment of the function to something that doesn't have the global environment as a parent, or grandparent, etc. The only common examples of that are baseenv() and emptyenv(). For example, x - 1 f - function() print(x) Then f() will work, and print the 1. But if I do environment(f) - baseenv() then it won't work: f() Error in print(x) : object 'x' not found The problem with doing this is that it is not the way users expect functions to work, and it will probably have weird side effects. It is not the way things work in packages (even packages with namespaces will eventually search the global environment, the namespace just comes first). There's no simple way to do it and yet get access to functions in other packages besides base without explicitly specifying them (e.g. you'd need to use stats::lm(), not just lm(), etc.) A variation of this would be: environment(f) - as.environment(2) which would skip over the global environment, .GlobEnv, but would still search the loaded packages. In the example above x would not be found but it still could find lm, etc. [[alternative HTML version deleted]] __ R-help@r-project.org 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.
Re: [R] Global variables
Hi Sebastian, You might also find the proto package useful as a way of restricting the scope of variables. It provides a more intuitive (at least to me) way of packaging variables and functions up into environments that can be related in a hierarchy. Michael On 10 January 2011 23:48, Sebastien Bihorel sebastien.biho...@cognigencorp.com wrote: Thank Gabor and Duncan, That will be helpful. Gabor Grothendieck wrote: On Thu, Jan 6, 2011 at 4:59 PM, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 06/01/2011 4:45 PM, Sebastien Bihorel wrote: Dear R-users, Is there a way I can prevent global variables to be visible within my functions? Yes, but you probably shouldn't. You would do it by setting the environment of the function to something that doesn't have the global environment as a parent, or grandparent, etc. The only common examples of that are baseenv() and emptyenv(). For example, x - 1 f - function() print(x) Then f() will work, and print the 1. But if I do environment(f) - baseenv() then it won't work: f() Error in print(x) : object 'x' not found The problem with doing this is that it is not the way users expect functions to work, and it will probably have weird side effects. It is not the way things work in packages (even packages with namespaces will eventually search the global environment, the namespace just comes first). There's no simple way to do it and yet get access to functions in other packages besides base without explicitly specifying them (e.g. you'd need to use stats::lm(), not just lm(), etc.) A variation of this would be: environment(f) - as.environment(2) which would skip over the global environment, .GlobEnv, but would still search the loaded packages. In the example above x would not be found but it still could find lm, etc. [[alternative HTML version deleted]] __ R-help@r-project.org 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. __ R-help@r-project.org 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.
Re: [R] Global variables
On 06/01/2011 4:45 PM, Sebastien Bihorel wrote: Dear R-users, Is there a way I can prevent global variables to be visible within my functions? Yes, but you probably shouldn't. You would do it by setting the environment of the function to something that doesn't have the global environment as a parent, or grandparent, etc. The only common examples of that are baseenv() and emptyenv(). For example, x - 1 f - function() print(x) Then f() will work, and print the 1. But if I do environment(f) - baseenv() then it won't work: f() Error in print(x) : object 'x' not found The problem with doing this is that it is not the way users expect functions to work, and it will probably have weird side effects. It is not the way things work in packages (even packages with namespaces will eventually search the global environment, the namespace just comes first). There's no simple way to do it and yet get access to functions in other packages besides base without explicitly specifying them (e.g. you'd need to use stats::lm(), not just lm(), etc.) Duncan Murdoch __ R-help@r-project.org 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.
Re: [R] Global variables
On Thu, Jan 6, 2011 at 4:59 PM, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 06/01/2011 4:45 PM, Sebastien Bihorel wrote: Dear R-users, Is there a way I can prevent global variables to be visible within my functions? Yes, but you probably shouldn't. You would do it by setting the environment of the function to something that doesn't have the global environment as a parent, or grandparent, etc. The only common examples of that are baseenv() and emptyenv(). For example, x - 1 f - function() print(x) Then f() will work, and print the 1. But if I do environment(f) - baseenv() then it won't work: f() Error in print(x) : object 'x' not found The problem with doing this is that it is not the way users expect functions to work, and it will probably have weird side effects. It is not the way things work in packages (even packages with namespaces will eventually search the global environment, the namespace just comes first). There's no simple way to do it and yet get access to functions in other packages besides base without explicitly specifying them (e.g. you'd need to use stats::lm(), not just lm(), etc.) A variation of this would be: environment(f) - as.environment(2) which would skip over the global environment, .GlobEnv, but would still search the loaded packages. In the example above x would not be found but it still could find lm, etc. -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ R-help@r-project.org 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.
Re: [R] global variables in a function
I think you are looking for a macro facility. See defmacro in gtools which is based on Thomas Lumley's function of the same name whose article you can find in back issues of R News. On Thu, Dec 10, 2009 at 11:00 AM, Keith Jones keit...@keithljelp.com wrote: Y'all, I would like to have most of the variables in my function to be global instead of local. I have not found any references that tell me now to do that. If I have missed a reference please excuse me and tell me what I have missed. Thanks, Keith Jones __ R-help@r-project.org 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. __ R-help@r-project.org 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.
Re: [R] global variables in a function
Hi Keith, A more specific example of what you're looking for might be helpful--ie do you want to read global variables or set them? You probably want to look at environments and closures; ?- and ?assign are good starting points (the latter has an example of Global Assignment within a function) The following code is what I think you're interested in: x - 5 ex - function(k) { + x - x + k + 2^x + } x [1] 5 ex(1) [1] 64 x [1] 6 Best, Gray On Thu, Dec 10, 2009 at 10:00 AM, Keith Jones keit...@keithljelp.com wrote: Y'all, I would like to have most of the variables in my function to be global instead of local. I have not found any references that tell me now to do that. If I have missed a reference please excuse me and tell me what I have missed. Thanks, Keith Jones __ R-help@r-project.org 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. -- Gray Calhoun Assistant Professor of Economics Iowa State University __ R-help@r-project.org 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.