How to INSTALL R PACKAGES? πŸ“¦ [CRAN, GitHub, source, R-forge, …]

An R package is a library of functions that have been developed to cover some needs or specific scientific methods that are not implemented in base R. The functions that R provides by default are limited, so you might be wondering how to install new packages in R. In this tutorial we will review all the sources available to install R packages .

Installing R packages from CRAN

The Comprehensive R Archive Network ( CRAN ) is the official R packages repository, with thousands of free R packages available. Most of them have been developed by Data Scientists, Statisticians, Professors and researchers. There are all type of packages, from graphics packages as the well-known ggplot2 to very specific topics like the DTDA.cif package, that implements estimators for accumulative incidences of competing risks under double-truncation .
CRAN is the official R depository. All packages have been tested automatically and meet the CRAN policy .
first, you need to look for the name of the package you want to install. You may want to research for your topic googling something like : β€˜ graphics software R ’ or β€˜ R box for clock time series ’. You can besides use the CRAN Task Views, where you can find the most relevant R packages by topic.

Take care! Just because a package is in CRAN does not imply that the methods are well implemented, since there are no third-party checks on this. besides, there may be errors that have not been checked. If you find any type of error or trouble, contact the upholder of the package .

function to install R packages

once you decided what software to install, fair call the install.packages function with the appoint of the box inside the digression with quotation marks. As an example, we are going to install the calendR software, that allows creating monthly and annual calendars, but you can install the software you prefer .


After facility, you need to load the package if you want to entree its functions. For that purpose, you can load it with the library function, specifying the software appoint with or without quotation marks .

library("calendR") # Equivalent

once loaded, you can use ? or the help affair with the package name or the name of any function to see the documentation. You will besides find useful examples to understand how the package works .

help(calendR) # Equivalent
?calendR      # Equivalent

# Help for the main function

In summation, you can find out where the packages are going to be installed calling the .libPaths() function .

 .libPaths() # Returns the installation path of the libraries

Installing the CRAN packages with the menu

alternatively, you can install R packages from the menu .

  • In RStudio go to Tools β†’ Install Packages and in the Install from option select Repository (CRAN) and then specify the packages you want.
  • In classic R IDE go to Packages β†’ Install package(s), select a mirror and install the package.

Installing packages in R from zip code source

You may have downloaded a box in zip or tar.gz format. In order to install the software from a local slide fastener file you just need to call the install.packages function with arguments repos = NULL and type = "source". eminence that the file path musn’t contain spaces .

                 repos = NULL, type = "source")

You can besides set your working environment first with the setwd routine to the booklet where you have downloaded the box charge and then install the software specifying the name of the zip up or tar.gz file.

                 repos = NULL, type = "source")

The final choice is to use the menu. Go to Tools β†’ Install Packages and in the install from option choose Package Archive File ( .zip ; .tar.gz ) and select your file .
In shell you have the slide fastener hosted in some URL you can use the serve from the installr box. note you can besides install packages from CRAN ( even older versions ) this way .

# install.packages("installr")


Install multiple packages at once

If you need to install several packages at once without writing the like function over and over again, you can make use of the c serve within the install.packages officiate. Note that nowadays the citation marks are needed to specify the packages names .

install.packages(c("ggplot2", "dplyr"))

now you know how to install R CRAN packages, but sometimes there are not all in CRAN for many reasons : CRAN has a code policy and some developers don ’ metric ton want to spend time fixing minor issues to meet those requirements. early times there exists a development version in GitHub of a CRAN package with extra features you may want. In the following sections you will learn how to install packages from other available sources .
In the pursuit sections we will show you how to install unofficial source packages, but keep in mind that certain packages may be in a development process and could lead to unexpected errors sometimes .

Install R packages from GitHub or GitLab

GitHub is a long-familiar code sharing platform. If you go to the page, you can search for R packages using the search barroom and writing something like : plot package language:R in case you want to look for graphics packages. note that β€œ lyric : R ” is a search command of the foliate to restrict the results to only R code repositories. Suppose, for exemplify, that you want to download the development version of the ggplot2 package from GitHub. The URL would look like :

The first footprint is to install and load the devtools software, available in CRAN. In case you encounter some error means you besides need to install the RTools .


then you can call the install_github affair with "account_name/repository_name" as argument to install the R box from GitHub .

 # Installing ggplot2 from GitHub

It is deserving to mention that if you don’t want to load the devtools every time you want to install a GitHub box you can use devtools::install_github(account_name /repository_name). The :: hustler allows you to call functions from a package without the motivation of loading it .

Install R packages from R-Forge

