Press "Enter" to skip to content

How to install/load packages in R

Zigya Acadmey 0

R Packages /Libraries

Packages in R comprise of R functions, data and compiled code that are present in a well-defined format. The location where the packages are installed is known as a library. R comes with a standard set of packages. Other packages are available for download and installation. In packages, we use many R functions, free libraries of code written by R’s active user community.

Installing a single package in R :


Usage:

> install.packages("Package Name")

All the R packages will be downloaded from CRAN, “Comprehensive R Archive Network”.

The “Comprehensive R Archive Network” (CRAN) is a collection of sites which carry identical material, consisting of the R distribution(s), the contributed extensions, documentation for R, and binaries. CRAN also provides access to documentation on R, existing mailing lists and the R Bug Tracking system

There are thousands of useful R packages for us, but navigating them all can be a challenge. Below are some of the very basic packages that can be installed in R. Here are a few packages that you can install using above syntax: |install.packages(“package_name”)|

  1. Tidyverse: tidyverse is a collection of packages that are useful for many tasks like data management, data reshaping, data formatting, memory management, etc.
  2. Ggplot2: the ggplot2 package is the most popular graphics package in R. It extends R’s graphical capabilities by a large margin able to make static, dynamic as well as animated graphics with simple commands.
  3. Installr: the installr package is highly useful for installing, updating and managing versions of R, RStudio, and R packages.
  4. Stringr: Stringr is a package very popular for its string manipulation functions. Stringr functions are much easier to use and more powerful than the base R functions.

Installing packages without CRAN (Manual Installation):

There might be some packages that are yet not available in CRAN. We can install these packages remotely from Github.

There are various ways in which we can install packages present in Github.

The usage of the methods are given below:

Using “remotes”

Install the released version of remotes from CRAN:

install.packages("remotes")

To install the latest version of a package in the default branch from GitHub, you can use the user/repo form. Note that user can also be an organization:

remotes::install_github("r-lib/conflicted")

If the R package is inside a subdirectory of the root directory, then give this subdirectory as well:

# build = FALSE because of some specificities of XGBoost package
install_github("dmlc/xgboost/R-package", build = FALSE)

To install the latest release, append @*release to the repo name:

remotes::install_github("gaborcsardi/pkgconfig@*release")

To install a pull request, append # and the id (an integer number) of the pull request to the repo name:

remotes::install_github("r-lib/pkgconfig#7")

Using “devtools”:

The devtools package provides install_github() that enables installing packages from GitHub.

install_github("hadley/dplyr")

We can also install the packages by using :

devtools::install_github("package_name")

Using “githubinstall”

You can install the githubinstall package from CRAN.

install.packages("githubinstall")

The githubinstall package provides several useful functions.

  • gh_install_packages() or githubinstall()
  • gh_suggest()
  • gh_suggest_username()
  • gh_list_packages()
  • gh_search_packages()
  • gh_show_source()
  • gh_update_package_list()

The functions have common prefix gh. githubinstall() is an alias of gh_install_packages().

To use these functions, first, you should load the package as follows.

library(githubinstall)

There is a difference between install.packages() and install_github() in their required argument. install.packages() takes package names, while install_github() needs repository names in addition. It means that when you want to install a package on GitHub, you must remember its repository name correctly.

Loading packages in R

Now, to load any packages you just installed so far all you need to do is:

> library(dplyr)

Conclusion

Hence, We saw how to install packages using different options like by remote, devtool, and GitHub and also saw to find any packages and how to check the info about that package. Also saw how to load these packages in your R projects.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Leave a Reply

Your email address will not be published. Required fields are marked *