Sunday, October 28, 2018

How to unload a package without restarting R



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) : invalid name argument




detach("vegan")



Error in detach("vegan") : invalid name argument




So how do I unload a package?


Answer



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 detach 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)

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...