我需要在一个 R session 中加载和分离大量包(我正在查看哪些函数是跨不同包的方法)。 detach()
对我想要的不起作用,因为它不会从环境中删除所有内容;例如,如果你运行:
require(pomp)
detach('package:pomp', character.only = TRUE)
print(methods('show'))
show,pomp.fun-method
仍然在列表中,这不是基础 R 中存在的方法。如何删除与关联的所有 方法和对象一袋?或者,有没有办法在 R 中创建一个临时环境来加载包,然后我可以销毁它以删除包中方法中的所有对象?
要尝试卸载在加载包时加载的命名空间,您必须在 detach()
中设置参数 unload = TRUE
。
在你的例子中:
detach('package:pomp', unload = TRUE, character.only = TRUE)
但是,如果您阅读了文档中的详细信息 (?detach
),则需要注意一些事项:
If a package has a namespace, detaching it does not by default unload
the namespace (and may not even with unload = TRUE), and detaching
will not in general unload any dynamically loaded compiled code
(DLLs). Further, registered S3 methods from the namespace will not be
removed. If you use library on a package whose namespace is loaded, it
attaches the exports of the already loaded namespace. So detaching and
re-attaching a package may not refresh some or all components of the
package, and is inadvisable.
强调我的。请注意它可能并不总是有效。