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.
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.
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.
Removing Package In R
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.