Detach All User-Installed Packages in R (Example)

 

In this tutorial you’ll learn how to unload all currently loaded add-on packages in the R programming language.

Table of contents:

So now the part you have been waiting for – the examples…

 

Setting Up the Example

First, let’s check the currently loaded packages after restarting RStudio:

(.packages())                                                          # Return currently loaded packages
# "grid"      "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"

As you can see based on the previously shown output of the RStudio console, there are several packages loaded, even though we have just restarted R. These packages are Base R Packages such as stats or methods. These packages are always loaded at the startup of R.

Let’s load some additional packages…

library("ggplot2")                                                     # Load additional packages
library("dplyr")

…and then let’s check the list of currently loaded packages again:

(.packages())                                                          # Return currently loaded packages
# "dplyr"     "ggplot2"   "grid"      "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"

As you can see, the list of currently loaded R packages increase by ggplot2 and dplyr.

 

Example: Unload All Add-On Packages

Let’s assume that we want to detach all user-installed packages that we have manually loaded in the current session (i.e. ggplot2 and dplyr). Then, we can use the following R programming code:

invisible(lapply(paste0("package:", names(sessionInfo()$otherPkgs)),   # Unload add-on packages
                 detach,
                 character.only = TRUE, unload = TRUE))

Let’s check the list of currently loaded packages again:

(.packages())                                                          # Return currently loaded packages
# "grid"      "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"

As you can see, the updated list contains only the base packages that were loaded at the beginning of the session. The ggplot2 and dplyr packages were unloaded.

 

Video & Further Resources

Do you want to learn more about detaching packages in R? Then I can recommend watching the following video of my YouTube channel. I’m explaining the examples of this tutorial in the video:

 

The YouTube video will be added soon.

 

In addition, you may read the other articles of my homepage. Please find some articles here:

 

In summary: At this point of the post you should have learned how to remove attached packages in R. Don’t hesitate to let me know in the comments section, if you have additional questions or comments. In addition, please subscribe to my email newsletter in order to get updates on new tutorials.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top