How do you determine the namespace of a function?

后端 未结 5 519
予麋鹿
予麋鹿 2020-11-30 05:48

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

相关标签:
5条回答
  • 2020-11-30 06:00

    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 讨论(0)
  • 2020-11-30 06:07

    You want getNamespaceName + environment:

    getNamespaceName(environment(mean.default))
    # [1] "base"
    
    0 讨论(0)
  • 2020-11-30 06:08

    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"
    
    0 讨论(0)
  • 2020-11-30 06:08

    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 讨论(0)
  • 2020-11-30 06:19

    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.

    0 讨论(0)
提交回复
热议问题