Functions with the same (identical) names in R packages

Hi,
I have such a question as how to check in R, which packages in my current R session that are listed in
sessionInfo() contain functions with exactly the same names, e.g:

  1. flextable::border() and ggpubr::border()
  2. dplyr::intersect() and base::intersect()
  3. Hmisc::is.discrete() and plyr::is.discrete()

How to check it out in order to know in advance, that RStudio at startup loaded packages with the same functions' names and it could cause conflicts/errors?

So eg. I want to use a function count() and in what currently loaded package is it in ?

best regards,

Andrzej

1 Like

When you load packages with conflicting names, you are getting the version of last loaded package. To avoid confusion, I would suggest to always use qualified imports, e.g., dplyr::select. This way you don't need to care/remember which package is loaded when.

Another way is to use conflicted package (https://github.com/r-lib/conflicted#conflicted). There, you either explicitly say which functions should be used when or you get an error when you encounter an ambiguous function.

4 Likes

The base function conflicts() will list duplicates in the search path. Combine that result with find() (documented in utils::apropos) and I think it answers your question.

sapply(conflicts(), find)
5 Likes

Thank you very much indeed Mishabalyasin and Grosscol. Misha's solution is very clear as it gives explicit output:

Grosscol's solution - I wish it worked in a simpler way but it gives a little bit vague output:
List of 2
axis.line : list() ..- attr(*, "class")= chr [1:2] "element_blank" "element" panel.background:List of 5
.. fill : NULL .. colour : chr [1:356]
..$ size :function (what, mode = "any", numeric = FALSE, simple.words = TRUE)

and more text like this one above, etc.

Anyway thank you both for fast response and help.

kind regards,
Andrzej

3 Likes

Huh. That's peculiar. I wonder if some package conflicts changed which find or conflicts were being called. I get a pretty neat matrix back:

     filter          lag             body<-            intersect       kronecker         setdiff         setequal        union          
[1,] "package:dplyr" "package:dplyr" "package:methods" "package:dplyr" "package:methods" "package:dplyr" "package:dplyr" "package:dplyr"
[2,] "package:stats" "package:stats" "package:base"    "package:base"  "package:base"    "package:base"  "package:base"  "package:base" 

What does the list applied version give you?

conflicted <- base::conflicts()
which_packages_by_function <- lapply(conflicted, utils::find)
names(which_packages_by_function) <- conflicted
which_packages_by_function
#$filter
#[1] "package:dplyr" "package:stats"
#
#$lag
#[1] "package:dplyr" "package:stats"
#
#$`body<-`
#[1] "package:methods" "package:base"   
#
# ...

Now with your code this looks much better and it shows all conflicts as a list. The list is big as I have many packages installed.

my_conflicts <- which_packages_by_function

and:

But I would like to get such a matrix as you have got, with all packages divided by:
filter lag body<- intersect kronecker setdiff setequal union.

How can I do that ?

That's odd that our results are so different. I don't know what is going on in the environment. You might try clearing your environment, only loading a couple of packages like dplyr and tidyr, then re-running the conflict listing code. See if that gets your results closer to what I'm seeing.

To get a matrix output, you might try running the sapply approach and namespace the references to the functions base::conflicts() and utils::find.

Also, please copy and paste your output using this forum's formatting blocks. Posting images of text is difficult for me to read.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.