Packages are the fundamental concept of code reusability in R programming. A package in R is a collection of functions, sample data, and the documentation that describes how to use them. There are multiple ways to install R Packages. In this article, we are going to focus on the most commonly used techniques to install the package in R.
Install R Packages
A quick and easy way to install a package in R is with the install.packages function. If you know the package name, then this approach is handy. 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 R programming. Here, we want to install the RODBC package, which is useful to load data from Database management systems like SQL Server
TIP: Type?Package Name, or help(Package Name) to get the information about the package.
Step 2: You might get a slightly different output but, don’t worry about the output unless you see an error message. From the screenshot below, you can observe that R Studio 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 () also help us to check whether we successfully installed the package or not.
Step 4: Below screenshot shows that we haven’t got any error messages. If there is an issue, then it throws 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 of installing a package is beneficial. 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, the following pop up opened.
Install from: Here we have two options:
- Repository (CRAN, CRANextra): This option help to install packages available in the 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 displays 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 starts installing the selected package, and here it is DBI. As you can see in the console, it is stating that out package installed successfully. You might get a slightly different output but, don’t get panic unless you see an error message.
From the screenshot below, 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) help finding the list of packages available in R programming language. This method (Step 5 to Step 8) helps to download and install R packages from third-party websites.
Step 1: To find the available packages, first Go to the official R Programming website by clicking this link Packages. The screenshot below shows 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, the following window or tab 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 opened. Please click on the Packages hyperlink under the Software.
Step 4: Once you click on the Packages hyperlink, the below-shown screenshot displayed. Here, it displays 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 opens the page with a list of available packages. Please select the package you want to download on install into your R work environment. From the screenshot below, 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 opened to save this file. Please select the Save File option.
Step 7: 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 opened. 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 into R, and here it is plot3D.
You might wonder why I have chosen the plot3D package. Because plot3D won’t install unless you installed a misc3d package, it means when you click on the install button; it throws an error stating that you are missing the dependency package misc3d. It is one of the common errors you 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 an installed package is very simple in R programming. To do so, we have to use the 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 screenshot below, you can observe that the library(“plot3D”) is throwing an error saying that there is no package called polt3D.