By "At the top level" Hadley meant to put that code outside of the function definition. In you source file that line should be very near the top, before any function definitions. Then "myenv" will not be temporary (well it will go away when you end the R session). Further, when this code is compiled into a package then "myenv" becomes package local, meaning that functions within the package can access it and the objects inside of it, but it will not interfere with any other packages or the global environment.
On Tue, Dec 2, 2014 at 9:32 AM, Karim Mezhoud <kmezh...@gmail.com> wrote: > Thanks Dr Hadley, > > but when I use a function the myenv remains temporary and I am to face the > same problem. > > > fun <- function(){ > > myenv <- new.env(parent = emptyenv()) > > fun1 <- function(){ > myenv$X <- 5 > } > > } > > ls(myEnv) > #character(0) > > Ô__ > c/ /'_;~~~~kmezhoud > (*) \(*) ⴽⴰⵔⵉⵎ ⵎⴻⵣⵀⵓⴷ > http://bioinformatics.tn/ > > > > On Tue, Dec 2, 2014 at 4:17 PM, Hadley Wickham <h.wick...@gmail.com> > wrote: > > > At the top level do: > > > > myenv <- new.env(parent = emptyenv()) > > > > Then in your functions do > > > > myenv$x <- 50 > > myenv$x > > > > etc > > > > You also should not be using data() in that way. Perhaps you want > > R/sysdata.rda. See http://r-pkgs.had.co.nz/data.html for more details. > > > > Hadley > > > > On Tue, Dec 2, 2014 at 2:28 AM, Karim Mezhoud <kmezh...@gmail.com> > wrote: > > > Dear All, > > > > > > I am writing a GUIpackage that needs global variables. > > > I had many warning message when I checked the code as for example: > > > geteSet: no visible binding for global variable ‘curselectCases’ > > > I would like to write a function that creates a global place for > Objects > > to > > > be loaded as: > > > > > > > > > Fun <- function(){ > > > > > > Object <- 5 > > > > > > Var2Global <- function(Object){ > > > .myDataEnv <- new.env(parent=emptyenv()) # not exported > > > isLoaded <- function(Object) { > > > exists(Object, .myDataEnv) > > > } > > > getData <- function(Object) { > > > if (!isLoaded(Object)) data(Object, envir=.myDataEnv) > > > .myDataEnv[[Object]] > > > } > > > } > > > > > > } > > > > > > To avoid the use of: Object <<- 5 > > > > > > but it seems not working yet. Object == 5 is not a global variable > after > > > running Fun(). > > > > > > Any Idea? > > > Thanks > > > Ô__ > > > c/ /'_;~~~~kmezhoud > > > (*) \(*) ⴽⴰⵔⵉⵎ ⵎⴻⵣⵀⵓⴷ > > > http://bioinformatics.tn/ > > > > > > [[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. > > > > > > > > -- > > http://had.co.nz/ > > > > [[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. > -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com [[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.