0

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.

Kaleb Coberly
  • 420
  • 1
  • 4
  • 19
  • 5
    You can use, e.g., `dplyr::mutate()` or `plyr::mutate()` to explicitly reference a package in a function call. – Gregor Thomas Dec 16 '20 at 18:37
  • 2
    Also I think your underselling the `conflicted` package, it offers functionality beyond transparency. Maybe you're confusing it with the base `conflicts()` function that only offers transparency? As [the package vignette shows](https://github.com/r-lib/conflicted) you can, e.g., use `conflicted::conflict_prefer("filter", "dplyr")`. And at the end of the (short) vignette there's an *Alternatives* section that mentions two other packages you could look at, `import`, and `modules`, both of which give a more Python-like solution. – Gregor Thomas Dec 16 '20 at 18:42
  • @GregorThomas, thanks! That's exactly what I was looking for. Haha, looking through the conflicts package info I incidentally stumbled upon that syntax for the first time. I'm only a week into R, lol, and I think it shows. – Kaleb Coberly Dec 16 '20 at 18:50
  • @GregorThomas, yeah, I will definitely look further into `conflicted`. – Kaleb Coberly Dec 16 '20 at 18:51

1 Answers1

0

Just prefix the package name with a double colon:

<package>::<function>()

For instance:

ggplot2::ggplot(data=data, ggplot2::aes(x=x)) +
    ggplot2::geom_histogram()
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Kaleb Coberly
  • 420
  • 1
  • 4
  • 19