教你如何优雅地管理R包

2024-04-23  本文已影响0人  生信探索

在R启动的时候,会自动source工作目录下的.Rprofile文件;Mac和Linux中默认的路径为$HOME,Windows中默认路径为C:\Users\用户名\Documents

把代码框中的代码复制.Rprofile文件中,如果.Rprofile不存在则需要新建;

using函数是我写在.Rprofile中的函数,因此每次打开R就能使用,using的功能是一次加载多个包,并且使用了suppressPackageStartupMessages函数,因此不会显示加载包过程中的信息。

# 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.18/bioc",

    BioCann = "https://mirrors.tuna.tsinghua.edu.cn/bioconductor/packages/3.18/data/annotation",

    BioCexp = "https://mirrors.tuna.tsinghua.edu.cn/bioconductor/packages/3.18/data/experiment",

    BioCworkflows = "https://mirrors.tuna.tsinghua.edu.cn/bioconductor/packages/3.18/workflows"

)

pak添加镜像

写在.Rprofile文件中

pak安装R包

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

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

install.packages("pak")

using(pak)

CRAN

pak::pkg_install("dplyr")

Bioconductor

pak::pkg_install("ComplexHeatmap")

GitHub

上一篇下一篇

猜你喜欢

热点阅读