Consider the following R session under Windows XP Pro. Note that if we set debugging for function f then even if we reset its environment it still has debugging on. Also if we copy f to g then g has debugging; however, if we change g's environment then g no longer has debugging. Why did f retain debugging when its environment was changed but g did not? R : Copyright 2004, The R Foundation for Statistical Computing Version 1.9.0 (2004-04-12), ISBN 3-900051-00-3 [...snip...] Type 'q()' to quit R.> f <- function(x) { x } > f(1)[1] 1> debug(f) > f(1)debugging in: f(1) debug: { x } Browse[1]> Q> environment(f) <- new.env() > f(1)debugging in: f(1) debug: { x } Browse[1]> Q> g <- f > g(1)debugging in: g(1) debug: { x } Browse[1]> Q> environment(g) <- new.env() > g(1)[1] 1> >P.S. Here is just the input if you want to try it yourself by copying and pasting into an R session: f <- function(x) { x } f(1) debug(f) f(1) Q environment(f) <- new.env() f(1) Q g <- f g(1) Q environment(g) <- new.env() g(1) _______________________________________________ Make My Way your home on the Web - http://www.myway.com
> P.S. Here is just the input if you want to try it yourself by copying > and pasting into an R session:If you are using Windows RGUI and you haven't noticed the new "Paste commands only" in the Edit menu of R1.9.0, check it out. It is a fantastic time saver. Rob
On Tue, 20 Apr 2004, Gabor Grothendieck wrote:> > > > Consider the following R session under Windows XP Pro. Note that > if we set debugging for function > f then even if we reset its environment it still has debugging on. > Also if we copy f to g then g has debugging; however, if we change > g's environment then g no longer has debugging. Why did f retain > debugging when its environment was changed but g did not? >My guess, which could be verified by careful reading of the code, is as follows: - There is a debugging flag on each function object - Changing the environment does not change this flag - Assigning f to g makes g a reference to the same copy of f - Now there are two references to the same function, modifying the environment of either one causes copying, and the modified function will not have the debugging flag set. This correctly predicts that after f<-function(x) {x} g<-f debug(f) environment(f)<-new.env() the debugging flag will be set for g but not f, since f was modified. -thomas