生信喵 发表于 2022-7-6 22:50:18

R 语言环境搭建

本帖最后由 生信喵 于 2022-7-7 10:36 编辑

      R 语言已经广泛的应用与生物信息分析中,包括 RNAseq,单细胞,生物统计,绘图等都要用到 R 语言。R 语言是生物信息分析平台重要的组成部分。本章节中我们将在服务器中配置完整的 R 语言分析环境。
一、安装 R 软件
      默认的 CentOS 源中是没有 R 软件的,需要安装 epel 源,我们之前已经安装了,R 软件在里面的名字为R.x86_64,如果不知道这个名字,可以使用yum search进行搜索。由于yum search不能使用正则表达式进行限制范围,会将所有包含 R 字符的结果都列出来,这里我们利用一个技巧。
(base) # yum search R | grep "^R"
R.x86_64 : A language for data analysis and graphics
R-4.1.1.x86_64 : GNU R statistical computation and graphics system
R-RInside.x86_64 : C++ Classes to Embed R in C++ (and C) Applications
R-RInside-devel.x86_64 : RInside Development Files
R-RInside-examples.x86_64 : RInside Examples
R-RUnit.noarch : R Unit test framework
R-Rcpp.x86_64 : Seamless R and C++ Integration
R-Rcpp-devel.x86_64 : Rcpp Development Files
R-Rcpp-examples.x86_64 : Rcpp Examples
R-core.x86_64 : The minimal R components necessary for a functional runtime
R-core-devel.x86_64 : Core files for development of R packages (no Java)
R-devel.x86_64 : Full R development environment metapackage
R-highlight.x86_64 : R Syntax Highlighter
R-inline.noarch : Functions to Inline C, C++, Fortran Function Calls from R
R-java.x86_64 : R with Fedora provided Java Runtime Environment
R-java-devel.x86_64 : Development package for use with Java enabled R components
R-littler.x86_64 : littler: R at the Command-Line via 'r'
R-littler-examples.x86_64 : R-littler Examples
R-qtl.x86_64 : Tools for analyzing QTL experiments
R-rlecuyer.x86_64 : R interface to RNG with multiple streams
R2spec.noarch : Python script to generate R spec file
RBTools.noarch : Tools for use with ReviewBoard
RabbIT.noarch : Proxy for a faster web
RackTables.noarch : A data-center asset management system
RdRand.x86_64 : Library for generating random numbers using the RdRand
RdRand-devel.x86_64 : Development files for the RdRand
ReviewBoard.noarch : Web-based code review tool      其中的 R.x86_64 就是要的结果,使用 yum info 可以查看 R 包的版本信息。
#查看 R 版本
yum info R.x86_64      但由于 R 软件更新较快,每半年就有一个较大的版本升级,且目前很多 R 包安装编译比较麻烦,这里我们推荐使用 bioconda 来安装 R 和相应的 R 包。
#利用 bioconda 安装 R
mamba install -y r-base      还可以编译安装R语言,之前介绍过
#下载
wget https://cloud.r-project.org/src/base/R-4/R-4.2.1.tar.gz
tar -zxvf R-4.2.1.tar.gz
cd R-4.2.1
#检测配置
./configure
#编译
make
make installmv /usr/bin/R /usr/bin/R.bak #将系统3.6.0的R备份
ln -s /share/Software/biosoft/R-4.2.1/bin/R /usr/bin/R #软连接新的4.2.1
# ll /usr/bin/R
lrwxrwxrwx 1 root root 37 Jul6 16:49 /usr/bin/R -> /share/Software/biosoft/R-4.2.1/bin/R      还可以使用rstudio官网上的rpm安装(推荐)export R_VERSION=4.2.1
curl -O https://cdn.rstudio.com/r/centos-7/pkgs/R-${R_VERSION}-1-1.x86_64.rpm
sudo yum install R-${R_VERSION}-1-1.x86_64.rpm
mv /usr/local/bin/R /usr/local/bin/R.bak
mv /usr/local/bin/Rscript /usr/local/bin/Rscript.bak
sudo ln -s /opt/R/${R_VERSION}/bin/R /usr/local/bin/R
sudo ln -s /opt/R/${R_VERSION}/bin/Rscript /usr/local/bin/Rscript
二、安装 Rstudio-server
      Rstudio 是 R 的集成开发环境,非常的好用,在服务器端安装 Rstudio-server 之后就可以通过浏览器访问 R,非常的方便。在配置 Rstudio-server 之前需要注意,首先需要安装 R 软件。另外使用免费版本的 rstudio-server 软件只能使用一个版本的 R 语言。且只能通过管理员安装和配置,个人用户无法配置 rstudio-server。
2.1 安装 rstudio-server
      https://www.rstudio.com/products/rstudio/download-server/
#安装rstudio-server
wget https://download2.rstudio.org/server/centos8/x86_64/rstudio-server-rhel-2021.09.2-382-x86_64.rpm
yum install rstudio-server-rhel-2021.09.2-382-x86_64.rpm

#检查是否安装成功
rstudio-server verify-installation
rstudio-server status      默认 rstudio-server 使用的是/usr/bin/下的 R,现在我要切换默认使用 bioconda 的 R。需要
      修改 rstudio-server 的配置文件。或者将 R 软连接到/usr/bin 下。
#修改rstudio-server配置,配置R版本的地址,前面我们软链接了,所有省去这一步
# vim /etc/rstudio/rserver.conf
# Server Configuration File
#rsession-which-r=/usr/local/bin/R2.2 开启 8787 端口
       CentOS7 系统采用新的防火墙策略,因此,安装完成 Rstudio 之后,还不能通过浏览器访问,需要进行防火墙设置才行,ubuntu 系统可以直接访问,下面的设置非常重要。
systemctl start rstudio-server.service
systemctl enable rstudio-server.service
systemctl status rstudio-server
firewall-cmd --permanent --add-port=8787/tcp
firewall-cmd --permanent --add-port=8787/udp
firewall-cmd --reload       完成以上步骤,rstudio-server 就配置完成了。利用浏览器输入 ip 地址:8787 就可以愉快的访问了。后面需要安装 R 包,都可以通过 bioconda 进行安装了,非常的方便。

2.3 配置 R 包
       可以在 R 中安装 R 包,我们需要注意下载的位置,安装到库的位置,大家都可以做下了解,以防文件混乱
       也可以使用 bioconda 进行配置,需要注意 R 包的名字在 bioconda中以 r-为前缀,Bioconductor 包的名字为 bioconductor-前缀。
.libPaths(c("/opt/R/4.2.1/lib/R/library"))#查看R包库位置
"/home/xhs/R/x86_64-pc-linux-gnu-library/4.1" "/opt/R/4.2.1/lib/R/library"
#上面有两个,我们想安装到第二个路径中
#先在命令行给到目录修改权限、doc为包的介绍html存放目录也需要权限
sudo chmod -R 777 /opt/R/4.2.1/lib/R/library
sudo chmod -R 777 /opt/R/4.2.1/lib/R/doc
#/home/xhs/Rpack/download创建,用于存放下载的R包
R中安装命令
install.packages('ggplot2',lib = "/opt/R/4.2.1/lib/R/library",destdir = '/home/xhs/Rpack/download')
页: [1]
查看完整版本: R 语言环境搭建