Reputation: 155
I recently ran into a situation in which existing R code broke due the introduction of the dplyr
library. Specifically, the lag
function from the stats package, is being replaced by dplyr::lag
. The problem is previously documented here, however no work around is provided. Research into R namespaces and environments leads to 2 possible solutions, neither very robust in my opinion:
package:stats
appears first in the search()
path so that lag
resolves as the function in the stats package.lag
in my code to stats::lag
My question is whether either of these other solutions are possible:
dplyr
package in a way to force it to be in a "private" namespace in which its objects can only be accessed through the ::
operator.lag
to resolve as stats::lag
. This could be done either by removing dplyr::lag
or overriding the search path (similar to the C++ using namespace::function directive.)Upvotes: 11
Views: 2637
Reputation: 351
you should consider library(conflicted)
as it's designed for exactly this problem.
https://cran.r-project.org/web/packages/conflicted/index.html
putting conflicted::conflict_prefer(name = "lag", winner = "stats")
after you load your packages ensures that anytime the function lag()
is called in your script, it will use the stats function by default.
Upvotes: 6