Skip to content
Prev 391166 / 398506 Next

Creating and manipulating environment within function

Hello,

Use ?assign instead. And there was a bug in manipulate_e1, it would 
throw an error a not found.


create_e1 <- function(){
   assign("e1", new.env(), envir = globalenv())
   exists('e1', mode = 'environment')
}

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

create_e1()
#> [1] TRUE

exists('e1', envir = globalenv())
#> [1] TRUE

x <- "vec"
e1$vec
#> NULL
manipulate_e1(x)
#> [1] TRUE
e1$vec
#> [1] 1


Hope this helps,

Rui Barradas

?s 21:29 de 30/03/2022, Sebastien Bihorel escreveu: