How to uninstall an R package

There are times in R programming when it is necessary to uninstall a package. It is a surprisingly straightforward process; in fact, it is sometimes harder to find the correct package in the first place than it is to uninstall it. However, when you are done using a package you may not want to keep it hanging around on your computer. As a result, it is helpful to know how to uninstall a package, when you no longer have a use for it.

What are packages and how do they work in the R environment

An R package is a prepackaged collection of functions and datasets that helped supply more functionality to the R programming language. Datasets are often provided as part of these collections to supply test data as you learn to use the functions in the package. Sometimes those datasets will have information that can be used for other purposes. You can think of an R package as a toolbox that you can add to your R programs to be able to process data in ways not available, or difficult to do using the functions that come with the R programming language.

When you install a package in R, you get more than just that package but any packages that it has a dependency with as well. This ensures that the package you are installing will be fully functional as soon as you have installed it. These dependencies occur because you can always use functions from one package to create new functions to be included with another in a new package.

After you have installed the package, it is not automatically included in your program. Because of this, you must add it using the library function with the format of library(name), where “name” is the name of the package being loaded.

Once you have loaded a package into your program you can access its functions and datasets. You can get a list of the datasets contained in the package by using the data function in the format of data(package=’name’) where “name” is the name of the package you want to look at. To get a list of the functions in a package use the help function in the form of help(package = name) where “name” is the name of the package you want to look at. The help function does more than bring up a list of the functions but includes a link to the help page on each of the functions so that you can learn how to use them.

Remove packages function

If you need to do an uninstall package operation from within your code, you need to use the remove packages function at the command prompt. This command has the format of remove.packages(“name”) where “name” is the package name to be removed. The following example illustrates the use of this function.

> remove.packages(“dplyr”)
Removing package from ‘C:/Users/ccrea/Documents/R/win-library/3.6’
(as ‘lib’ is unspecified)

This simple function easily uninstalls the selected package. Once you have applied this function to a package that you had previously installed it will no longer be available for use. If you load a program that you had previously written using the functions from a package that you have uninstalled you will get error messages. If you want to use the functions again you will have to reinstall the package.

As you can see from the above example, uninstalling a package does not take long. In fact, it often takes less time than it does to originally install the package. The main reason it often takes longer to install a package than it does to uninstall it is the fact that uninstalling a package does not require the time or process involved in downloading the package and unpacking it for installation. The remove packages function usually works quickly and easily. The package removal process will render the functions defined by the package nonfunctional.

If you are not careful when uninstalling a package, you could find yourself having to deal with error messages if you try to run a program that you used that package in. Because uninstalling a package makes it unusable you need to make sure that you remove any code that uses it. When removing a package removing the code that uses it will help you to avoid getting errors from trying to run nonexistent functions. It is important to make sure that you have any packages installed that you need to run a given program before you run it.

Common issues

There are some common issues that can arise when you uninstall a previously installed package.

> remove.packages(“dplyr”)
Removing package from ‘C:/Users/ccrea/Documents/R/win-library/3.6’
(as ‘lib’ is unspecified)
Error in remove.packages : there is no package called ‘dplyr’

If you try to access the package after uninstalling it, you will get an error message as seen in this example where we tried to uninstall a package that we have just uninstalled.

> library(dplyr)
Error in library(dplyr) : there is no package called ‘dplyr’

In this case, we tried connecting a package to our program that we had uninstalled. the result was an error message. This would be the most common issue you are likely to have after uninstalling a package. If you have any programs that were using that package and you try to run them, you will get error messages. These error messages will include a function not found error. This would be an easy problem to encounter because you could forget that you uninstalled the package or forget that that particular program uses the package that you just deleted. The way to avoid this problem is to go over your R programs to see if you still have any that use the package you are considering uninstalling. If you do, you may want to consider removing them along with it.

Sometimes a package does not uninstall when you run the command. The best solution for this issue is to manually delete the package. However, the computer may not let you do this while the R environment is still running. If you have this problem, you should shut down the R environment and then manually delete the package before starting up your R environment again. After this, when you open up your R environment, the package should be gone and you will find that you do not have access to it any longer.

Alternative Approach

If you are uninstalling a specific package, other than the remove package function there are a couple of alternative ways to do the job. One works with or without R studio, but the second one is an option available through R studio. This provides you with multiple options for uninstalling a package if you run into problems.

The first alternative is to simply locate the package on your computer and then manually delete it. While this is a straightforward process, you can get a warning from R studio if the last script that you were running was using the package you just deleted. Furthermore, you have to know where to look for the package. Once you find the package you were looking for, removing it is a simple matter of deleting the folder. This makes a good approach if you are having trouble uninstalling a package any other way. You should probably only use this method if you have problems uninstalling a package from within the R environment.

The second alternative is part of the R studio environment. Simply click on the packages tab and find the package that you want to uninstall. Then go over to the right side of the window where you will find a grey circle with an X in it. It even says “remove package” if you move your cursor over that circle. When you click on it, a dialog box comes up asking if you want to uninstall the package. Click “no” and it just closes the dialog box. Click “yes” And it uninstalls the package by triggering the uninstall command. When it is done, you will have successfully uninstalled the package.

Sometimes it may be necessary to uninstall a package, or you just want to get rid of it because you are not using it anymore. In any case, there are several ways that you can uninstall a package in R. The programming language supplies functions for both installing and uninstalling packages. It is a handy way of preventing your package list from getting filled with packages that you no longer use.

Scroll to top
Privacy Policy