Rhelp
R Basics¶
- R Step-By-Step (for beginners)
- [R Introduction - Handout] -coming soon-
Tip
Use right mouse-click or [ctrl]-click to download files.
R Packages¶
Repositories¶
Packages are the fundamental units of R code. They include R functions, documentation, and sample data. R packages can be found in repositories and these are the most popular ones:
- CRAN - official R repository
- Bioconductor - topic specific R repository
- Github - most popular repository for open source projects, not R specific
Installing Packages via devtools¶
The different repositories have unique ways to install a package. This might be a bit confusing if you are using packages from different repositories. You could use devtools package instead to unify this process. For more information, visit the devtools readme page.
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 Repository ## 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")
Find Packages¶
# Searching for packages on CRAN install.packages("packagefinder"); library(packagefinder) findPackage("tea") packageDetails("tea")
Package Info/Help¶
It is a superb idea to look at the package information.
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) } } )
R Code with Style¶
Style Guides¶
Picture Source: https://xkcd.com
Style Packages¶
-
styler -
install.packages("styler")
-
lintr -
devtools::install_github("jimhester/lintr")
-
formatR / Help with formatR -
install.packages("formatR")
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