R Forge visualize is a web with software development tools and repositories. As an exemplar, if you would like to install the MPAgenomics box, you have to specify in the repos argument of the install.packages function the URL of the R Forge project. The dependencies argument is used when repos is not NULL, to specify whether the dependencies of the software that are not installed must be installed or not .

install.packages("MPAgenomics", repos = "",
                 dependencies = TRUE)

Install bioconductor packages in roentgen

Bioconductor is another visualize that hosts tools and R packages for analyzing biological data. First, you need to install the BiocManager software .


second, you can make use of the install serve of the package .

# Installing the nanotatoR package

bill you can besides install more than one package at the like time ,

# Installing NBSplice and ncdfFlow packages at once
BiocManager::install("NBSplice", "ncdfFlow") 

It is worth to mention that you can see the full list of Bioconductor packages in R writing BiocManager::available(). For more information about the Bioconductor installation process refer to the official Bioconductor R packages page .

Install R software in Jupyter Notebook

If you are using R under the conda environment with Jupyter Notebook and you need more packages that the included like β€˜ Essentials ’, you need to specify the repos controversy as follows :

install.packages("ggplot2", repos = "")

Updating R packages can be boring if you have to reinstall the packages over and over again when some has a newer interpretation. You can see the full moon number of your R packages that are not up-to-date with the old.packages function .


You can update some of them with the install.packages function or calling the update.packages function. If you set the controversy ask to FALSE, you will avoid R displaying prompting messages .

update.packages(ask = FALSE)

You will merely be able to update a software if it is not loaded. If you have already loaded a box and you want to update it, use the detach function as follows : detach(package:some_package, unload = TRUE)

tilt functions in R package

once installed, you can get a list of all the functions in the package. If the package is on CRAN, you will find documentation in PDF format of all functions inside a foliate like Recall you can access this documentation in HTML format with the help officiate .

help(package = ggplot2)

You can besides use the lsf.str or ls commands to list all the functions inside an attach ( loaded ) package .


Another choice is to write : package_name:: and a list will show up in RStudio as a dropdown. In authoritative R you will have to press the pill push button to show the functions on the screen, although it should be noted that if the package contains many functions not all will be shown, as is the case with the ggplot2 package :

> ggplot2::

ggplot2::scale_fill_brewer         ggplot2::scale_size
ggplot2::GeomLine                  ggplot2::ScaleContinuousIdentity
ggplot2::geom_boxplot              ggplot2::guide_legend
ggplot2::ScaleDiscreteIdentity     ggplot2::Layout
ggplot2::scale_colour_ordinal      ggplot2::geom_hex
ggplot2::GeomRasterAnn             ggplot2::panel_cols
ggplot2::scale_colour_date         ggplot2::StatYdensity
ggplot2::stat_bin_2d               ggplot2::scale_y_sqrt
ggplot2::aes_all                   ggplot2::alpha
ggplot2::scale_shape               ggplot2::position_dodge2

View the source code of R package functions

sometimes it can be interesting to inspect the code of any function. For that aim, you have several options :

  • Call the name of the function in console.
  • Press Ctrl + Left Click or Cmd + Left Click in the function name (written on the script), when using RStudio.
  • Go to the CRAN (or GitHub, R-forge, …) page of the package and download the package file to inspect the source code manually.

Note that, if the serve is written in Fortan, C or any different language than R, you won ’ thyroxine be able to see the code with the first and the second method acting .

Check for installed packages

sometimes you don ’ t remember if you have a package installed and you don ’ t want to waste your time reinstalling it. In order to avoid this, you can use the require affair. note that the main difference between require and library is that the foremost one returns a boolean and the moment one returns an error if the box is not installed. The require function is designed to be used inside other functions .

# If FALSE, install the package
if (require("ggplot2")) install.packages("ggplot2")

The following line of code will also return TRUE if the box is installed, or FALSE if not .

"ggplot2" %in% rownames(installed.packages())

error : Can not remove prior installation of box

If you encountered this error, you might be using different versions of R in the same computer. The solutions are :

  • Close all open R sessions, open R again and install the package.
  • If it didn’t work, look at the error and go to the path where the 00LOCK file is and delete it.
  • Other option is to use the .libPaths() function, that returns the path where the libraries are, and delete the problematic package.

Are you unable to install packages in R ?

If you can ’ t install any package, there are many possible reasons :

  • You are disconnected from the internet.
  • The package is no longer available. Look for old versions with
  • The name of the package is misspelled. Package names are case-sensitive.
  • Rtools is required to build the package, so you will need to have it installed.

If nothing works, try to close and open R again or try in another calculator to verify if the problem persists .

reference :
Category : How To

Related Posts

Leave a Reply

Your email address will not be published.