Given a function, how do you determine which namespace it has come from?
For example, if I type mean.default
at the command prompt, the output includes
getNamespaceName()
as proposed in @hadley's answer doesn't work when the function isn't in the current namespace.
The following does:
environmentName(findFunction(f = "str_detect",
where = getNamespace("stringr"))[[1]])
Set the where
argument according to your needs. Its documentation:
The environment, namespace, or search-list position from which to search for objects. By default, start at the top-level environment of the calling function, typically the global environment (i.e., use the search list), or the namespace of a package from which the call came. It is important to supply this argument when calling any of these functions indirectly. With package namespaces, the default is likely to be wrong in such calls.
You want getNamespaceName
+ environment
:
getNamespaceName(environment(mean.default))
# [1] "base"
I very recently learned about find()
which seems to do just this.
R> find("ls")
[1] "package:base"
R> find("na.locf")
[1] "package:zoo"
This function searches functions in the loaded namespaces and global environment:
getEnvName <- function(f) {
attached <- c(environmentName(.GlobalEnv), loadedNamespaces())
envs <- c(.GlobalEnv, lapply(attached[-1], .getNamespace))
attached[vapply(envs, function(env) exists(f, env, inherits = FALSE), logical(1))]
}
median <- function() {}
getEnvName("median")
#> [1] "R_GlobalEnv" "stats"
getEnvName(".try_quietly")
#> [1] "tools"
getEnvName("getEnvName")
#> [1] "R_GlobalEnv"
findFunction
is another option and ?findFunction
says you should use it instead of find
with mode="function"
. The result of findFunction
is a list of the environment(s) where the visible versions of the function are located.