如何优雅地管理R包

2023-04-17 17:35:28 浏览数 (1)

using 加载R包

using函数是我写在$HOME/.Rprofile中的函数,因此每次打开R就能使用。

using的功能是一次加载多个包,并且使用了suppressPackageStartupMessages函数,因此不会显示加载包过程中的信息。

$HOME/.Rprofile文件

代码语言:shell复制
Sys.setenv(LANGUAGE = "en")
options(
    bitmapType = "cairo",
    warnPartialMatchArgs = TRUE,
    warnPartialMatchAttr = TRUE,
    warnPartialMatchDollar = TRUE,
    timeout = 999999,
    R_MAX_VSIZE = 2^30
)


# library
using <- function(...) {
    packages <- as.character(match.call(expand.dots = FALSE)[[2]])

    if (length(packages) == 0) {
        return(invisible())
    }
    # Attempt to load packages making note of which don't load
    loaded <- sapply(packages, function(x) {
        # Try to load package
        if (suppressPackageStartupMessages(require(x, character.only = TRUE, quietly = TRUE))) {
            return(TRUE)
        }
        # Couldn't load
        return(FALSE)
    })

    # Give a warning if some packags couldn't be loaded
    if (!all(loaded)) {
        failed <- packages[!loaded]
        warning("n Failed to load: ", paste(failed, collapse = ", "))
    }
    return(invisible(loaded))
}

# pre-library packages
using(pak, data.table, stringr, tibble, dplyr, tidyr, purrr, magrittr)

# mirrors
pak::repo_add(
    CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN",
    BioCsoft = "https://mirrors.tuna.tsinghua.edu.cn/bioconductor/packages/3.16/bioc",
    BioCann = "https://mirrors.tuna.tsinghua.edu.cn/bioconductor/packages/3.16/data/annotation",
    BioCexp = "https://mirrors.tuna.tsinghua.edu.cn/bioconductor/packages/3.16/data/experiment",
    BioCworkflows = "https://mirrors.tuna.tsinghua.edu.cn/bioconductor/packages/3.16/workflows"
)

pak添加镜像

写在$HOME/.Rprofile文件中

代码语言:text复制
pak::repo_add(
    CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN",
    BioCsoft = "https://mirrors.tuna.tsinghua.edu.cn/bioconductor/packages/3.16/bioc",
    BioCann = "https://mirrors.tuna.tsinghua.edu.cn/bioconductor/packages/3.16/data/annotation",
    BioCexp = "https://mirrors.tuna.tsinghua.edu.cn/bioconductor/packages/3.16/data/experiment",
    BioCworkflows = "https://mirrors.tuna.tsinghua.edu.cn/bioconductor/packages/3.16/workflows"
)

pak安装R包

使用pak管理R包,可以从Bioconductor、CRAN、Github、本地、URL安装R包,解决了R包安装需要多个不同R包去安装的问题。

除了本地安装使用local_install函数,其他几种安装方式都是用pkg_install函数

代码语言:text复制
install.packages("pak")
using(pak)
  • CRAN
代码语言:shell复制
pak::pkg_install("dplyr")
  • Bioconductor
代码语言:shell复制
pak::pkg_install("ComplexHeatmap")
  • GitHub
代码语言:shell复制
pak::pkg_install("sqjin/CellChat")
  • URL
代码语言:shell复制
pak::pkg_install("url::https://cytotrace.stanford.edu/CytoTRACE_0.3.3.tar.gz")
  • local
代码语言:shell复制
# shell
wget https://cytotrace.stanford.edu/CytoTRACE_0.3.3.tar.gz
tar -xf CytoTRACE_0.3.3.tar.gz  # 解压后为CytoTRACE

# R
pak::local_install("CytoTRACE")
  • 一次安装多个R包
代码语言:shell复制
pak::pkg_install(c("BiocNeighbors", "ComplexHeatmap", "circlize", "NMF"))

pak删除R包

代码语言:shell复制
pkg_remove("CytoTRACE")

pak查寻依赖包

只能查CRAN 或 Bioconductor的包的依赖

