[R] Creating and manipulating environment within function

Sebastien Bihorel @eb@@t|en@b|hore| @end|ng |rom @|mu|@t|on@-p|u@@com
Wed Mar 30 22:29:19 CEST 2022


Hi

I am trying to create a set of functions to create and manipulate objects within a dedicated environment, which I was hoping to put directly under the global environment. However, my implementation (see code below) is facing some scoping issues: the environment created by one function does not appear to be accessible from outside the function.

What would be the proper way to create a manipulation an environment in different functions?

Thanks

```
create_e1 <- function(){
  e1 <- new.env(parent = globalenv())
  exists('e1', mode = 'environment')
}
create_e1()

manipulate_e1 <- function(){
  if ( exists('e1', mode = 'environment') ) {
    assign(a, 1, envir = e1)
    TRUE
  } else {
    FALSE
  }
}

exists('e1', envir = globalenv())
manipulate_e1()
```
This message is sent to you because your email address is the contact email address provided for receiving messages related to: our products or services purchased, or information requested, from us; and/or evaluating or having entered into a business relationship with us. Visit our privacy policy at https://www.simulations-plus.com/privacy-policy/ to learn more about how we use your personal information and your rights regarding your personal information, including how to opt out of receiving marketing emails from us.



More information about the R-help mailing list