Hi all, I'm having sometrouble with managing the seach path, in a function , I need to attach some data set at the begining and detach them at the end, say, myfunction<- function() { attach(mylist); ............. detach(mylist) } , the problem is, since I am still debugging this code, sometimes it got error and ended before reaching the end, thus the data is left in the searching path. What 's the right way to make mylist detached no matter what ? Thanks a lot. best
Dear Tong I think on.exit() makes the job..Namely: attach(Yourdata) on.exit(detach(YourData)) vito Tong Wang wrote:> Hi all, > I'm having sometrouble with managing the seach path, in a function , I need to attach some data set at the begining > and detach them at the end, say, myfunction<- function() { attach(mylist); ............. detach(mylist) } , > the problem is, since I am still debugging this code, sometimes it got error and ended before reaching the end, thus > the data is left in the searching path. > What 's the right way to make mylist detached no matter what ? > > Thanks a lot. > > best > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- ===================================Vito M.R. Muggeo Dip.to Sc Statist e Matem `Vianelli' Universit? di Palermo viale delle Scienze, edificio 13 90128 Palermo - ITALY tel: 091 6626240 fax: 091 485726/485612
On 10/17/2006 3:49 AM, Tong Wang wrote:> Hi all, > I'm having sometrouble with managing the seach path, in a function , I need to attach some data set at the begining > and detach them at the end, say, myfunction<- function() { attach(mylist); ............. detach(mylist) } , > the problem is, since I am still debugging this code, sometimes it got error and ended before reaching the end, thus > the data is left in the searching path. > What 's the right way to make mylist detached no matter what ?on.exit, as Vito said. But you may find that doing the calculations in "with" does a better job, i.e. with(mylist, [do something]) The advantages of with() are: - it takes precedence over local variables; attach (by default) comes behind local variables and the global environment. This may mean your code fails when a user happens to have variables with the same name defined. - it is a temporary change, so no "detach" is needed. Duncan Murdoch