42
votes

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 the fact that it is in the base package. I want to be able to do something like getNamespace(mean.default) and have it return "base" (or the actual base environment).

There is a getNamespace function but seems to only accept package names rather than function names.

print.function uses internal code to retrieve the namespace. I got as far as browsing do_printfunction in src/main/print.c but then gave up.

5
Three excellent answers; thanks all. Tough to choose between them since they all work, so I'm going with the popular vote.Richie Cotton

5 Answers

43
votes

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"
18
votes

You want getNamespaceName + environment:

getNamespaceName(environment(mean.default))
# [1] "base"
16
votes

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.

2
votes

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"
0
votes

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.