教你如何优雅地管理R包

使用using 加载R包

在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值