How to unmask a function in R, due to name collisions on searchpath
Asked Answered
E

3

12

When I loaded package debug to debug a script with zoo objects, I got trouble: function index from zoo got masked by debug package. How can I unmask index? In general, how to deal with these name colliding problems? We just do not use debug package with `zoo'?

Earshot answered 13/7, 2010 at 21:8 Comment(0)
S
11

Exported symbols are always identifiable with the :: operator:

zoo::index

Hidden functions not declared in the namespace can still be accessed using ::: (triple-colon), and example would be

zoo:::.onLoad

which you can see even though it is not exported.

Skin answered 13/7, 2010 at 21:13 Comment(2)
On the other hand this is the reason why setting system<-function(){} does not magically make R secure.Praedial
thanks. I can temperarily set index<-zoo::index to work around this problem without changing my original script. It seems some packages just do not work together.Earshot
A
15

You can detach the package which has masked functions and then reattach it. It will regain precedence in the search path:

detach("package:zoo")
library("zoo")

In the future, if you want to attach a package while preventing it from masking other functions, you can specify its position in the search path with an arbitrary large number:

library("debug", pos = .Machine$integer.max)
Anticathexis answered 8/5, 2013 at 21:10 Comment(0)
S
11

Exported symbols are always identifiable with the :: operator:

zoo::index

Hidden functions not declared in the namespace can still be accessed using ::: (triple-colon), and example would be

zoo:::.onLoad

which you can see even though it is not exported.

Skin answered 13/7, 2010 at 21:13 Comment(2)
On the other hand this is the reason why setting system<-function(){} does not magically make R secure.Praedial
thanks. I can temperarily set index<-zoo::index to work around this problem without changing my original script. It seems some packages just do not work together.Earshot
H
4

Its only masked to you but its not masked to zoo so when a zoo function tries to use index it will still find its own index first.

zoo also has a time.zoo method so if z is a zoo object you can use time(z) in place of index(z).

Finally you can always refer to zoo::index to make sure you get the one in zoo.

Heavyhanded answered 14/7, 2010 at 0:28 Comment(2)
thanks. time.zoo will work but I dont want to change my original script. And index(zooobject) reports error in my case. Apperently R doesnot know index.zoo should be called on zooobject.Earshot
If you load zoo after loading debug/mvbutils rather than the other way around then the index in zoo will be the one you get by default rather than the one in debug/mvbutils.Heavyhanded

© 2022 - 2024 — McMap. All rights reserved.