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 following function. If you know the name, then this approach is handy. The Syntax behind this function is:
Step 1: Please replace the name with your desired package name in R programming. Here, we want to install the RODBC, which is useful to load data from Database management systems like SQL Server
TIP: Type?PackageName, or help(PackageName) to get the information about it.
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’.
Step 3: Type library (RODBC) to load the package, and you can see that it is showing a list of available ones along with our RODBC. This library () also help us to check whether we successfully done it 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 none called RODBC.
The second approach to Install R Packages
If you don’t know the name or you want to check all the names available in R Programming, 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 for a new source package.
Once you click on the tab, the following pop up opened.
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 downloaded zip files or third-party websites.
Once you type a character or letter, then it displays the list of available ones in CRAN, starting with that letter.
In this example, we want to load the DBI that’s why we are selecting the DBI. Feel free to select your required one.
Click on the Install button to start the process.
Once you click on the button, R studio starts installing the selected library package, and here it is DBI. As you can see in the console, it is stating that the process was 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’ pkg is under the User library path.
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 ones, first Go to the official R Programming website. 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 hyperlink, the below-shown screenshot displayed. Here, it displays two options:
- Table of available packages, Sorted by Date of Publication
- Table of available ones, 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 ones. 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.
Step 6: Here, Click on the Plot3D 1.1.tar.gz hyperlink to start downloading the 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 one that we downloaded, Open the R Studio and Under the packages tab, Please click on the Install tab. Once you click on the tab, a pop up opened. Please select the Archive File (.zip, .tar.gz) option from it. Next, click on the Browse button to browse the zip file.
Step 8: Once you selected the required one, click on the Install button to start the process for selected module into R, and here it is plot3D.
You might wonder why I have chosen the plot3D. Because plot3D won’t work unless you have a misc3d, it means when you click on the install button; it throws an error stating that you are missing the dependency pack misc3d. It is one of the common errors you face while you R install packages. And to resolve this error, abort the process and install the misc3d using any of these three approaches and then load this plot3D.