R Help

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 (version A)

# 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)
    }
  }
)

## Load multiple CRAN packages (version B)

#  An R function provided by Steven Worthington (https://gist.github.com/stevenworthington)
#  In a fist step, the fuction checks if the package is installed. Install the package if not installed, then load the package into the R session.

ipak <- function(pkg){
    new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
    if (length(new.pkg)) 
        install.packages(new.pkg, dependencies = TRUE)
    sapply(pkg, require, character.only = TRUE)
}

packages <- c("ape","vegan","ggplot","tidyverse","plotly","gridExtra","igraph","RColorBrewer")
ipak(packages)

R Code with Style

Style Guides

Style Packages

Code Folding

RStudio supports both automatic and user-defined folding for regions of code. Code folding allows you to easily show and hide blocks of code to make it easier to navigate your source file and focus on the coding task at hand.

To insert a new code section you can use the Code > Insert Section command. Alternatively, any comment line which includes at least four trailing dashes (-), equal signs (=), or pound signs (#) automatically creates a code section.

## Setup ----

## clean/reset environment
rm(list=ls())

## R and Bioconductor libraries
library(ggplot2)

## Data Import ----

otufile <- "ZOTU_c99_Count_Sintax.txt"
mapfile <- "MapFile.txt"

## Import into Phyloseq
d.ZOTU <- import_qiime(otufilename = otufile, mapfilename = mapfile)
d.ZOTU

Cheat-Sheets PDFs