[R] function to avoid <<-

Karim Mezhoud kmezhoud at gmail.com
Tue Dec 2 17:32:22 CET 2014


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.wickham at 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 <kmezhoud at 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 at 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]]



More information about the R-help mailing list