How to Search the Web for Help with R
How to Use Internal Functions in R
How to Name the Values in Your Vectors in R

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
How to Create a Data Frame from Scratch in R
How to Use Data Tables in R
How to Combine Logical Statements in R
How to Use Logical Vectors as Indices in R
How to Do More with Loops in R
Advertisement

Inside Dummies.com