I'd like to unload a package without having to restart R (mostly because restarting R as I try out different, conflicting packages is getting frustrating, but conceivably this could be used in a program to use one function and then another--although namespace referencing is probably a better idea for that use).
?library
doesn't show any options that would unload a package.
There is a suggestion that detach
can unload package, but the following both fail:
detach(vegan)
Error in
detach(vegan)
: invalidname
argument
detach("vegan")
Error in
detach("vegan")
: invalidname
argument
So how do I unload a package?
To detach a package in R, we can simply use the detach function. But we need to remember that once the package will be detached there is no way to use any of the functions of that particular package.
1 Answer. You can use the packageVersion() function to print version information about the loaded packages. To print the version information about R, the OS and attached or loaded packages, use the sessionInfo() function.
Try this (see ?detach
for more details):
detach("package:vegan", unload=TRUE)
It is possible to have multiple versions of a package loaded at once (for example, if you have a development version and a stable version in different libraries). To guarantee that all copies are detached, use this function.
detach_package <- function(pkg, character.only = FALSE) { if(!character.only) { pkg <- deparse(substitute(pkg)) } search_item <- paste("package", pkg, sep = ":") while(search_item %in% search()) { detach(search_item, unload = TRUE, character.only = TRUE) } }
Usage is, for example
detach_package(vegan)
or
detach_package("vegan", TRUE)
You can also use the unloadNamespace
command, as in:
unloadNamespace("sqldf")
The function detaches the namespace prior to unloading it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With