In R, what does “loaded via a namespace (and not attached)” mean?

后端 未结 2 1416
南笙
南笙 2020-12-08 19:21

In R, what does it mean for a package to be \"loaded via a namespace (and not attached)\" in sessionInfo()?

Edit:

相关标签:
2条回答
  • 2020-12-08 19:48

    It means the package (In this case R) can access the package functions/objects, but the user can not without explicitly loading the tools package where as stats, graphics, etc. are loaded and ready to go for the user.

    Here's an example:

    sessionInfo()
    file_ext("file.com")
    tools::file_ext("file.com")
    sessionInfo()
    
    0 讨论(0)
  • 2020-12-08 19:50

    When one evaluates library(foo), R first loads the package foo in to memory, and then attaches the package to the search() path. Other operations, such as loadNamespace("foo") or foo::fun, or when a third party indicates that it imports symbols from foo, load the package but do not attach it to the search path. Since R is a dynamic language, each function call involves a traversal of the available symbols to find the first that matches. It is efficient, and avoids unnecessary name conflicts, by separating the attach and load operations and hence restricting the number of symbols to search.

    In the example above, the tools package has been loaded, but not (yet) attached. When one types a symbol at the R command prompt, R looks for the symbol first in the global name space (the first element returned by search() and if not found then in successive elements of search(). Since tools isn't attached, the symbols in tools are not resolved.

    > file_ext
    Error: object 'file_ext' not found
    

    Nonetheless, one has access to them with tools::file_ext, whether tools is on the search path or not.

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