R包相关函数

安装

# CRAN
install.packages("package_name")

# bioconductor
if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("package_names")

# github 
library("devtools")
install_github("github_user_name/package_name")

# local
devtools::install_local("path_to_package_file.zip")

升级

update.packages("package_names")

载入

library("package_names") # 如果不存在或缺少相关文件则会报错,程序停止运行

require("package_names") # 如果不存在或缺少相关文件不会报错而是警告,程序继续运行

character.only = TRUE参数可以使得这两个函数可以接受变量所代表的包名:

> a = "ggplot2"
> library(a)
Error in library(a) : there is no package called ‘a’
> library(a, character.only = TRUE)
>

suppressPackageStartupMessages函数使包加载信息不被显示

查看载入的所有包及信息

> sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-conda-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux Server 7.6 (Maipo)

Matrix products: default
BLAS/LAPACK: /sibcb2/bioinformatics2/wangjiahao/software/Miniconda3/envs/r-4.0/lib/libopenblasp-r0.3.10.so

locale:
 [1] LC_CTYPE=en_US.UTF-8          LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8           LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8       LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8          LC_NAME=en_US.UTF-8
 [9] LC_ADDRESS=en_US.UTF-8        LC_TELEPHONE=en_US.UTF-8
[11] LC_MEASUREMENT=en_US.UTF-8    LC_IDENTIFICATION=en_US.UTF-8

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

other attached packages:
[1] ggplot2_3.3.5 xlsx_0.6.5

loaded via a namespace (and not attached):
 [1] magrittr_2.0.1   tidyselect_1.1.1 munsell_0.5.0    colorspace_2.0-2
 [5] R6_2.5.1         rlang_0.4.12     fansi_0.5.0      dplyr_1.0.7
 [9] tools_4.0.5      grid_4.0.5       gtable_0.3.0     utf8_1.2.2
[13] DBI_1.1.1        withr_2.4.3      ellipsis_0.3.2   assertthat_0.2.1
[17] tibble_3.1.6     lifecycle_1.0.1  crayon_1.4.2     rJava_1.0-5
[21] purrr_0.3.4      vctrs_0.3.8      xlsxjars_0.6.1   glue_1.5.1
[25] compiler_4.0.5   pillar_1.6.4     generics_0.1.1   scales_1.1.1
[29] pkgconfig_2.0.3

> search()
 [1] ".GlobalEnv"        "package:ggplot2"   "package:xlsx"
 [4] "package:stats"     "package:graphics"  "package:grDevices"
 [7] "package:utils"     "package:datasets"  "package:methods"
[10] "Autoloads"         "package:base"

> print(.packages())
[1] "ggplot2"   "xlsx"      "stats"     "graphics"  "grDevices" "utils"
[7] "datasets"  "methods"   "base"

> packageVersion("ggplot2")
[1] ‘3.3.5’

查看包安装路径

> system.file(package = "ggplot2")
[1] "/sibcb2/bioinformatics2/wangjiahao/software/Miniconda3/envs/r-4.0/lib/R/library/ggplot2"

查看包详细信息

> packageDescription("ggplot2")
Package: ggplot2
Version: 3.3.5
Title: Create Elegant Data Visualisations Using the Grammar of Graphics
Description: A system for 'declaratively' creating graphics, based on
        "The Grammar of Graphics". You provide the data, tell 'ggplot2'
        how to map variables to aesthetics, what graphical primitives
        to use, and it takes care of the details.
......

查看/切换库路径

> .Library # 保存默认路径
[1] "/sibcb2/bioinformatics2/wangjiahao/software/Miniconda3/envs/r-4.0/lib/R/library"
> .libPaths() # 切换路径
[1] "/sibcb2/bioinformatics2/wangjiahao/software/Miniconda3/envs/r-4.0/lib/R/library"

帮助

> help(ggplot2)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容