[R] recover lost global function

Duncan Murdoch murdoch.duncan at gmail.com
Wed Apr 4 23:00:32 CEST 2012


On 12-04-04 4:52 PM, Sam Steingold wrote:
> Since R has the same namespace for functions and variables,
>> c<- 1
> kills the global function, which can be restored by
>> c<- get("c",mode="function")
>
> Is there a way to prevent R from overriding globals
> or at least warning when I do that
> or at least warning when I replace a functional value with non-functional?

It doesn't kill it, it just hides it.  You can still get the original by 
telling R which one you want, e.g. base::c.

You'll get a warning when you do this in a package, e.g. library(Hmisc) 
will tell you that it has hidden 5 functions from view.

There's no warning when you mask a function with a non-function at top 
level, and little need for one, because R does the right search based on 
the fact that you're making a function call:

 > c
[1] 1
 > c(1,2)
[1] 1 2

It only matters when you need to pass the function as an argument, e.g. 
to one of the apply() family of functions.

Duncan Murdoch



More information about the R-help mailing list