[R] Scope
Gabor Grothendieck
ggrothendieck at gmail.com
Thu Feb 8 22:15:43 CET 2007
You can assign the environment within main to a variable in
the global environment and that will make it accessible even
after main terminates:
main <- function() {
assign("main.env", environment(), .GlobalEnv)
x <- 1; y <- 2
}
main()
main.env$x
main.env$y
# or
attach(main.env)
search() # note that main.env is on search path
x
y
detach() # remove from path now that we are finished
Alternately you could assign each variable within main
that you want to save:
main <- function() {
x <- 1; y <- 2
assign("x", x, .GlobalEnv)
assign("y", y, .GlobalEnv)
}
main()
x
y
On 2/8/07, Geoffrey Zhu <gzhu at peak6.com> wrote:
> Hi all,
>
> When I write a script, I'd like to create a main() function so that I
> only need to type main() t re-run it. However, I'd like all the
> variables in main() to be global so that when the function terminates, I
> still have access to the variables in the environment. Does anyone know
> how to do that?
>
> Best regards,
> Geoffrey
>
>
>
> _______________________________________________________=0A=
> =0A=
> =0A=
> The information in this email or in any file attached hereto...{{dropped}}
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list