In this tutorial, you will install R packages to your RStudio environment. R packages increase the power of R by improving existing base R functionalities, or by adding new ones. For example, if you are usually working with data frames, probably you will have heard about dplyr or data.table, two of the most popular R packages.
Repositories
Repositories are where packages are located so you can install them from it. Three popular repositories are:
- CRAN: The official repository maintained by R community.
- Bioconductor: Topic-specific repository, intended for open-source software for bioinformatics
- Github: Popular repository site for open-source projects
Installing packages
Make sure you are in an interactive node, and have R loaded and RStudio opened. For specific instructions, see Getting Started with Slurm Interactive Jobs.
Make sure to have the Console open in RStudio. We will be typing in commands here.
Installing from CRAN
CRAN packages can be installed directly using the following command:
install.packages("package")
For example, to install vioplot package, we will use:
install.packages("vioplot")
To install more than one package at a time, use a character vector as follows:
install.packages(c("vioplot", "MASS"))
Often, packages have different mirrors. Here is a list of available mirrors. To use a specific mirror, use the repo parameter:
install.packages("vioplot", repo = "https://lib.ugent.be/CRAN/")
Installing Bioconductor packages
Detailed instructions can be found at the official site here. First, we install basic functions needed to install Bioconductor packages. Execute the following script:
install.packages("BiocManager")
Get a list of available software packages here. To check available packages programmatically, use the following command:
BiocManager::available()
Install specific packages, e.g., “GenomicFeatures” and “AnnotationDbi”, with:
BiocManager::install(c("GenomicFeatures", "AnnotationDbi"))
Installing GitHub (and other) packages via devtools
You can install devtools, an R package that makes installation easier using the following command:
install.packages("devtools")
After devtools is installed, you will be able to use the utility functions to install another packages. Here is more documentation on devtools. The basic options are:
- `install_bioc()` from Bioconductor,
- `install_bitbucket()` from Bitbucket,
- `install_cran()` from CRAN,
- `install_git()` from a git repository,
- `install_github()` from GitHub,
- `install_local()` from a local file,
- `install_svn()` from a SVN repository,
- `install_url()` from a URL, and
- `install_version()` from a specific version of a CRAN package.
For example, to install babynames package from GitHub, run the following command:
devtools::install_github("hadley/babynames")
Removing packages
You can remove packages using the following command:
remove.packages("vioplot")
Updating Packages
To check what packages need update, type:
old.packages()
To update a specific package, run the install command again:
install.packages("vioplot")
To update all packages, use:
old.packages()
Using GUI to install packages
While we recommend using Console since it’s faster, RStudio has a GUI option for installing packages. You can find it under Tools –> Install Package, and there you have a pop-up window to type the package you want to install.