jimmy <- function(a,b,m = 2){
(a b)^m
}
jimmy(5,6,2)
代码语言:r复制## [1] 121
代码语言:r复制if(!require(data.table))install.packages(data.table)
library(data.table)
if(!require(rio))install.packages("rio")
library("rio")
if(!require("tibble"))install.packages("tibble")
library("tibble")
if(!require("ggplot2"))install.packages("ggplot2");library("ggplot2")
if(!require("ggpubr"))install.packages("ggpubr");library("ggpubr")
ins_p <- function(a){
if(!require(a))install.packages(a);library(a,character.only = T)
}
ins_p("eoffice")
代码语言:r复制## Error in install.packages : Updating loaded packages
代码语言:r复制library("eoffice")
ins_p("patchwork")
代码语言:r复制## Error in install.packages : Updating loaded packages
代码语言:r复制library("dplyr")
library("tidyr")
library("stringr")
?sd
install.packages("limma")
代码语言:r复制## Error in install.packages : Updating loaded packages
代码语言:r复制library("limma");ls("package:limma")
代码语言:r复制## [1] "[.EList" "[.EListRaw" "[.MAList" "[.MArrayLM"
## [5] "[.RGList" "[.TestResults" "alias2Symbol" "alias2SymbolTable"
## [9] "alias2SymbolUsingNCBI" "anova.MAList" "arrayWeights" "arrayWeightsQuick"
## [13] "as.data.frame.EList" "as.data.frame.EListRaw" "as.data.frame.MAList" "as.data.frame.MArrayLM"
## [17] "as.MAList" "as.matrix.EList" "as.matrix.EListRaw" "as.matrix.ExpressionSet"
## [21] "as.matrix.LumiBatch" "as.matrix.MAList" "as.matrix.MArrayLM" "as.matrix.marrayNorm"
## [25] "as.matrix.PLMset" "as.matrix.RGList" "as.matrix.vsn" "asMatrixWeights"
## [29] "auROC" "avearrays" "avearrays.default" "avearrays.EList"
## [33] "avearrays.MAList" "avedups" "avedups.default" "avedups.EList"
## [37] "avedups.MAList" "avereps" "avereps.default" "avereps.EList"
## [41] "avereps.EListRaw" "avereps.MAList" "avereps.RGList" "backgroundCorrect"
## [45] "backgroundCorrect.matrix" "barcodeplot" "beadCountWeights" "blockDiag"
## [49] "bwss" "bwss.matrix" "camera" "camera.default"
## [53] "cameraPR" "cameraPR.default" "cbind.EList" "cbind.EListRaw"
## [57] "cbind.MAList" "cbind.RGList" "changeLog" "chooseLowessSpan"
## [61] "classifyTestsF" "contrastAsCoef" "contrasts.fit" "controlStatus"
## [65] "convest" "coolmap" "cumOverlap" "decideTests"
## [69] "decideTests.default" "decideTests.MArrayLM" "designI2A" "designI2M"
## [73] "detectionPValues" "detectionPValues.default" "detectionPValues.EListRaw" "diffSplice"
## [77] "dim.EList" "dim.EListRaw" "dim.MAList" "dim.MArrayLM"
## [81] "dim.RGList" "dimnames.EList" "dimnames.EListRaw" "dimnames.MAList"
## [85] "dimnames.MArrayLM" "dimnames.RGList" "dimnames<-.EList" "dimnames<-.EListRaw"
## [89] "dimnames<-.MAList" "dimnames<-.RGList" "duplicateCorrelation" "eBayes"
## [93] "exprs.MA" "fitFDist" "fitFDistRobustly" "fitGammaIntercept"
## [97] "fitmixture" "fitted.MArrayLM" "fry" "fry.default"
## [101] "genas" "geneSetTest" "getDupSpacing" "getEAWP"
## [105] "getGeneKEGGLinks" "getKEGGPathwayNames" "getLayout" "getLayout2"
## [109] "getSpacing" "gls.series" "goana" "goana.default"
## [113] "goana.MArrayLM" "goanaTrend" "gridc" "gridr"
## [117] "head.EList" "head.EListRaw" "head.MAList" "head.MArrayLM"
## [121] "head.RGList" "head.TestResults" "heatdiagram" "heatDiagram"
## [125] "helpMethods" "ids2indices" "imageplot" "imageplot3by2"
## [129] "interGeneCorrelation" "intraspotCorrelation" "is.fullrank" "isNumeric"
## [133] "kegga" "kegga.default" "kegga.MArrayLM" "kooperberg"
## [137] "labels.TestResults" "levels.TestResults" "limmaUsersGuide" "lm.series"
## [141] "lmFit" "lmscFit" "loessFit" "logcosh"
## [145] "logsumexp" "MA.RG" "ma3x3.matrix" "ma3x3.spottedarray"
## [149] "makeContrasts" "makeUnique" "mdplot" "merge.EList"
## [153] "merge.EListRaw" "merge.MAList" "merge.RGList" "mergeScansRG"
## [157] "modelMatrix" "modifyWeights" "mrlm" "mroast"
## [161] "mroast.default" "nec" "neqc" "nonEstimable"
## [165] "normalizeBetweenArrays" "normalizeCyclicLoess" "normalizeForPrintorder" "normalizeForPrintorder.rg"
## [169] "normalizeMedianAbsValues" "normalizeMedianValues" "normalizeQuantiles" "normalizeRobustSpline"
## [173] "normalizeVSN" "normalizeVSN.default" "normalizeVSN.EListRaw" "normalizeVSN.RGList"
## [177] "normalizeWithinArrays" "normexp.fit" "normexp.fit.control" "normexp.fit.detection.p"
## [181] "normexp.signal" "plotDensities" "plotDensities.default" "plotDensities.EList"
## [185] "plotDensities.EListRaw" "plotDensities.MAList" "plotDensities.RGList" "plotExonJunc"
## [189] "plotExons" "plotFB" "plotFB.default" "plotFB.EListRaw"
## [193] "plotFB.RGList" "plotlines" "plotMA" "plotMA.default"
## [197] "plotMA.EList" "plotMA.EListRaw" "plotMA.MAList" "plotMA.MArrayLM"
## [201] "plotMA.RGList" "plotMA3by2" "plotMD" "plotMD.default"
## [205] "plotMD.EList" "plotMD.EListRaw" "plotMD.MAList" "plotMD.MArrayLM"
## [209] "plotMD.RGList" "plotMDS" "plotMDS.default" "plotMDS.MDS"
## [213] "plotPrintorder" "plotPrintTipLoess" "plotRLDF" "plotSA"
## [217] "plotSplice" "plotWithHighlights" "poolVar" "predFCm"
## [221] "printHead" "printorder" "printtipWeights" "propexpr"
## [225] "propTrueNull" "protectMetachar" "qqf" "qqt"
## [229] "rankSumTestWithCorrelation" "rbind.EList" "rbind.EListRaw" "rbind.MAList"
## [233] "rbind.RGList" "read.columns" "read.idat" "read.ilmn"
## [237] "read.ilmn.targets" "read.imagene" "read.maimages" "readGAL"
## [241] "readGenericHeader" "readGPRHeader" "readImaGeneHeader" "readSMDHeader"
## [245] "readSpotTypes" "readTargets" "removeBatchEffect" "removeExt"
## [249] "residuals.MArrayLM" "RG.MA" "roast" "roast.default"
## [253] "romer" "romer.default" "sampleInfoFromGEO" "selectModel"
## [257] "show" "spotc" "spotr" "squeezeVar"
## [261] "strsplit2" "subsetListOfArrays" "summary.EList" "summary.EListRaw"
## [265] "summary.MAList" "summary.MArrayLM" "summary.RGList" "summary.TestResults"
## [269] "tail.EList" "tail.EListRaw" "tail.MAList" "tail.MArrayLM"
## [273] "tail.RGList" "tail.TestResults" "targetsA2C" "tmixture.matrix"
## [277] "tmixture.vector" "topGO" "topKEGG" "topRomer"
## [281] "topSplice" "topTable" "topTableF" "topTreat"
## [285] "treat" "tricubeMovingAverage" "trigammaInverse" "trimWhiteSpace"
## [289] "tZscore" "unique.TestResults" "uniquegenelist" "uniqueTargets"
## [293] "unwrapdups" "vennCounts" "vennDiagram" "volcanoplot"
## [297] "voom" "vooma" "voomaByGroup" "voomaLmFit"
## [301] "voomWithQualityWeights" "weighted.median" "weightedLowess" "wilcoxGST"
## [305] "write.fit" "wsva" "wtarea" "wtflags"
## [309] "wtIgnore.Filter" "zscore" "zscoreGamma" "zscoreHyper"
## [313] "zscoreT"
函数和R包
1 函数与参数
1.1 形式参数与实际参数
seq(from=3,to=21,by=3) seq:函数;from,to,by:形式参数,模板,可不出现;3,21,3:实际参数,可改动
1.2 写函数的函数
示例 jimmy <- function(a,b,m = 2){
(a b)^m } function:创造其他函数 jimmy:函数名称;m存在默认值=2,可修改 函数的原理是代数,把参数带入大括号里的代码
2 R包(R package)介绍
2.1 R包是什么?
-可以粗略理解为多个函数的打包存档
-有的R包中会有数据,甚至只有数据没有函数
-包含函数/数据、帮助文档、描述文件等。
学习目的:找到所有R包的使用规律
3 R包镜像
镜像网站相当于主网站的副本(备份),访问主网站存在障碍时可选择访问镜像网站。
R和Bioconductor主网站位于国外,可选择国内镜像e.g.清华镜像(tuna, Beijing)、中科大镜像(ustc, Hefei)
R包设置
方法1:tools--global option--packages
镜像只能来自于CRAN网站
方法2:代码设置
options("repos"=c(CRAN="http://mirrors.tuna.tsinghua.edu.cn/CRAN/")) options(BioC_mirror="http://mirrors.tuna.tsinghua.edu.cn/bioconductor/") Bioconductor相对CRAN复杂一些,且只能通过代码设置
4 R包来源与安装方式
4.1 CRAN网站
install.packages()
4.2 Bioconductor
BiocManager::install()
4.3 Github
devtools::install_github() 例:devtools::install_github("jmzeng1314/idmap1") jmzeng:发布者用户名;idmap1:包名
5 xx包怎么安装?
5.1 安装
CRAN: install.packages() Bioconductor: BiocManager::install() Github: devtools::install_github()
5.2 安装后加载
library() e.g.library(stringr) require() e.g.require(stringr) 仅需安装一次,但每次打开新的session都需要加载
6 R包安装和使用的逻辑
6.1 安装包-加载包-使用包里的函数
library(stringr) library()是检查是否安装成功的标准 R包安装成功的唯一标准:library()没有error
str_detect(x,"a")
6.2 已经安装的包
可以用::快速调用里面的函数
e.g.
pheatmap::pheatmap(volcano) BiocManager::install("limma") devtools::install_github() 包名::函数名(),相当于: library(pheatmap);pheatmap(volcano) library(a)中,a会被认为是包名,解决方案: library(a,character.only = T)
满分操作
if(!require(stringr))install.packages("stringr") #已安装-加载成功-require()-T--!require-F #未安装-加载失败-require()-F--!require-T #if(FALSE),则后面代码被跳过-不安装 #if(TRUE),则后面代码被执行-安装
可用pacman包方便快捷地批量管理各种包
提示信息设置为英文:
Sys.setenv(LANGUAGE = "en")
7 R包如何使用-获取帮助
7.1 快速查看函数帮助文档
“?” 函数名
?sd sd(x,na.rm = FALSE) #若存在缺失值则应写sd(x,na.rm=TRUE)
7.2 找R包介绍页面
limma:Linear Models for Microarray Data
browseVignettes("limma")
7.3 Vignettes
browseVignettes("limma") #同上
7.4 列出一个包里都有哪些函数或数据
ls("package:limma")
文件读写
1 注意用Project管理工作目录
原因1:文件没有存放于工作目录下(RProject)
原因2:拼写错误(用tab自动补齐)
2 文件读取
read.csv() #通常读取CSV文件 read.csv("ex3.csv") read.table() #通常读取TXT格式文件 失败有两种情况:1.报错;2.意外的结果 直接读取如果失败,则需要指定一些参数 #read.系列函数 参数通用,不同函数间参数默认值不同 read.table()默认header=F,若TXT文件存在列名,应改为: read.table("文件名",header = TRUE) read.csv()设置行名和特殊字符: read.csv("文件名",row.names = 1,check.names = F) 规则:数据框不允许重复的行名 报错解决方法: 1.先不加row.names参数进行读取; 2.处理第一列的重复值(去重复,两行求平均值,合并为一行); 3.将第一行设为行名
3 数据框导出
CSV格式:write.csv()
write.csv(ex2,file = "example.csv") ex2:要导出数据框的变量名;example.csv:为导出的文件命名
TXT格式:write.table()#不常用
write.table(ex2,filfe = "example.txt")
用于读取/导出文件的R包
base:
read.csv() read.table() read.delim() #存在个别参数改变,同样适用于读取txt文件 write.table() write.csv()
readr:
read_table() read_csv() read_tsv() write_table() write_csv()
data.table:
fread() #不区分CSV,TXT等格式,读取速度较快,但会加入data.table数据结构,且不支持直接设置行名
rio:
import() import_list() #导入多个工作簿的excel表格 export()
data.table读取
library(data.table) ex1 = fread("ex1.txt") class(ex1) 1 "data.table" "data.frame" 修改ex1 = fread("ex1.txt",data.table = F)
ex2 = fread("ex2.csv",data.table = F) #不支持直接设置行名 library(tibble) ex2 = column_to_rownames(ex2,"V1") #将ex2中“V1”列设置为行名
rio读取
library(rio) #一个函数支持读取多种格式,见帮助文档 ex1 = import("ex1.txt") #一个函数支持导出多种格式,见帮助文档 export(ex1,file = "ex1.xlsx")
R语言能够读取的文件格式
通用格式:CSV, XLS, TXT, TSV, JSON, PDF, SPSS...
生信格式:FASTA, FASTQ, BAM, VCF, BED, GTF...
引用自生信技能树