代码语言:shell复制
pak::pkg_deps("tibble")
代码语言:text复制
✔ Updated metadata database: 7.30 MB in 9 files.                          
✔ Updating metadata database ... done                                     
# A data frame: 11 × 32                                                   
   ref    type  direct directpkg status package version license needscompilation
   <chr>  <chr> <lgl>  <lgl>     <chr>  <chr>   <chr>   <chr>   <lgl>           
 1 cli    stan… FALSE  FALSE     OK     cli     3.6.1   MIT   … FALSE           
 2 fansi  stan… FALSE  FALSE     OK     fansi   1.0.4   GPL-2 … FALSE           
 3 glue   stan… FALSE  FALSE     OK     glue    1.6.2   MIT   … FALSE           
 4 lifec… stan… FALSE  FALSE     OK     lifecy… 1.0.3   MIT   … FALSE           
 5 magri… stan… FALSE  FALSE     OK     magrit… 2.0.3   MIT   … FALSE           
 6 pillar stan… FALSE  FALSE     OK     pillar  1.9.0   MIT   … FALSE           
 7 pkgco… stan… FALSE  FALSE     OK     pkgcon… 2.0.3   MIT   … FALSE           
 8 rlang  stan… FALSE  FALSE     OK     rlang   1.1.0   MIT   … FALSE           
 9 tibble stan… TRUE   TRUE      OK     tibble  3.2.1   MIT   … FALSE           
10 utf8   stan… FALSE  FALSE     OK     utf8    1.2.3   Apache… FALSE           
11 vctrs  stan… FALSE  FALSE     OK     vctrs   0.6.1   MIT   … FALSE           
# ℹ 23 more variables: priority <chr>, md5sum <chr>, sha256 <chr>,
#   filesize <int>, built <chr>, platform <chr>, rversion <chr>,
#   repotype <chr>, repodir <chr>, target <chr>, deps <list>, mirror <chr>,
#   sources <list>, remote <list>, error <list>, metadata <list>,
#   dep_types <list>, params <list>, sysreqs <chr>, cache_status <chr>,
#   lib_status <chr>, old_version <chr>, new_version <chr>

依赖树

代码语言:shell复制
pak::pkg_deps_tree("tibble")
代码语言:text复制
tibble 3.2.1 ✨ ⬇ (unknown size)
├─fansi 1.0.4 ✨ ⬇ (unknown size)
├─lifecycle 1.0.3 ✨ ⬇ (unknown size)
│ ├─cli 3.6.1 ✨ ⬇ (unknown size)
│ ├─glue 1.6.2 ✨ ⬇ (unknown size)
│ └─rlang 1.1.0 ✨ ⬇ (unknown size)
├─magrittr 2.0.3 ✨ ⬇ (unknown size)
├─pillar 1.9.0 ✨ ⬇ (unknown size)
│ ├─cli
│ ├─fansi
│ ├─glue
│ ├─lifecycle
│ ├─rlang
│ ├─utf8 1.2.3 ✨ ⬇ (unknown size)
│ └─vctrs 0.6.1 ✨ ⬇ (unknown size)
│   ├─cli
│   ├─glue
│   ├─lifecycle
│   └─rlang
├─pkgconfig 2.0.3 ✨ ⬇ (unknown size)
├─rlang
└─vctrs

Key:  ✨ new |  ⬇ download

一个文件夹中的依赖关系

这个tree实在太长了,我只保留前几行和后几行

代码语言:text复制
# shell
wget https://cytotrace.stanford.edu/CytoTRACE_0.3.3.tar.gz
tar -xf CytoTRACE_0.3.3.tar.gz # 解压后为CytoTRACE

# R
pak::local_deps_tree("CytoTRACE")
代码语言:text复制
✔ Loading metadata database ... done
local::CytoTRACE 0.3.3 ✨?? ⬇ (unknown size)                             
├─sva 3.46.0 ✨ ⬇ (unknown size)
│ ├─mgcv 1.8-42 
│ │ ├─nlme 3.1-162 
│ └─magrittr
└─plyr 1.8.8 ✨ ⬇ (unknown size)
  └─Rcpp

Key:  ✨ new | ✋ outdated |  ⬇ download | ?? build

安装一个文件夹中的依赖s

代码语言:text复制
local_install_deps("CytoTRACE")

# Installs development and optional dependencies as well.
local_install_dev_deps("CytoTRACE")
代码语言:text复制
→ Will update 3 packages.
→ Will download 4 packages with unknown size.
  DEoptimR 1.0-11    → 1.0-12     ⬇
  RCurl    1.98-1.10 → 1.98-1.12  ⬇
  RSQLite  2.3.0     → 2.3.1      ⬇
? Do you want to continue (Y/n) 

pak还有更多功能,比如增加Repositories、Lock file等可以在官网查看

Reference

代码语言:shell复制
https://pak.r-lib.org/reference/get-started.html

0 人点赞