How to unload a package without restarting R

ID : 3317

viewed : 173

Tags : rpackager-faqr

Top 5 Answer for How to unload a package without restarting R

vote vote


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", TRUE) 
vote vote


You can also use the unloadNamespace command, as in:


The function detaches the namespace prior to unloading it.

vote vote


You can uncheck the checkbox button in RStudio (packages).

RStudio packages pane

vote vote


I tried what kohske wrote as an answer and I got error again, so I did some search and found this which worked for me (R 3.0.2):

require(splines) # package detach(package:splines) 

or also

library(splines) pkg <- "package:splines" detach(pkg, character.only = TRUE) 
vote vote


When you are going back and forth between scripts it may only sometimes be necessary to unload a package. Here's a simple IF statement that will prevent warnings that would appear if you tried to unload a package that was not currently loaded.

if("package:vegan" %in% search()) detach("package:vegan", unload=TRUE)  

Including this at the top of a script might be helpful.

I hope that makes your day!

Top 3 video Explaining How to unload a package without restarting R