Can you manage R package conflicts by explicitly referencing packages in function calls?

2.2k Views Asked by At

I don't like that you can have R package conflicts in function calls, or namespace collisions. For instance, plyr and dplyr have functions with the same names, so if you have them both loaded you need to know which functions these are so you can attach and detach packages appropriately. Sure, in this example, dplyr is meant to replace plyr which is why conflicts emerge, but this can hypothetically happen with any number of packages. What a nightmare! This isn't an issue in Python because you prefix the function call with the name/alias of the package you imported, e.g. pd.melt().

So, my question is: is there any equivalent way to do this in R? Can you manage package conflicts by explicitly referencing the package in a function call?

I see someone asked basically the same question here six years ago, and it remains unsolved. The only answer offered is to check out the conflicted package. This is a start, lending transparency to the conflicts, but you have to dig through the package to find anything better than that (update: see comments below to find references to which features of the package are most useful for this issue).

Update

There are a number of solutions in the comments, including those found in this post. But, while that post answers my question, it doesn't ask it, which might be part of the reason I didn't find it in my search. It starts with the assumption that you already know the quick and dirty solution of using the proper prefix syntax. So, it might be best to leave this post up as a non-duplicate for future searchers.

1

There are 1 best solutions below

0
On

Just prefix the package name with a double colon:

<package>::<function>()

For instance:

ggplot2::ggplot(data=data, ggplot2::aes(x=x)) +
    ggplot2::geom_histogram()