R 语言是为数学研究工作者设计的一种数学编程语言,主要用于统计分析、绘图、数据挖掘。R 语言是解释运行的语言(与 C 语言的编译运行不同),它的执行速度比 C 语言慢得多,不利于优化。但它在语法层面提供了更加丰富的数据结构操作并且能够十分方便地输出文字和图形信息,所以它广泛应用于数学尤其是统计学领域。这也是大多数生信工作者选择R语言的原因。

1. 学习资源

  • 推荐书籍:R语言实战,R数据科学等等;
    遇见bug怎么办
  • R语言初学者必备-cheatsheet大全:https://www.rstudio.com/resources/cheatsheets/
  • R语言菜鸟教程:https://www.runoob.com/r/r-tutorial.html
  • 简书、知乎等各大平台都会提供很多生信分析常见的教程,一定要利用好这些资源;
  • 报错怎么办:度娘来帮忙;一定要学会百度!一定要学会百度!一定要学会百度!

2. 了解并安装R, Rstudio 及R包

  • 下载R语言的软件: https://cran.r-project.org/bin
  • 下载Rstudio这个R编辑器: https://www.rstudio.com/products/rstudio/download/ 

 


生信必备技巧之R语言基础教程—R包安装


 

  • 安装一些必要的包
  • 什么是R包:包是 R 函数、实例数据、预编译代码的集合,包括 R 程序,注释文档、实例、测试数据等;
  • 安装包 install.packages(" xxxxxx ")
    对于生物信息学常用的包,可以使用:BiocManager::install("xxxx")函数进行安装
  • 加载包 library( xxxxx )
  • 查看包的帮助文档help("xxxxx") 或?xxxxx
# 生物信息学分析必备的一些R包,复制以下代码,直接运行即可;
rm(list = ls())
# 设置镜像:
options()$repos
options()$BioC_mirror
#options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
options(BioC_mirror="http://mirrors.tuna.tsinghua.edu.cn/bioconductor/")
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
options()$repos
options()$BioC_mirror

# 方法一:
options()$repos
install.packages('WGCNA')
install.packages(c("FactoMineR", "factoextra"))
install.packages(c("ggplot2", "pheatmap","ggpubr"))
library("FactoMineR")
library("factoextra")

# 方法二:
if (!requireNamespace("BiocManager", quietly = TRUE))
 install.packages("BiocManager")
BiocManager::install("KEGG.db",ask = F,update = F)
BiocManager::install(c("GSEABase","GSVA","clusterProfiler" ),ask = F,update = F)
BiocManager::install(c("GEOquery","limma","impute" ),ask = F,update = F)
BiocManager::install(c("org.Hs.eg.db","hgu133plus2.db" ),ask = F,update = F)

# 方法三:从github中安装

# 所有的R包都提交上传到CRAN,如Github,需要通过一定的渠道进行安装
# R安装devtools包
install.packages("devtools")
library(devtools)
# 安装github上的R包(需翻墙或改hosts)
devtools::install_github('lchiffon/REmap')
# 前为github的用户名,后为包名

# 测试--加载R包;
library(REmap)
library(GSEABase)
library(GSVA)
library(clusterProfiler)
library(ggplot2)
library(ggpubr)
library(hgu133plus2.db)
library(limma)
library(org.Hs.eg.db)
library(pheatmap)
  • 获取当前工作区间getwd()
  • 更改工作区间setwd( "xxxxxx")
  • 清除当前对象rm(xx)

生信必备技巧之R语言基础教程—R包载入