Open In App

Removing Package In R

Last Updated : 17 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

R is s statistical Programming language that data scientists and analysts widely use because it provides a wide range of packages and libraries for analysis and estimation. We install these packages in R Programming Language to make our task easier but uninstalling them is equally important. We can remove a package from R because of multiple reasons such as freeing up space, resolving conflicts, or simply cleaning up our environment. In this article, we will learn multiple ways to remove packages in R.

Need to Remove Packages in R?

  1. Freeing Up Space: When we install so many packages for our use, it can use a lot of disk space. To get space we remove packages that we are not using. This helps in getting more storage space for other works.
  2. Resolving Conflicts: Sometimes we use multiple packages and these packages may share the same function names which can cause issues in operating that function, therefore removing packages that cause conflict can make our coding easier.
  3. Cleaning Up the Environment: Regularly reviewing and removing unnecessary packages helps maintain a clean and organized R environment.
  4. Specific Dependency: Removing packages that we do not use and making our R environment more work-oriented makes our work more focused.
  5. Changing Analysis Requirements: For certain projects, we may find that certain packages are not required, to make our work more focused we can remove unnecessary packages from R.

Removing Packages in R

Package Detach

Before removing a package from R permanently it is good to detach that package from the session. We use the detach() function for this purpose. This detaches the package from the current session without actually removing it.

R




# Detach a package
detach("packageName", unload = TRUE)


Using “remove.packages()” function

We can uninstall packages by the “remove.packages()” function but it does not delete the package files, instead, it removes the package from the library. After using this function for a package we cannot use it in future code unless we install it again.

R




# Syntax
remove.packages(pkgs, lib)
 
# Example
remove.packages("packageName")
 
#specific example
remove.packages("ggplot2")


pkgs: It is the package name that we want to uninstall
lib: It is the library name showing the location of the package.

Using unlink() Function

The above-mentioned syntax does not delete the package files as well while removing them. If we wish the delete the package files as well we use the “unlink()” function. This function removes files or directories associated with the specified package.

R




# Syntax
unlink(system.file("libs", "packageName", package = "packageName"), recursive = TRUE)
 
# Example
unlink(system.file("libs", "dplyr", package = "dplyr"), recursive = TRUE)


system.file(): Constructs the path to the installed package directory.
recursive: If set to TRUE, removes directories and their contents.

Using R studio

If you are using R Studio, you can uninstall the packages directly from the graphic user interface. To uninstall the packages from R studio we can follow certain steps:

Step 1: Go to the bottom right tab below the environment section and select “Packages”

This will give you the list of all the packages that you have in-built or installed in R studio

Step 2: Go to the package you want to uninstall and select the cross button mentioned there.

For example, if you wish to uninstall the “DALEXtra” package from R, Go to the package and click on the cross.

Screenshot-(101)

Removing Package In R

Step 3: After selecting the cross we will have a pop-up bar on the screen asking for confirmation to remove the package permanently. Here, we are taking the example of the DALEXtra package. We need to select “yes” to uninstall the package permanently.

POP-UP-REMOVE-PACKAGE-GFG

Uninstall Package

Checking Installed Packages

After following these steps we can also check if we still have the package installed or not. To get a list of all installed packages, you can use the installed ones.packages() function.

R




# Get a list of installed packages
installed.packages()


Output:

                      Package                
abind "abind"
ade4 "ade4"
askpass "askpass"
assertthat "assertthat"
backports "backports"
base64enc "base64enc"
BH "BH"
bio3d "bio3d"
bit "bit"
bit64 "bit64" ..........

This function can also help us in assessing the list of all the packages we have in R.

To reinstall these packages we can use the install.packages() function in R

R




#syntax to reinstall packages
install.packages("packageName")
#example
install.packages("ggplot2")


Checking and removing the package

We can also ask the R if it has that certain package or not with the help of the below-given syntax

R




removeIfInstalled <- function(packageName) {
  # Check if the package is installed
  if (packageName %in% rownames(installed.packages())) {
    cat(paste("Package '", packageName, "' is installed.\n", sep = ""))
     
    # Unload the package if it is currently loaded
    if (packageName %in% search()) {
      cat("Detaching the package from the current session.\n")
      detach("package", packageName, unload = TRUE)
    }
     
    # Remove the package
    cat("Removing the package from the library.\n")
    remove.packages(packageName)
     
    cat(paste("Package '", packageName, "' has been successfully removed.\n", sep = ""))
  } else {
    cat(paste("Package '", packageName, "' is not installed.\n", sep = ""))
  }
}
 
# Example usage for DALEXtra (replace with the actual package name if needed):
removeIfInstalled("DALEXtra")


Output:

Package 'DALEXtra' is installed.
Removing the package from the library.
Package 'DALEXtra' has been successfully removed.

Conclusion

In this article, we learned multiple ways of removing a package in R. We can follow these steps to remove the packages for multiple reasons with the help of multiple examples.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads