Installing, Loading, Unloading, and Removing R Packages in RStudio

R has thousands of packages available for statistics and data analytics, but before you can use them, they need to be installed. In this post I cover installing, loading, unloading, and removing R packages in RStudio. In these examples, I use the ggplot2 package – a popular graphics and visualization package in R. Wherever you see ggplot2 in the examples below, you can replace it with the package you want to perform these actions on.

To install a package via the User Interface

In RStudio, select Tools -> Install Packages from the main menu, or click Install in the Packages tab on the bottom-right.

The Install Packages dialog appears.

Start typing the name of the package you want to install, and a list of all packages that start with the letters you have type will show up in the selection list.

Select (or type the full name of) the package you want to install, ensure that “Install dependencies” is checked, and click “Install”.
The statement will be automatically entered and run as shown below.

And the output will show if the package is successfully installed.

At this point, you will be able to see the package in the list in the Packages tab on the right.

To install a package via script
Instead of using the user interface (menu), you can also install packages directly via script.

install.packages("ggplot2")

See script statement below. And as before, the package shows in the Packages tab.

After a package has been installed, it needs to be loaded before you can use it.

Loading and Unloading a package (via user interface or script):

To load a package you can simply check the checkbox beside the package name in the Packages tab – as shown by the yellow box highlight below. This will automatically enter and execute the command shown with the yellow arrow.

Or you can enter the script, by entering the command as shown with the green arrow:

library("ggplot2")

As an alternative, the require(ggplot2) command will also load the package.

To unload the package, you can simply uncheck the checkbox beside the package name in the Packages tab, or enter the command shown by the red arrow:

detach("package:ggplot2", unload=TRUE)

You will notice that after running the detach command, the Package checkbox will not be checked (will be unchecked).

To remove (uninstall) a package (via user interface or script):

To remove a package, you can simply click the “x” icon shown to the right of the package in the Packages window. See the yellow box highlight beside ggplot2 below.

Or you can run the script command as shown below in the Console window.

remove.packages("ggplot2")

The below shows the output after removing a package. You will notice that the package is no longer in the Packages list on the right hand side. In this example, ggplot2 is no longer in the list of packages.

An advantage of using the script option instead of the user interface methods to perform the above actions is that you will have a history of what you have done.