I need to load and detach a lot of packages in one R session (I'm looking at which functions are methods across different packages). detach()
doesn't work for what I want, because it doesn't remove everything from the environment; for example, if you run:
require(pomp)
detach('package:pomp', character.only = TRUE)
print(methods('show'))
the show,pomp.fun-method
is still listed, which is not a method that exists in base R. How do I remove all methods and objects associated with a package? Alternately, is there a way to create a temporary environment in R to load the package, which I can then destroy to remove all objects in methods in a package?
To try to unload the namespace that was loaded when loading a package, you have to set the argument unload = TRUE
in detach()
.
In your example:
detach('package:pomp', unload = TRUE, character.only = TRUE)
However, if you read the details in the docs (?detach
), there are some things to watch out for:
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.
Emphasis mine. Be wary that it may not always work.