常用代码集合

常规设置

rm(list = ls())#清空环境变量
options(stringsAsFactors = F)##字符不作为因子读入

加载包时不要出现冗余信息

suppressPackageStartupMessages({
  library(XXXX)
})  

设置安装包的镜像网站

options("repos"="https://mirrors.ustc.edu.cn/CRAN/")
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")

保存R数据

save(var1,var2,var3,file="filename.Rdata")
load("filename.Rdata")

安装R包

if(!require("XXXX")) install.packages('XXXX',update = F,ask = F)#如果是CRAN的包
if(!require("XXXX")) BiocManager::install('XXXX',update = F,ask = F)#如果是Bioconductor的包

批量安装R包

cran_packages <- c('','','','') 
Biocductor_packages <- c('','','','','')

for (pkg in cran_packages){
  if (! require(pkg,character.only=T) ) {
    install.packages(pkg,ask = F,update = F)
    require(pkg,character.only=T) 
  }
}

for (pkg in Biocductor_packages){
  if (! require(pkg,character.only=T) ) {
    BiocManager::install(pkg,ask = F,update = F)
    require(pkg,character.only=T) 
  }
}

#前面的所有提示和报错都先不要管。主要看这里
for (pkg in c(Biocductor_packages,cran_packages)){
  require(pkg,character.only=T) 
}

PCA作图

library("FactoMineR")
library("factoextra") 

df.pca <- PCA(t(ex), graph = FALSE)

fviz_pca_ind(df.pca,
             geom.ind = "point",
             col.ind = group_list, #group_list自定义
             addEllipses = TRUE, 
             legend.title = "Groups"

你可能感兴趣的:(常用代码集合)