Skip to content
Prev 2280 / 12125 Next

[R-pkg-devel] Assignments to the Global environment

On 07/01/2018 12:17 PM, William Dunlap wrote:
Yes, that's one way to do what Saeb wants.

Another way is to define the relevant functions as local functions, and 
do the assignment in their environment.  For example,

fns <- local({
   s <- NULL
   saveS <- function( newSval ) {
     s <<- newSval
   }
   showS <- function() s
   list(saveS, showS)
})
saveS <- fns[[1]]
showS <- fns[[2]]

saveS(123)
showS()

which will return the saved value at the end.

As a general principle, writing into the global environment should 
almost never be allowed.

Duncan