开发者

How do you determine the namespace of a function?

Given a function, how do you determine which namespace it has come from?

开发者_如何学Go

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.


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"


You want getNamespaceName + environment:

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


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.


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"


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