Search code examples
rnamespacesr-environment

How to check if an environment is a package namespace


I am looking for a way to detect if an environment is a package namespace. Desired behavior:

is.namespace(environment(data.frame))
## [1] TRUE
is.namespace(environment(ggplot2::ggplot))
## [1] TRUE
is.namespace(globalenv())
## [1] FALSE
is.namespace(new.env(parent = globalenv()))
## [1] FALSE

Solution

  • Turns out there is such a function. But for reasons that I don't understand it is named isNamespace instead of is.namespace.

    > isNamespace(environment(data.frame))
    [1] TRUE
    

    More information can also be found in the related question here: How to distinguish package namespace environment from other environment objects