This is not a better answer than jkt's, but I was puttering anyway because AK88's findAllFun
intrigued me as a way of finding all of the functions on that may be loaded in the search path (though it should be noted that AK88's function seems to return all of the packages in the library that have the function name in its namespace).
Anyway, here is a function that will return a vector of package names that contained a function of a desired name. Most importantly, it orders the package names in the order of the search path. That means that if you just type function_name
, the first package in which the function will be encountered is the first package in the result.
locate_function <- function(fun, find_in = c("searchpath", "library")){
find_in <- match.arg(arg = find_in,
choices = c("searchpath", "library"))
# Find all libraries that have a function of this name.
h <- help.search(pattern = paste0("^", fun, "$"),
agrep = FALSE)
h <- h$matches[,"Package"]
if (find_in == "library") return(h)
# List packages in the search path
sp <- search()
sp <- sp[grepl("^package", sp)]
sp <- sub("^package[:]", "", sp)
# List the packages on the search path with a function named `fun`
# in the order they appear on the search path.
h <- h[h %in% sp]
h[order(match(h, sp, NULL))]
}
## SAMPLE OUTPUT
library(dplyr)
library(MASS)
locate_function("select")
# [1] "MASS" "dplyr"
## Unload the dplyr package, then reload it so.
## This makes it appear BEFORE MASS in the search path.
detach("package:dplyr", unload = TRUE)
library(dplyr)
locate_function("select")
# [1] "dplyr" "MASS"
It also includes an option that lets you see all of the packages (even unloaded packages) that contain a function of the desired name.
locate_function("select", find_in = "library")
# [1] "dplyr" "raster" "MASS"