Cheap way of making local scopese in R?

I like Juilia's let blocks which allow me to make a local scope full of temporary variables that I don't care about.

c = let 
  a = 1
  b = 2
  a + b
# a and b not defined here

It seems you could use an environment or a function. Here is some simple code demonstrating how objects defined withing those are not available in the Global Environment.

E <- new.env()
E$a <-  3
E$b <- 2
a + b #Error!
E$a + E$b # returns 5

MyFunc <- function() {
  x <-  5
  y <-  7
  x + y
x + y #Error!
MyFunc() #returns 12
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.