10

I'm debugging some code and I think I might have twice the same function in 2 packages.

I want to output the package name of the function as it would be executed by the R console.

Examples :

  • function_package_name(print) # --> base
  • function_package_name(select) # --> dplyr

I cannot simply use ?select because I think it links to the choice of 2 packages: dplyr and MASS.

How can I know which select function I'm using ?

NB : this is NOT a duplicate of list all functions on CRAN, nor of Find the package names from a function name in R, so sos::findFn() is not an acceptable answer ! I'm not looking for potential other functions named like this one, I'm looking for the package name of the one I'm currently using !

Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
  • Have you tried `findAllFun <- function(f) { h <- help.search(paste0("^",f,"$"),agrep=FALSE) h$matches[,"Package"] } findAllFun("qplot")` from https://stackoverflow.com/questions/10553755/name-of-a-package-for-a-given-function-in-r – AK88 Jun 22 '17 at 10:16
  • 1
    if you want to use a specific function you can use `package::function`, usually it is the function of the package you've last loaded I think. Also you can just type the function name and you'll see where it comes from by the environment (eg with `select`, you get ``) – Cath Jun 22 '17 at 10:20
  • @AK88 `findAllFun("select")` returns `"dplyr" "MASS" `, which doesn't help me much – Dan Chaltiel Jun 22 '17 at 10:20
  • not using `findAllFun` which, as you can get from the name, gives all select function in your session. Just type `select` to see the one that is "by default" used in your session – Cath Jun 22 '17 at 10:21
  • 1
    @Cath this is my answer ! just typing the function name, this was ridiculously easy :-) – Dan Chaltiel Jun 22 '17 at 10:22
  • 1
    perhaps `environment(select)` might be more convenient? – jkt Jun 22 '17 at 10:42

3 Answers3

19

Perhaps even most convenient, if you are just after the package name:

environmentName(environment(select))

The advantage is that this produces a string rather than an environment object.

jkt
  • 946
  • 1
  • 7
  • 18
2

Try the function where from the pryr package

> library(dplyr)
> pryr::where("select")
<environment: package:dplyr>
attr(,"name")
[1] "package:dplyr"
attr(,"path")
[1] "/home/francois/.R/library/dplyr"
> library(MASS)
> pryr::where("select")
<environment: package:MASS>
attr(,"name")
[1] "package:MASS"
attr(,"path")
[1] "/home/francois/.R/library/MASS"
fmic_
  • 2,281
  • 16
  • 23
2

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" 
Benjamin
  • 16,897
  • 6
  • 45
  • 65