Grant Rettke
2014-Aug-05 18:49 UTC
[Rd] Is it a good idea or even possible to redefine attach?
Hi, Today I got curious about whether or not I /could/ remove `attach' from my system so: - Backed it up - Implemented a new one - Like this ,---- | attach.old <<- attach | attach <<- function(...) {stop("NEVER USE ATTACH")} `---- I got the error: ,---- | Error: cannot change value of locked binding for 'attach' `---- If I unlock `attach' I assume that I could stomp on it... however is that even a good idea? What will I break? My goal was never to allow `attach' in my system, but it was just an idea. Kind regards, Grant Rettke | ACM, ASA, FSF, IEEE, SIAM gcr at wisdomandwonder.com | http://www.wisdomandwonder.com/ ?Wisdom begins in wonder.? --Socrates ((? (x) (x x)) (? (x) (x x))) ?Life has become immeasurably better since I have been forced to stop taking it seriously.? --Thompson
Ista Zahn
2014-Aug-05 19:18 UTC
[Rd] Is it a good idea or even possible to redefine attach?
On Tue, Aug 5, 2014 at 2:49 PM, Grant Rettke <gcr at wisdomandwonder.com> wrote:> Hi, > > Today I got curious about whether or not I /could/ remove `attach' from > my system so: > - Backed it up > - Implemented a new one > - Like this > > ,---- > | attach.old <<- attach > | attach <<- function(...) {stop("NEVER USE ATTACH")} > `----Just masking it with your own function, e.g., attach <- function(...) {stop("NEVER USE ATTACH")} should be enough to discourage you from using it.> > I got the error: > > ,---- > | Error: cannot change value of locked binding for 'attach' > `---- > > If I unlock `attach' I assume that I could stomp on it... however is > that even a good idea? > > What will I break?Anything that uses attach.> > My goal was never to allow `attach' in my system, but it was just an > idea. > > Kind regards, > > Grant Rettke | ACM, ASA, FSF, IEEE, SIAM > gcr at wisdomandwonder.com | http://www.wisdomandwonder.com/ > ?Wisdom begins in wonder.? --Socrates > ((? (x) (x x)) (? (x) (x x))) > ?Life has become immeasurably better since I have been forced to stop > taking it seriously.? --Thompson > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Winston Chang
2014-Aug-05 19:47 UTC
[Rd] Is it a good idea or even possible to redefine attach?
On Tue, Aug 5, 2014 at 1:49 PM, Grant Rettke <gcr at wisdomandwonder.com> wrote:> > Hi, > > Today I got curious about whether or not I /could/ remove `attach' from > my system so: > - Backed it up > - Implemented a new one > - Like this > > ,---- > | attach.old <<- attach > | attach <<- function(...) {stop("NEVER USE ATTACH")} > `---- > > I got the error: > > ,---- > | Error: cannot change value of locked binding for 'attach' > `---- > > If I unlock `attach' I assume that I could stomp on it... however is > that even a good idea? > > What will I break?If you change the base package environment's copy of `attach` (via `as.environment('package:base')`) , probably not much will break, except for your own code. If, on the other hand, you change the base namespace's copy of `attach` (via `asNamespace('base')`, any package that's subsequently loaded and uses `attach` would run into problems. Still, either way is probably not a good idea. I agree with Ista: assigning it yourself in the the global environment is a better idea. If you want to keep your global env clear of stuff like this, you can put it in an environment and attach that environment as a parent of global: e <- new.env() e$attach <- function(...) {stop("NEVER USE ATTACH")} base::attach(e, name = "my_stuff", warn.conflicts = FALSE) # Test it out: attach() # You can see that the "my_stuff" env is the parent of global env search() -Winston