Protecting function names in R

Is it possible to protect function names (or variables in general) in R so that they cannot be hidden.

I recently noticed that this could be a problem when creating a data frame named "new", which masked the function used by lmer, and thus stopped its operation. (Restoring is easy, once you know what the problem is, here "rm (new)" did it.)

+7
r
source share
3 answers

There is an easy workaround for your problem without worrying about protecting variable names (although playing with lockBinding really looks fun). If the function is masked, as in your example, you can still call the mask version using the :: operator.

In general, the syntax is packagename::variablename .

(If the function you want was not exported from the package, then you need three colons, ::: . However, this should not be applied in this case.)

+6
source share

Use of the medium is possible! This is a great way to split namespaces. For example:

 > a <- new.env() > assign('printer', function(x) print(x), envir=a) > get('printer', envir=a)('test!') [1] "test!" 
+4
source share

@hdallazuanna recommends (via Twitter)

 new <- 1 lockBinding('new', globalenv()) 

this makes sense when the variable is created by the user, but, of course, does not allow you to rewrite the function from the package.

+2
source share

All Articles