Packages are the fundamental concept of code reusability in R programming. A package is a collection of functions, sample data, and documentation that describes how to use them. There are multiple ways to install R Packages.
In this R article, we are going to focus on the most commonly used techniques to install the package.
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 this programming. Here, we want to install the R RODBC package, 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’ package.
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 helps us to check whether we have successfully done it or not.
Step 4: Below screenshot shows that we haven’t got any error messages while installing the R package. If there is an issue, it throws an error saying that there is no 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, and Viewer section.
Under the R packages tab, Please click on the Install tab for a new source package.
Once you click on the tab, the following pop-up opens.
Here we have two options:
- Repository (CRAN, CRANextra): This option helps to install packages available in the R 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 of the R package.
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 states that the process was successful. You might get a slightly different output but don’t get panic unless you see an error message.
The screenshot below shows 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 find the list of available ones in the 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 package download process.
Step 2: Once you click on the above-specified hyperlinks, the following window or tab opens. Please select your closest mirror for 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 opens. Please click on the Packages hyperlink under the Software.
Step 4: Once you click on the hyperlink, the below-shown screenshot is 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 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 opens 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 opens. 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 select the required one, click on the Install button to start the process for the selected module into R, and here it is plot3D.
You might wonder why I have chosen 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.