Re: [R] Global variables

From: Kenn Konstabel <lebatsnok_at_gmail.com>
Date: Mon, 02 May 2011 17:02:00 +0300

On Mon, May 2, 2011 at 2:19 PM, abhagwat <bhagwataditya_at_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_at_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_at_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. Received on Thu 05 May 2011 - 06:25:06 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 05 May 2011 - 07:00:05 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive