zl程序教程

您现在的位置是:首页 >  IT要闻

当前栏目

08. R studio/R 工具指南(七:具体学习R 包相关的函数)

2023-04-18 14:58:02 时间

一些基本操作

参见:https://www.omicsclass.com/article/517

  • 安装
1、安装 install.packages()(括号内指定包的名字)
  • 加载
加载 library()或require() (括号内指定包的名字)

这里我们可以指定参数character.only = T 从而让library 函数接受字符串作为参数,从而方便的使用apply 族函数实现批量加载R 包:

my_packages<- c("maftools", "data.table", "RColorBrewer", "paletteer")
tmp <- sapply(my_packages, function(x) library(x, character.only = T)); rm(tmp)
  • 查询

我们可以直接查看所有包的版本信息,并对类别做了区分:

> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] zh_CN.UTF-8/zh_CN.UTF-8/zh_CN.UTF-8/C/zh_CN.UTF-8/zh_CN.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] maftools_2.2.10

loaded via a namespace (and not attached):
 [1] compiler_3.6.3     Matrix_1.2-18      tools_3.6.3       
 [4] RColorBrewer_1.1-2 survival_3.1-8     Rcpp_1.0.5        
 [7] tinytex_0.26       wordcloud_2.6      splines_3.6.3     
[10] grid_3.6.3         data.table_1.13.2  xfun_0.18         
[13] lattice_0.20-38 

还可以使用installed.packages() 获得关于包的更具体的信息:

> head(installed.packages())
                 Package           
abind            "abind"           
airway           "airway"          
AnnoProbe        "AnnoProbe"       
annotate         "annotate"        
AnnotationDbi    "AnnotationDbi"   
AnnotationFilter "AnnotationFilter"
                 LibPath                                                         
abind            "/Library/Frameworks/R.framework/Versions/3.6/Resources/library"
airway           "/Library/Frameworks/R.framework/Versions/3.6/Resources/library"
AnnoProbe        "/Library/Frameworks/R.framework/Versions/3.6/Resources/library"
annotate         "/Library/Frameworks/R.framework/Versions/3.6/Resources/library"
AnnotationDbi    "/Library/Frameworks/R.framework/Versions/3.6/Resources/library"
AnnotationFilter "/Library/Frameworks/R.framework/Versions/3.6/Resources/library"
                 Version  Priority
abind            "1.4-5"  NA      
airway           "1.6.0"  NA      
AnnoProbe        "0.1.0"  NA      
annotate         "1.64.0" NA      
AnnotationDbi    "1.48.0" NA      
AnnotationFilter "1.10.0" NA      
                 Depends                                                                                        
abind            "R (>= 1.5.0)"                                                                                 
airway           "R (>= 3.5.0), SummarizedExperiment"                                                           
AnnoProbe        "R (>= 3.4.0),
methods,"                                                                      
annotate         "R (>= 2.10), AnnotationDbi (>= 1.27.5), XML"                                                  
AnnotationDbi    "R (>= 2.7.0), methods, utils, stats4, BiocGenerics (>=
0.29.2), Biobase (>= 1.17.0), IRanges"
AnnotationFilter "R (>= 3.4.0)"                                                                                 
                 Imports                                                                                  
abind            "methods, utils"                                                                         
airway           NA                                                                                       
AnnoProbe        "remotes,
GEOquery,
limma,
ggplot2,
DT,
ggpubr,
pheatmap,"                         
annotate         "Biobase, DBI, xtable, graphics, utils, stats, methods,
BiocGenerics (>= 0.13.8), RCurl"
AnnotationDbi    "DBI, RSQLite, S4Vectors (>= 0.9.25)"                                                    
AnnotationFilter "utils, methods, GenomicRanges, lazyeval"                                                
                 LinkingTo
abind            NA       
airway           NA       
AnnoProbe        NA       
annotate         NA       
AnnotationDbi    NA       
AnnotationFilter NA       
                 Suggests                                                                                                                                                                                                         
abind            NA                                                                                                                                                                                                               
airway           "knitr, GEOquery"                                                                                                                                                                                                
AnnoProbe        "knitr,
KEGGREST,
rmarkdown"                                                                                                                                                                                   
annotate         "hgu95av2.db, genefilter, Biostrings (>= 2.25.10), IRanges,
rae230a.db, rae230aprobe, tkWidgets, GO.db, org.Hs.eg.db,
org.Mm.eg.db, hom.Hs.inp.db, humanCHRLOC, Rgraphviz, RUnit,"                             
AnnotationDbi    "hgu95av2.db, GO.db, org.Sc.sgd.db, org.At.tair.db, KEGG.db,
RUnit, TxDb.Hsapiens.UCSC.hg19.knownGene, hom.Hs.inp.db,
org.Hs.eg.db, reactome.db, AnnotationForge, graph,
EnsDb.Hsapiens.v75, BiocStyle, knitr"
AnnotationFilter "BiocStyle, knitr, testthat, RSQLite, org.Hs.eg.db"                                                                                                                                                              
                 Enhances License        License_is_FOSS
abind            NA       "LGPL (>= 2)"  NA             
airway           NA       "LGPL"         NA             
AnnoProbe        NA       "Artistic-2.0" NA             
annotate         NA       "Artistic-2.0" NA             
AnnotationDbi    NA       "Artistic-2.0" NA             
AnnotationFilter NA       "Artistic-2.0" NA             
                 License_restricts_use OS_type MD5sum NeedsCompilation
abind            NA                    NA      NA     "no"            
airway           NA                    NA      NA     "no"            
AnnoProbe        NA                    NA      NA     NA              
annotate         NA                    NA      NA     "no"            
AnnotationDbi    NA                    NA      NA     "no"            
AnnotationFilter NA                    NA      NA     "no"            
                 Built  
abind            "3.6.0"
airway           "3.6.3"
AnnoProbe        "3.6.3"
annotate         "3.6.1"
AnnotationDbi    "3.6.1"
AnnotationFilter "3.6.1"
  • 还可以查看R 所加载的所有包所在的路径 :.libPaths()这个其实还挺好用的,比如说有的时候,这个路径没有设置好,导致下载的R 包无法被library,都可能和这个路径有关。
  • 更新
update.packages() (括号内指定包的名字)
  • 卸载
remove.packages()(括号内指定包的名字
  • 已加载环境的包和从环境中卸除
# 查看已加载的包
(.packages())
# 卸除已加载的包
detach("package:RMySQL")

这个还是非常有用的。有的时候有的包的更新所依赖的包如果加载在环境中的话,会导致报错。这时候可以detach 掉他萌~(其实更暴力的就是重新打开R proj)

还是我的R 包

我为什么要用还呢?https://github.com/mugpeng/pengToolkit

##################################################
## Project: Rescue the Princess
## File name: file.R
## Date: Sat Aug 28 19:45:14 2021
## Author: Peng
## Email: mugpeng@foxmail.com
## R_Version: R version 4.0.5 (2021-03-31)
## R_Studio_Version: 1.4.1106
## Platform Version: macOS Mojave 10.14.6
##################################################
rm(list = ls())

# 0.  packages loaded && data preparation ----
my_packages<- c("maftools", "data.table", "RColorBrewer", "paletteer")
tmp <- sapply(my_packages, function(x) library(x, character.only = T)); rm(tmp)
tmp <- pengToolkit::check_packages_version(my_packages)

# 1.   ----
# 2.   ----
#  ----
#  ----

分享脚本时,告诉别人R 和R 包的版本号,是个好习惯~

详细介绍,可以参见我的github主页。