A namespace question

Let's say I have three packages A, B, and C. All of them have a function with the same name filter but with different functionalities. I tend to use all these 3 packages in my work, and sometimes I couldn't remember the warning message about overriding. So if I use the function filter, how can I know which package it is from. I understand that can use A::filter, B::filter, or C::filter, but I want to know if my desired filter function is being used ex ante.

Have you tried using package_name::filter()? So, for example, you could use stats::filter() when you want to use filter() from stats, and you can do dplyr::filter() for when you want to use filter() from dplyr. That way, your script always knows which package to go to.

Also, this looks like a perfect case for using the conflicted package :slight_smile:

2 Likes

As @eoppe1022 mentioned, you can declare a session-wide preference with conflicted by using the conflict_prefer() function.

conflict_prefer("filter", "dplyr")

More generally speaking, whatever package/library you attach last (most recently, if you're thinking about it temporally) will have precedence in your search path (see fig from Advanced R | Environments, below)

2 Likes

This RStudio Addin might also be of interest:

1 Like

I often include a statement after my library ones along the lines of filter = dplyr::filter. I have wondered if there are any murky risks to doing that, though. Is using conflict_prefer() safer?