[R] global variables in a function

Gray Calhoun gray.calhoun at gmail.com
Thu Dec 10 20:20:30 CET 2009


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 <keithlj at 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 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.
>



-- 
Gray Calhoun

Assistant Professor of Economics
Iowa State University




More information about the R-help mailing list