Skip to content

R Packages

Installing and loading R packages

Repositories:

  • CRAN - official R repository
  • Bioconductor - topic specific repository
  • Github - most popular repository for open source projects but not R specific

Package Installation

The commands for the installation of R packages depends on the repository.

### CRAN repository :

install.packages("package")
install.packages(c("packageA", "packageB"))

### Bioconductor:

## R version 3.6+
if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("package")

## Older R versions
source("https://bioconductor.org/biocLite.R")
biocLite("package")

### GitHub
library(devtools)
devtools::install_github("link/to/package")

Package Info

It is always a good idea to look at the basic information about a package maybe before but certainly after you have installed it.

packageDescription("package")
help(package = "package")

Manage Packages

# List all installed packages
installed.packages()
# Get Package version
packageVersion("fun")
# Update a package
update.packages("fun")
# Load a package
library("fun")
# Un-load a package
detach("package:fun", unload=TRUE)
# Remove a package
remove.packages("fun")

Example(s)

## Alternative package version

# Install dplyr by installing tidyverse (collection of data science tools):
install.packages("tidyverse")
search()

# Alternatively, install just dplyr:
install.packages("dplyr")

# Or the development version from GitHub:
install.packages("devtools")
devtools::install_github("tidyverse/dplyr")

## Load multiple CRAN packages

# Package list
package.list = c("ggplot2","RColorBrewer","ggpubr")

package.manager <- lapply(
  package.list,
  FUN <- function(x) {
    # Load multiple packages and
    # install missing packages
    if (!require(x, character.only = TRUE)) {
      install.packages(x, dependencies = TRUE)
      library(x, character.only = TRUE)
    }
  }
)