美文网首页
Linux Ubuntu R语言安装配置

Linux Ubuntu R语言安装配置

作者: 火卫控 | 来源:发表于2023-07-21 23:17 被阅读0次

    先安装MiniConda
    再安装 R4.3.1
    再安装R包

    1. 安装MiniConda

    sudo su 切换到管理员root
    下载Miniconda

    wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh
    

    安装Miniconda

    bash Miniconda3-latest-Linux-x86_64.sh
    

    回车空格打完两个yes

    conda -h
    

    查看conda目前的镜像,命令:

    conda config --show
    

    添加镜像源

    conda config --add channels  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
     
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
     
    conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
     
    conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
     
    conda config --add channels https://mirrors.pku.edu.cn/anaconda/pkgs/main/
    

    进入环境变量文件,

    vim .bashrc
    

    添加 Miniconda中bin路径

    export PATH=/data/username/miniconda3/bin/:$PATH
    

    或者更新一下

    source ~/.bashrc
    

    利用conda安装一些软件测试,fastp,fastqc

    conda install fastp
    conda install fastqc
    

    2.准备安装R

    创建一个叫R4.3.1的环境,并安装R。

    conda create -n R4.3.1
    conda activate R4.3.1
    conda install -c conda-forge r-base=4.3.1
    

    控制台显示如下:

    (base) root@DESKTOP-727JVLV:/home/ai_qiangyun# conda activate R4.3.1
    (R4.3.1) root@DESKTOP-727JVLV:/home/ai_qiangyun# conda isntall -c conda-forge r-base=4.3.1
    
    CommandNotFoundError: No command 'conda isntall'.
    Did you mean 'conda install'?
    
    (R4.3.1) root@DESKTOP-727JVLV:/home/ai_qiangyun# conda install -c conda-forge r-base=4.3.1
    Collecting package metadata (current_repodata.json): done
    Solving environment: unsuccessful initial attempt using frozen solve. Retrying with flexible solve.
    Solving environment: unsuccessful attempt using repodata from current_repodata.json, retrying with next repodata source.
    Collecting package metadata (repodata.json): done
    Solving environment: done
    
    ## Package Plan ##
    
      environment location: /root/miniconda3/envs/R4.3.1
    
      added / updated specs:
        - r-base=4.3.1
    
    
    The following packages will be downloaded:
    
        package                    |            build
        ---------------------------|-----------------
        _libgcc_mutex-0.1          |      conda_forge           3 KB  conda-forge
        _openmp_mutex-4.5          |            2_gnu          23 KB  conda-forge
        _r-mutex-1.0.1             |      anacondar_1           3 KB  conda-forge
        binutils_impl_linux-64-2.40|       hf600244_0         5.2 MB  conda-forge
        bwidget-1.9.14             |       ha770c72_1         120 KB  conda-forge
        bzip2-1.0.8                |       h7f98852_4         484 KB  conda-forge
        c-ares-1.19.1              |       hd590300_0         111 KB  conda-forge
        ca-certificates-2023.7.22  |       hbcca054_0         146 KB  conda-forge
        cairo-1.16.0               |    hbbf8b49_1016         1.1 MB  conda-forge
        curl-8.2.0                 |       hca28451_0          89 KB  conda-forge
        expat-2.5.0                |       hcb278e6_1         134 KB  conda-forge
        font-ttf-dejavu-sans-mono-2.37|       hab24e00_0         388 KB  conda-forge
        font-ttf-inconsolata-3.000 |       h77eed37_0          94 KB  conda-forge
        font-ttf-source-code-pro-2.038|       h77eed37_0         684 KB  conda-forge
        font-ttf-ubuntu-0.83       |       hab24e00_0         1.9 MB  conda-forge
        fontconfig-2.14.2          |       h14ed4e7_0         266 KB  conda-forge
        fonts-conda-ecosystem-1    |                0           4 KB  conda-forge
        fonts-conda-forge-1        |                0           4 KB  conda-forge
        freetype-2.12.1            |       hca18f0e_1         611 KB  conda-forge
        fribidi-1.0.10             |       h36c2ea0_0         112 KB  conda-forge
        gcc_impl_linux-64-13.1.0   |       hc4be1a9_0        51.7 MB  conda-forge
    ...................
    .
    .
    .
    
    

    如何用R

    后面我们就首先激活环境

    conda activate R4.3.1
    

    然后R脚本命令行模式输入一个R就行。退出就输入q()就可以啦~

    进入环境

    source activate R4.2.2
    conda activate R4.2.2
    

    R命令行模式中安装R包,例如,安装ggplot2

    (R4.3.1) root@DESKTOP-727JVLV:/home/ai_qiangyun# R
    
    R version 4.3.1 (2023-06-16) -- "Beagle Scouts"
    Copyright (C) 2023 The R Foundation for Statistical Computing
    Platform: x86_64-conda-linux-gnu (64-bit)
    
    R is free software and comes with ABSOLUTELY NO WARRANTY.
    You are welcome to redistribute it under certain conditions.
    Type 'license()' or 'licence()' for distribution details.
    
    R is a collaborative project with many contributors.
    Type 'contributors()' for more information and
    'citation()' on how to cite R or R packages in publications.
    
    Type 'demo()' for some demos, 'help()' for on-line help, or
    'help.start()' for an HTML browser interface to help.
    Type 'q()' to quit R.
    
    > ^[[200~install.packages("ggplot2")~
    Error: unexpected input in "
    > install.packages("ggplot2")
    --- Please select a CRAN mirror for use in this session ---
    also installing the dependencies ‘lattice’, ‘colorspace’, ‘utf8’, ‘nlme’, ‘Matrix’, ‘farver’, ‘labeling’, ‘munsell’, ‘R6’, ‘RColorBrewer’, ‘viridisLite’, ‘fansi’, ‘magrittr’, ‘pillar’, ‘pkgconfig’, ‘cli’, ‘glue’, ‘gtable’, ‘isoband’, ‘lifecycle’, ‘MASS’, ‘mgcv’, ‘rlang’, ‘scales’, ‘tibble’, ‘vctrs’, ‘withr’
    
    trying URL 'https://mirrors.e-ducation.cn/CRAN/src/contrib/lattice_0.21-8.tar.gz'
    Content type 'application/x-gzip' length 589330 bytes (575 KB)
    ==================================================
    downloaded 575 KB
    
    trying URL 'https://mirrors.e-ducation.cn/CRAN/src/contrib/colorspace_2.1-0.tar.gz'
    Content type 'application/x-gzip' length 2116339 bytes (2.0 MB)
    ==================================================
    downloaded 2.0 MB
    
    

    R包安装方法及安装记录

    (1)对于一般的R包

    install.packages("package name")
    

    (2)对于Bioconductor的包(使用BiocManager安装)

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

    注意:有时候可根据回显添加force = TRUE

    安装实例

    install.packages("stringi")
    install.packages("ggplot2")
    install.packages("pls")
    install.packages("ggthemes")
    
    if (!requireNamespace("BiocManager", quietly = TRUE))
     install.packages("BiocManager")
    
    BiocManager::install("clusterProfiler")
    
    devtools::install_github("strengejacke/sjmisc")
    devtools::install_github("kassambara/ggpubr")
    devtools::install_github("cran/nloptr@1.2.2.3")
    devtools::install_github("jokergoo/ComplexHeatmap")
    devtools::install_github("https://github.com/cran/RbioRXN.git")
    

    查看已经安装了的包

    installed.packages()
    
    
    > installed.packages()
                 Package        LibPath
    base         "base"         "/root/miniconda3/envs/R4.3.1/lib/R/library"
    cli          "cli"          "/root/miniconda3/envs/R4.3.1/lib/R/library"
    colorspace   "colorspace"   "/root/miniconda3/envs/R4.3.1/lib/R/library"
    compiler     "compiler"     "/root/miniconda3/envs/R4.3.1/lib/R/library"
    datasets     "datasets"     "/root/miniconda3/envs/R4.3.1/lib/R/library"
    fansi        "fansi"        "/root/miniconda3/envs/R4.3.1/lib/R/library"
    farver       "farver"       "/root/miniconda3/envs/R4.3.1/lib/R/library"
    glue         "glue"         "/root/miniconda3/envs/R4.3.1/lib/R/library"
    graphics     "graphics"     "/root/miniconda3/envs/R4.3.1/lib/R/library"
    grDevices    "grDevices"    "/root/miniconda3/envs/R4.3.1/lib/R/library"
    grid         "grid"         "/root/miniconda3/envs/R4.3.1/lib/R/library"
    gtable       "gtable"       "/root/miniconda3/envs/R4.3.1/lib/R/library"
    isoband      "isoband"      "/root/miniconda3/envs/R4.3.1/lib/R/library"
    labeling     "labeling"     "/root/miniconda3/envs/R4.3.1/lib/R/library"
    lattice      "lattice"      "/root/miniconda3/envs/R4.3.1/lib/R/library"
    lifecycle    "lifecycle"    "/root/miniconda3/envs/R4.3.1/lib/R/library"
    magrittr     "magrittr"     "/root/miniconda3/envs/R4.3.1/lib/R/library"
    MASS         "MASS"         "/root/miniconda3/envs/R4.3.1/lib/R/library"
    Matrix       "Matrix"       "/root/miniconda3/envs/R4.3.1/lib/R/library"
    methods      "methods"      "/root/miniconda3/envs/R4.3.1/lib/R/library"
    mgcv         "mgcv"         "/root/miniconda3/envs/R4.3.1/lib/R/library"
    munsell      "munsell"      "/root/miniconda3/envs/R4.3.1/lib/R/library"
    nlme         "nlme"         "/root/miniconda3/envs/R4.3.1/lib/R/library"
    parallel     "parallel"     "/root/miniconda3/envs/R4.3.1/lib/R/library"
    pkgconfig    "pkgconfig"    "/root/miniconda3/envs/R4.3.1/lib/R/library"
    R6           "R6"           "/root/miniconda3/envs/R4.3.1/lib/R/library"
    RColorBrewer "RColorBrewer" "/root/miniconda3/envs/R4.3.1/lib/R/library"
    rlang        "rlang"        "/root/miniconda3/envs/R4.3.1/lib/R/library"
    scales       "scales"       "/root/miniconda3/envs/R4.3.1/lib/R/library"
    splines      "splines"      "/root/miniconda3/envs/R4.3.1/lib/R/library"
    stats        "stats"        "/root/miniconda3/envs/R4.3.1/lib/R/library"
    stats4       "stats4"       "/root/miniconda3/envs/R4.3.1/lib/R/library"
    tcltk        "tcltk"        "/root/miniconda3/envs/R4.3.1/lib/R/library"
    tools        "tools"        "/root/miniconda3/envs/R4.3.1/lib/R/library"
    utf8         "utf8"         "/root/miniconda3/envs/R4.3.1/lib/R/library"
    utils        "utils"        "/root/miniconda3/envs/R4.3.1/lib/R/library"
    vctrs        "vctrs"        "/root/miniconda3/envs/R4.3.1/lib/R/library"
    viridisLite  "viridisLite"  "/root/miniconda3/envs/R4.3.1/lib/R/library"
    withr        "withr"        "/root/miniconda3/envs/R4.3.1/lib/R/library"
                 Version   Priority
    base         "4.3.1"   "base"
    cli          "3.6.1"   NA
    colorspace   "2.1-0"   NA
    compiler     "4.3.1"   "base"
    datasets     "4.3.1"   "base"
    fansi        "1.0.4"   NA
    farver       "2.1.1"   NA
    ....
    

    它会给出来,所有已安装的R包的详细信息,包括包名,路径,版本,优先级,建议等

    提取已安装的包名:

    as.data.frame(installed.packages())$Package
    

    查看已加载的R包

    这里的小括号不可以省略

    (.packages())
    

    可以看到,默认加载了7个基础包。
    安装BIOManager遇到问题 , 节点选择需要弹窗,但是无法弹出,加一行命令

    chooseCRANmirror(graphics=FALSE)
    

    运行结果如下:

    > install.packages("BiocManager")
    --- Please select a CRAN mirror for use in this session ---
    
    
    Error in structure(.External(.C_dotTclObjv, objv), class = "tclObj") :
      [tcl] grab failed: window not viewable.
    >
    >
    >
    > chooseCRANmirror(graphics=FALSE)
    Secure CRAN mirrors
    
     1: 0-Cloud [https]
     2: Australia (Canberra) [https]
     3: Australia (Melbourne 1) [https]
     4: Australia (Melbourne 2) [https]
     5: Australia (Perth) [https]
     6: Austria [https]
     7: Belgium (Brussels) [https]
     8: Brazil (PR) [https]
     9: Brazil (SP 1) [https]
    10: Brazil (SP 2) [https]
    11: Bulgaria [https]
    12: Canada (MB) [https]
    13: Canada (ON) [https]
    14: Chile (Santiago) [https]
    15: China (Beijing 2) [https]
    16: China (Beijing 3) [https]
    17: China (Hefei) [https]
    18: China (Hong Kong) [https]
    19: China (Guangzhou) [https]
    20: China (Jinan) [https]
    21: China (Lanzhou) [https]
    
    

    安装成功后就可以用BiocManager安装R包了

    > library(BiocManager)
    Bioconductor version 3.17 (BiocManager 1.30.21.1), R 4.3.1 (2023-06-16)
    > BiocManager::install("limma")
    'getOption("repos")' replaces Bioconductor standard repositories, see
    'help("repositories", package = "BiocManager")' for details.
    Replacement repositories:
        CRAN: https://mirrors.e-ducation.cn/CRAN
    Bioconductor version 3.17 (BiocManager 1.30.21.1), R 4.3.1 (2023-06-16)
    Installing package(s) 'BiocVersion', 'limma'
    trying URL 'https://bioconductor.org/packages/3.17/bioc/src/contrib/BiocVersion_3.17.1.tar.gz'
    Content type 'application/x-gzip' length 971 bytes
    ==================================================
    downloaded 971 bytes
    
    

    参考:

    Miniconda官网
    Miniconda安装及使用--小白上路
    清华开源Anaconda库
    liunx如何把命令添加到环境变量,方便调用
    Liunx系统安装R
    R包安装方法及安装记录
    R语言中如何查看已安装的R包
    R语言 - Linux安装R语言扩展包相关命令(Windows雷同)

    相关文章

      网友评论

          本文标题:Linux Ubuntu R语言安装配置

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