How to Unload CRAN Packages in R

By loading a package, R first loads the package and then attaches the package and to your search path, which you can see as an internal database that tells R where to find functions and objects. Whenever R evaluates a variable (or function), it tries to find that variable (or function) in the search path. To list the packages that are loaded in the search path, use the search() function:

> search()

To unload a package from the search path, use detach(). The argument to detach() is the name of the package, preceded by package:, like this:

> detach(package:fortunes, unload=TRUE)

Note that you need to specify the argument unload=TRUE; otherwise, R removes the package from the search path but doesn’t unload it.

When you specify the argument unload=TRUE, R attempts to unload the package from memory. This is only an attempt — unloading can fail for many reasons, for example if you’ve loaded additional packages with dependencies, this unload will fail. If you really want to be sure that a package is no longer loaded, your best option is to simply start a new R session.

Because the authors of R packages work independently, it’s entirely possible for different authors to use the same function names and packages. If this happens, the package that was loaded last masks functions with the same name in packages that were loaded first. R gives you a message saying which objects were masked from other packages the moment this happens.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.