In this tutorial, we are beginning to explore the R programming language to an extended level. We will discuss how to install R packages into our operating systems. You will also learn different packages in R, their purpose, and how to load these packages in R.
In this tutorial, you will learn
And so on...
In the R programming language, the R packages form a fundamental unit of shareable code. An R package is a bundle with a collection of codes, data, documentation, and tests. The best part of the R package is it can be shared easily with others. The users can install these R packages from a centralized repository known as CRAN. The CRAN is the acronym for the Comprehensive R Archive Network. One of the main reasons for the successful use of R in different emerging fields like data science, Machine learning is the variety of available packages in CRAN.
The R packages are stored under a directory called “library” within the R environment. During R installation a set of packages gets installed by default. When the user starts with RStudio these default packages are only available. Some packages other than default packages need to be loaded by the user to the R program for their efficient utilization.
The R packages reside in software repositories. A software repository is the storage location where a collection of packages are stored, managed, and maintained. These are accessible online and the public or users or anybody can install R packages from these repositories depending upon their requirements.
Some of the software repositories that provide R packages are
Let us consider each repository and its performances. What makes one different from other repositories?
|MRAN and RStudio Package Manager
In R, the collection of functions, data, and compiled code form a package. The library is the storage location of these R packages. The R packages are installed in your system by downloading from some software repositories (CRAN, GitHub, etc) and are stored in the library. Once after installation of packages to make available in your code, the package needs to be loaded using the command or function library(package name). (Will discuss this in detail in this tutorial)
In RStudio, R packages are the bundles of commands or libraries to support certain analyses. Installing packages in RStudio is easy and can be done in multiple ways by
R packages can be installed manually using the simple function
Where the parentheses enclose the package name that needs to be installed in the double quotation. Suppose need to install the calendar package, the following command is used.
The screenshot shows the RStudio console with
install.packages() command to install package calendR. You can install any package of your interest.
To get rid of repeated writing of the same function again and again in the RStudio console to install different packages at the same time the R supports the below syntax
The function to install a package
install.packages() with another
c() function which contains the name of multiple packages to be installed at a time. The single-line function allows installing multiple packages at a times
Follow the steps to install an R package in RStudio
The command that comes with modeest package is known as mfv. The mfv() takes x as an argument inside the braces and finds the mode of the list of numbers 1,2,3,4,5. And returns output value 4.
After downloading the packages using either of the methods may be by function in the console or with the help of GUI menu options, the desired R package gets installed. The next step is to use these R packages in the R program. In order to use the R package installed, you need to load the package.
You can load the package by using the library() function. The function encloses the name of the package to be loaded in either quotes or without quotes.
After loading you can check the loaded package using help or ? in the R console. This further helps in all documentation related to any particular package name including its description, syntax the package follows, what the parameter or arguments inside each function define, and so on.
?(<name of package>)
help(<name of package>)
help(calendR) or help(“calendR”)
To remove a package from the library click on the × (cross) mark on the right side.
It displays the remove package message once placing the mouse on the × (cross). The removed packages will be no longer in the R library.
To modify operations and to improve functionalities sometimes R packages are updated.
It is advisable to update R packages occasionally. You can update R packages from RStudio using the update button in the toolbar of the Packages panel.
The RStudio verifies the R packages need to be updated and displays them in a dialog box as shown below
Before installing R packages from GitHub you need to check for devtools packages are available or not. You can check that by using the function
library(devtools) in the R console. If the devtools are already installed no messages will pop up, if no such package exit displays a message showing
Error in library(devtools) : there is no package called ‘devtools’.
If no devtools package exists use the function
install.packages(“devtools”) or tools -> install packages->packages using the interface of RStudio.
After installing devtools using either of the two ways type the install_github function with "account_name/repository_name" as an argument to install the R package from GitHub.
Consider the example
# Installing ggplot2 from GitHub