Packages are the fundamental concept of code reusability in R programming. A package is a collection of functions, sample data, and the documentation that describes how to use them. There are multiple ways to install R Packages, and in this article, we are going to focus on most commonly used techniques.
TIP: Type?Package Name, or help(Package Name) to get the information about the package.
Install R Packages
A quick and easy way to install the package is with the install.packages function. If you know the package name then this approach will be very useful. The Syntax behind this function is:
# Function to install package R install.packages("package Name")
Step 1: Please replace the package name with your desired package name. In this example, we want to install the RODBC package, which is useful to load data from Database management systems like SQL Server
Step 2: You might get slightly different output but, don’t worry about the output unless you see an error message. From the below screenshot you can observe that R Studio is installed the ‘RODBC’ package.
Step 3: Type library (RODBC) to load the package, and you can see that it is showing a list of available packages along with our RODBC package. This library () will also help us to check whether we successfully installed the package or not.
Step 4: Blow screenshot will show you that, we haven’t got any error messages. If there is an issue then it will throw an error saying that there is no package called RODBC
The second approach to Install R Packages
If you don’t know the package name, or you want to check all the names available in R then this approach is very useful. For this, we have to use the Files, Plots, Packages, Help, Viewer section.
Under the packages tab, Please click on the Install tab to install a new package.
Once you click on the Install tab, following pop up will be opened.
Install from: Here we have two options:
- Repository (CRAN, CRANextra): This option will help to install packages available in CRAN mirror. We are selecting this option for now.
- Package Archive File (.zip, .tar.gz): Please select this option, if you want to load the package from zip files, or third-party websites.
Packages: Once you type a character or letter then it will display the list of available packages in CRAN starting with that letter.
In this example, we want to load the DBI package that’s why we are selecting the DBI. Feel free to select your required package.
Click on the Install button to start the package installation process.
Once you click on the Install button, R studio will start installing the selected package and here it is DBI. As you can see in the console, it is stating that out package is installed successfully. You might get slightly different output but, don’t get panic unless you see an error message.
From the below screenshot you can observe that our ‘DBI’ package is under the User Library.
The traditional approach for Install R Packages
Although we don’t use this type of approach in real-time, most of these steps (Step 1 to Step 5) will be helpful to find the list of packages available in R programming language. This method (Step 5 to Step 8) will help you to download and install R packages from third-party websites.
Step 1: In order to find the available packages, first Go to the official R Programming website by clicking this link Packages. Below screenshot will show you the official website Homepage. Next, Click on the CRAN to start the R packages download process.
Step 2: Once you click on the above-specified hyperlinks, following window or tab will be opened. Please select your closest mirror for R Packages download. Since I reside in India, we are selecting the India Institute of Technology Madras mirror.
Step 3: Once the closest mirror is selected, the following window will be opened. Please click on the Packages hyperlink under the Software as we shown below
Step 4: Once you click on the Packages hyperlink, below screenshot will be displayed. Here, it will display two options:
- Table of available packages, Sorted by Date of Publication
- Table of available packages, Sorted by Name
Please select either of the options
Step 5: Once you click on the option, it will open the page with a list of available packages. Please select the package you want to download, and from the below screenshot you can observe that we are selecting the Plot3D package
Step 6: Here, Click on the Plot3D 1.1.tar.gz hyperlink to start downloading the R Package zip file. Once you Click on the link, a pop-up window will be opened to save this file. Please select the Save File option.
Step 7: In order to install the package that we downloaded, Open the R Studio and Under the packages tab, Please click on the Install tab to install a new package. Once you click on the Install tab, a pop up will be opened as we shown below. Please select the Package Archive File (.zip, .tar.gz) option under the Install from the option. Next, click on the Browse button to browse the zip file.
Step 8: Once you selected the required package, click on the Install button to start installing the selected package and here it is plot3D.
NOTE: You might wonder, why I have chosen plot3D package. Because plot3D will not install unless you installed misc3d package, it means when you click on the install button, it will throw an error stating that you are missing the dependency package misc3d. This is one of the common error you will face, while you install R packages. And to resolve this error, abort this installation and install the misc3d package using any of these three approaches and then install this plot3D
We have successfully installed the package.
Remove R Packages
Removing a package is very simple in R programming. In order to do so, we have to use remove.package function.
# Removing Plot3D Package from R Studio remove.packages("plot3D") # Removing misc3d package remove.packages("misc3d") # Checking whether we successfully removed Plot3D package or Not. library("plot3D")
From the below screenshot you can observe that library(“plot3D”) is throwing an error saying that, there is no package called polt3D
Thank You for Visiting Our Blog