Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unload a package without restarting R

Tags:

package

r

r-faq

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?

like image 724
Ari B. Friedman Avatar asked Aug 08 '11 09:08

Ari B. Friedman


People also ask

How do I unload a package in R?

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.

How do I see loaded packages in R?

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.


2 Answers

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) 
like image 194
kohske Avatar answered Oct 24 '22 04:10

kohske


You can also use the unloadNamespace command, as in:

unloadNamespace("sqldf") 

The function detaches the namespace prior to unloading it.

like image 45
rstober Avatar answered Oct 24 '22 04:10

rstober