美文网首页R语言
R包安装及设置镜像

R包安装及设置镜像

作者: yancylau | 来源:发表于2018-12-19 22:57 被阅读221次

    安装包及设置镜像

    安装包

    安装CRAN软件

    install.packages(pkgs, lib, repos = getOption("repos"), quiet = FALSE)
    

    lib: character vector giving the library directories where to install the packages. Recycled as needed. If missing, defaults to the first element of .libPaths().
    repos: character vector, the base URL(s) of the repositories to use, e.g., the URL of a CRAN mirror such as "https://cloud.r-project.org". For more details on supported URL schemes see url.

    quiet: logical: if true, reduce the amount of output.

    安装Bioconductor 相关包

    # Source biocLite from web
    source("https://bioconductor.org/biocLite.R")
    biocLite("package_name")
    
    # Use biocLite under BiocInstaller
    install.packages("BiocInstaller")
    BiocInstaller::biocLite("package_name")  
    
    biocLite(pkgs=c("Biobase", "IRanges", "AnnotationDbi"),  
             suppressUpdates=FALSE,
             suppressAutoUpdate=FALSE,
             siteRepos=character(),
             ask=TRUE, ...)
    

    Install or update Bioconductor, CRAN, or GitHub packages

    if (!requireNamespace("BiocManager", quietly=TRUE)) 
        install.packages("BiocManager")
    BiocManager::install("package_name")
    

    Note: BioInstaller and biocLiteare deprecated use the biocmanager cran package instead.

    本地安装源码包

    install.packages("path/to/pkg/package_name.tar.gz", repos = NULL, type = "source")
    

    源码编译安装的时候,要将源码压缩成tar.gz格式再安装,zip格式会出奇怪的错误。

    R包相关操作

    # 查看R包安装位置
    .libPaths()
    
    # 查看已安装的包
    installed.packages()
    
    # 查看包版本
    packageVersion("package_name")
    
    # 更新包
    update.packages("package_name")
    
    # 加载包
    library("package_name")
    require("package_name")
    
    # 查看加载的包
    .packages()
    
    # 移除已加载的包(将包从R运行环境中移除)
    detach("package_name")
    
    # 彻底删除已安装的包:
    remove.packages("package_name", lib = file.path("path/to/library"))
    

    镜像设置

    常用镜像地址

    Name URL host type
    China (Anhui) [https] https://mirrors.ustc.edu.cn/bioc/ 中科大 CRAN_mirror
    China (Anhui) http://mirrors.ustc.edu.cn/bioc/ 中科大 CRAN_mirror
    China (Beijing) [https] https://mirrors.tuna.tsinghua.edu.cn/CRAN/ 清华 BioC_mirror
    China (Beijing) http://mirrors.tuna.tsinghua.edu.cn/CRAN/ 清华 BioC_mirror

    CRAN镜像下载安装包

    # 从CRAN镜像下载安装包
    install.packages(pkgs, repos = "https://mirrors.ustc.edu.cn/CRAN/")
    
    # 从BioC镜像安装包
    biocLite(pkg, siteRepos = "http://mirrors.ustc.edu.cn/bioc/")
    

    常用函数

    # 查看当前镜像地址
    getOption("repos")
    
    # 查看R_HOME地址
    R.home()
    
    R_HOME/doc/CRAN_mirrors.csv
    R_HOME/doc/BioC_mirrors.csv
    

    将镜像添加到配置文件

    Bioconductor 镜像源配置文件之一是 .Rprofile (linux 下位于 ~/.Rprofile )。

    在文末添加如下语句:

    options(BioC_mirror="https://mirrors.tuna.tsinghua.edu.cn/bioconductor")
    

    相关文章

      网友评论

        本文标题:R包安装及设置镜像

        本文链接:https://www.haomeiwen.com/subject/abtsqqtx.html