Today I learned of the utils::getAnywhere()
function in R, which I discovered when looking for a way to determine which function is going to be used when two attached packages have the same function name. I knew of base::search()
which provides the search path used by R to look for objects (remember that everything in R is an object). If package A and B both have the select()
function but package A is loaded after package B, then package A's select()
function will be used because the search path will start with package A. Let's illustrate with an example.
AnnotationDbi and dplyr are two packages that I use, so I know that they both have a select()
function. If we load them in that order, we will see that the search path is in the reverse order because R will look in the most recently loaded package.
library(AnnotationDbi)
library(dplyr)
base::search()
[1] ".GlobalEnv" "package:dplyr" "package:AnnotationDbi" "package:IRanges"
[5] "package:S4Vectors" "package:Biobase" "package:BiocGenerics" "package:stats4"
[9] "tools:rstudio" "package:stats" "package:graphics" "package:grDevices"
[13] "package:utils" "package:datasets" "package:methods" "Autoloads"
[17] "package:base"
If we load it the other way around, we will see that the search path has changed.
library(dplyr)
library(AnnotationDbi)
base::search()
[1] ".GlobalEnv" "package:AnnotationDbi" "package:IRanges" "package:S4Vectors"
[5] "package:Biobase" "package:BiocGenerics" "package:stats4" "package:dplyr"
[9] "tools:rstudio" "package:stats" "package:graphics" "package:grDevices"
[13] "package:utils" "package:datasets" "package:methods" "Autoloads"
[17] "package:base"
The function utils::getAnywhere()
goes one step beyond and can be used to locate all objects with a matching name, whether visible on the search path, registered as an S3 method or in a namespace but not exported.
utils::getAnywhere("select")
4 differing objects matching ‘select’ were found
in the following places
package:AnnotationDbi
package:dplyr
namespace:AnnotationDbi
namespace:MASS
namespace:dplyr
namespace:tidyselect
Use [] to view one of them
Again the order indicates which attached package will be searched for first.
library(AnnotationDbi)
library(dplyr)
utils::getAnywhere("select")
4 differing objects matching ‘select’ were found
in the following places
package:dplyr
package:AnnotationDbi
namespace:AnnotationDbi
namespace:dplyr
namespace:MASS
namespace:tidyselect
Use [] to view one of them
Pretty cool hey!

This work is licensed under a Creative Commons
Attribution 4.0 International License.