- 1 Installing R packages from CRAN
- 2 Installing packages in R from zip source
- 3 Install multiple packages at once
- 4 Install R packages from GitHub or GitLab
- 5 Install R packages from R-Forge
- 6 Install bioconductor packages in R
- 7 Install R package in Jupyter Notebook
- 8 Update R packages
- 9 List functions in R package
- 10 View the source code of R package functions
- 11 Check for installed packages
- 12 Error: Cannot remove prior installation of package
- 13 Are you unable to install packages in R?
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 cumulative incidences of competing risks under double-truncation.
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 package R’ or ‘R package for time series’. You can also use the CRAN Task Views, where you can find the most relevant R packages by topic.
Function to install R packages
Once you decided what package to install, just call the
install.packages function with the name of the package inside the parenthesis with quotation marks. As an example, we are going to install the
calendR package, that allows creating monthly and yearly calendars, but you can install the package you prefer.
After installation, you need to load the package if you want to access its functions. For that purpose, you can load it with the
library function, specifying the package name with or without quotation marks .
library(calendR) library("calendR") # Equivalent
Once loaded, you can use
? or the
help function with the package name or the name of any function to see the documentation. You will also find useful examples to understand how the package works.
help("calendR") help(calendR) # Equivalent ?calendR # Equivalent # Help for the main function help(calendR)
In addition, you can find out where the packages are going to be installed calling the
.libPaths() # Returns the installation path of the libraries
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 source
You may have downloaded a package in zip or tar.gz format. In order to install the package from a local zip file you just need to call the
install.packages function with arguments
repos = NULL and
type = "source". Note that the file path musn’t contain spaces.
install.packages("file_path\\package_file_name.extension", repos = NULL, type = "source")
You can also set your working environment first with the
setwd function to the folder where you have downloaded the package file and then install the package specifying the name of the zip or tar.gz file.
setwd("file_path") install.packages("package_file_name.extension", repos = NULL, type = "source")
The last option 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 case you have the zip hosted in some URL you can use the
install.packages.zip function from the
installr package. Note you can also install packages from CRAN (even older versions) this way.
# install.packages("installr") library(installr) install.packages.zip("https://cran.r-project.org/bin/windows/contrib/r-release/calendR_1.0.zip")
Install multiple packages at once
If you need to install several packages at once without writing the same function over and over again, you can make use of the
c function within the
install.packages function. Note that now the quotation marks are needed to specify the packages names.
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’t want to spend time fixing minor issues to meet those requirements. Other times there exists a development version in GitHub of a CRAN package with additional features you may want. In the following sections you will learn how to install packages from other available sources.
Install R packages from GitHub or GitLab
GitHub is a well-known code sharing platform. If you go to the page, you can search for R packages using the search bar and writing something like:
plot package language:R in case you want to look for graphics packages. Note that “language: R” is a search command of the page to restrict the results to only R code repositories. Suppose, for instance, that you want to download the development version of the
ggplot2 package from GitHub. The URL would look like:
The first step is to install and load the
devtools package, available in CRAN. In case you encounter some error means you also need to install the RTools.
Then you can call the
install_github function with
"account_name/repository_name" as argument to install the R package from GitHub.
# Installing ggplot2 from GitHub install_github("tidyverse/ggplot2")
It is worth to mention that if you don’t want to load the
devtools every time you want to install a GitHub package you can use
:: operator allows you to call functions from a package without the need of loading it.
Install R packages from R-Forge
R Forge project is a web with package development tools and repositories. As an example, if you would like to install the
MPAgenomics package, 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 package that are not installed must be installed or not.
install.packages("MPAgenomics", repos = "http://R-Forge.R-project.org", dependencies = TRUE)
Install bioconductor packages in R
Bioconductor is another project that hosts tools and R packages for analyzing biological data. First, you need to install the
Second, you can make use of the
install function of the package.
# Installing the nanotatoR package BiocManager::install("nanotatoR")
Note you can also install more than one package at the same 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 package 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 argument as follows:
install.packages("ggplot2", repos = "http://cran.us.r-project.org")
Update R packages
Updating R packages can be tedious if you have to reinstall the packages over and over again when some has a newer version. You can see the full list of your R packages that are not up-to-date with the
You can update some of them with the
install.packages function or calling the
update.packages function. If you set the argument
FALSE, you will avoid R displaying prompting messages.
update.packages() update.packages(ask = FALSE)
detachfunction as follows:
detach(package:some_package, unload = TRUE)
List 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 page like
https://cran.r-project.org/web/packages/package_name. Recall you can access this documentation in HTML format with the
help(package = ggplot2)
You can also use the
ls commands to list all the functions inside an attached (loaded) package.
Another option is to write:
package_name:: and a list will show up in RStudio as a dropdown. In classic R you will have to press the tab 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:: 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 [...truncated]
View the source code of R package functions
Sometimes it can be interesting to inspect the code of any function. For that purpose, 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.
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 function. Note that the main difference between
library is that the first one returns a boolean and the second one returns an error if the package 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 package is installed, or FALSE if not.
"ggplot2" %in% rownames(installed.packages())
Error: Cannot remove prior installation of package
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
00LOCKfile 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 computer to verify if the problem persists.