Limiting variable scope

后端 未结 4 2116
北荒
北荒 2020-11-30 12:50

I\'m trying to write a function, which limits the scope of R variables. For example,

source(\"LimitScope.R\")
y = 0
f = function(){
   #Raises an error as y          


        
4条回答
  •  忘掉有多难
    2020-11-30 13:27

    What occassionally happens to me is that I've got a split screen in ESS with a file buffer of R code on the left and the interpreter on the right. I may have set some values in the interpreter while I debug the code I am working on in the buffer. It's then possible that the code in the buffer accidentally refereneces something I set in the intepreter. That's hard to detect problem unless I evaluate my buffer in a fresh interpreter every time, which is not how ESS works by default.

    If this is the kind of problem you are seeing frequently, an rm(list=ls(envir=.GlobalEnv)) in the thing you source might help, but that of course creates other issues, such as erasing anything they were using to keep state while debugging, etc.

提交回复
热议问题