vlambda博客
学习文章列表

服务器centos8.3安装R和Rstudio

1. install R

sudo yum -y update
sudo dnf install epel-release
sudo dnf install 'dnf-command(config-manager)'
sudo dnf config-manager --set-enabled PowerTools
sudo dnf install R

2. install Rstudio-server

我是通过浏览器下载的,网速原因,服务器下载不动

Download RStudio Server for Red Hat/CentOS - RStudio

https://rstudio.com/products/rstudio/download-server/redhat-centos/

wget https://download2.rstudio.org/server/centos8/x86_64/rstudio-server-rhel-1.4.1106-x86_64.rpm
sudo yum install rstudio-server-rhel-1.4.1106-x86_64.rpm

假如是需要升级RStudio-server,比如我原先的是1.1.456最新的是1.4.1106, 需要先暂停当前的服务并卸载

/usr/sbin/rstudio-server stop
yum remove rstudio-server

之后才是安装

sudo yum install rstudio-server-rhel-1.4.1106-x86_64.rpm

安装完成之后,我们可以通过修改/etc/rstudio/rserver.conf更改端口和R所在路径

www-port=8787 # 端口, 默认8787,在这里可以修改
www-address=0.0.0.0
rsession-which-r=/usr/bin/R # 安装R的路径

修改完成之后,用rstudio-server restart重启服务,没有任何信息就表示安装成功了,但是我看到的是没有问题的,但是在浏览器里输入IP:8787端口时总是提示错误,后来在网上找了一堆的的攻略,最终还是没有起作用。无奈之下,我删除了R和Rstudio,重新安装还是同样的错误。在这里需要注意的一点是,第二次安装后自动调用的/etc/rstudio/rserver.conf是缓存中的文件,若是缓存中的这个文件有问题,R还是会调用的,因此要清理缓存和删除的/etc/rstudio/rserver.conf

image.png

服务器centos8.3安装R和Rstudio

image

清理缓存

yum install nscd-2.28-127.el8.x86_64 
nscd -i hostssystemctl status nscd
#nscd.service - Name Service Cache Daemon
#Loaded: loaded (/usr/lib/systemd/system/nscd.service; disabled; vendor preset: disabled)
#Active: inactive (dead)

当然还可以用rstudio-server verify-installation来验证下,如果没有任何输出信息就表示安装成功,

但是我报错了,提示Address already in use端口被占用。

22 Mar 2021 10:35:33 [rserver] ERROR system error 98 (Address already in use); OCCURRED AT rstudio::core::Error rstudio::core::http::initTcpIpAcceptor(rstudio::core::http::SocketAcceptorService<rstudio_boost::asio::ip::tcp>&, const string&, const string&) src/cpp/server/ServerInit.cpp:103; LOGGED FROM: int main(int, char* const*) src/cpp/server/ServerMain.cpp:683

查找占用的端口,然后杀掉进程,重新确认是否安装。

netstat -lnp|grep 8787
#tcp 0 0 0.0.0.0:8787 0.0.0.0:* LISTEN 236101/sshd
#tcp6 0 0 :::8787 :::* LISTEN 236101/sshd

kill -9 236101

netstat -lnp|grep 8787

firewall-cmd --zone=public --list-ports
#8787/tcp 22/tcp

systemctl restart firewalld

sudo rstudio-server start

sudo rstudio-server status
# rstudio-server.service - RStudio Server
# Loaded: loaded (/usr/lib/systemd/system/rstudio-server.service; enabled; vendor preset: disable>
# Active: active (running) since Mon 2021-03-22 18:39:32 CST; 6s ago
# Process: 286233 ExecStart=/usr/lib/rstudio-server/bin/rserver (code=exited, status=0/SUCCESS)
# Main PID: 286234 (rserver)
# Tasks: 3 (limit: 3355442)
# Memory: 18.6M
# CGroup: /system.slice/rstudio-server.service
└─286234 /usr/lib/rstudio-server/bin/rserver

#Mar 22 18:39:32 localhost.localdomain systemd[1]: Starting RStudio Server...

#[10]+ Stopped sudo rstudio-server status

sudo rstudio-server verify-installation
#Server is running and must be stopped before running verify-installation

最直接的方法就是直接访问"IP:端口",能够出现RStudio的登陆界面就意味着安装成功了。

出现下面这条信息,有人说存在问题,但是我成功使用。

Server is running and must be stopped before running verify-installation

服务器关机重启后,还是8787端口被占用,因此在上面修改端口处修改,然后没有再出现问题。

参考链接:https://www.jianshu.com/p/213677311acd

3. 安装R包

代码来自曾老师,生信技能树!

options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
if(! require("devtools")) install.packages("devtools")
if(! require("reshape2")) install.packages("reshape2")
if(! require("ggplot2")) install.packages("ggplot2")
if(! require("pheatmap")) install.packages("pheatmap")
if(! require("ggfortify")) install.packages("ggfortify")
if(! require("stringr")) install.packages("stringr")
if(! require("survival")) install.packages("survival")
if(! require("survminer")) install.packages("survminer")
if(! require("lars")) install.packages("lars")
if(! require("glmnet")) install.packages("glmnet")
if(! require("timeROC")) install.packages("timeROC")
if(! require("ggpubr")) install.packages("ggpubr")
if(! require("randomForest")) install.packages("randomForest")
if(! require("ROCR")) install.packages("ROCR")
if(! require("Hmisc")) install.packages("Hmisc")
if(! require("caret")) install.packages("caret")
# if(! require("genefilter")) install.packages("genefilter")
if(! require("ggstatsplot")) install.packages("ggstatsplot")
if(! require("Seurat")) install.packages("Seurat")
if(! require("SeuratObject")) install.packages("SeuratObject")

### 下面的包是为了临床三线表
if(! require("tableone")) install.packages("tableone")
## 网络不好,就不要安装了。
## 而且Windows电脑安装 rJava 也经常是需要人指导的。
# https://github.com/rstudio/rstudio/issues/2254
if(! require("rJava")) install.packages("rJava")
if(require('rJava')){
# https://cran.r-project.org/src/contrib/Archive/ReporteRs/
if(! require("ReporteRs")) install.packages("ReporteRs")
devtools::install_github('davidgohel/ReporteRsjars')
devtools::install_github('davidgohel/ReporteRs')
}
library(devtools)
source("http://bioconductor.org/biocLite.R")
## 如果你的网络实在是太差,试试看:
# install.packages("BiocInstaller",repos="http://bioconductor.org/packages/3.7/bioc")
## 很可惜你在中国大陆,不得不承受这个痛苦。
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
library('BiocInstaller')
if(! require('edgeR')){
BiocManager::install(c('airway','DESeq2','edgeR','limma'))
}
if(! require("CLL")) BiocManager::install("CLL")
if(! require("org.Hs.eg.db")) BiocManager::installe('org.Hs.eg.db')
library(BiocInstaller)
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
if(! require("maftools")) BiocManager::install("maftools")
if(! require("RTCGA")) BiocManager::install("RTCGA")
if(! require("RTCGA.clinical")) BiocManager::install("RTCGA.clinical")
# https://bioconductor.org/packages/3.6/data/experiment/src/contrib/RTCGA.clinical_20151101.8.0.tar.gzn
if(! require("RTCGA.miRNASeq")) BiocManager::install("RTCGA.miRNASeq")
if(! require("maftools")) BiocManager::install("maftools")
if(! require("genefilter")) BiocManager::install("genefilter")

image.png

但是发现丢了库文件,之前也遇到过这个问题,原因是

image.png

在rstudio下,安装包时,默认的库路径是/usr/lib64/R/library",而R软件本身的默认路径是/usr/share/R/library。

其中“/usr/share/R/library”这一路径就是所有用户共享library的地方,因此想要让共享一个库只要将包安装到这一目录就可以了。

但是/usr/share/R/library这一路径的组权限默认是“staff”的,因此想要安装到这个目录还有赋予权限才行。

将library的组修改为mygroup这一分组,这样mygroup里的成员就可以把包安装到library目录里了。

但是/usr/local/lib64/R/library这一路径的组权限默认是”staff”的,因此想要安装到这个目录还有赋予权限才行。

将library的组修改为mygroup这一分组,这样mygroup里的成员就可以把包安装到library目录里了。

sudo chgrp mygroup /usr/local/lib64/R/library

参考 原文链接:**https://blog.csdn.net/wa2003/article/details/46324135

在linux下查看包的路径及权限

sudo ls -all /usr/share/R/library
sudo ls -all /usr/lib64/R/library

4. Rstudio-server常用操作

sudo rstudio-server verify-installation 
查看运行中R进程
rstudio-server active-sessions
指定PID,停止运行中的R进程
rstudio-server suspend-session <pid>
停止所有运行中的R进程
rstudio-server suspend-all
强制停止运行中的R进程,优先级最高,立刻执行
rstudio-server force-suspend-session
rstudio-server force-suspend-all
RStudio Server临时下线,不允许web访问,并给用户友好提示
rstudio-server offline
RStudio Server临时上线
rstudio-server online

5. CentOS 8 开启防火墙端口

主要是开放Rstudio浏览器端口

查看防火墙某个端口是否开放
firewall-cmd --query-port=3306/tcp
开放防火墙端口3306
firewall-cmd --zone=public --add-port=3306/tcp --permanent
注意:开放端口后要重启防火墙生效
重启防火墙
systemctl restart firewalld
关闭防火墙端口
firewall-cmd --remove-port=3306/tcp --permanent
查看防火墙状态
systemctl status firewalld
关闭防火墙
systemctl stop firewalld
打开防火墙
systemctl start firewalld
开放一段端口
firewall-cmd --zone=public --add-port=40000-45000/tcp --permanent
查看开放的端口列表
firewall-cmd --zone=public --list-ports
查看被监听(Listen)的端口
netstat -lntp
检查端口被哪个进程占用
netstat -lnp|grep 3306

参考原文链接:https://blog.csdn.net/qq_32656561/article/details/105619911

6. 安装一些常用动态库

为下一步安装R包做准备

sudo yum install geos geos-devel
yum install gdal.x86_64 gdal-devel.x86_64 gdal-libs.x86_64
yum install proj.x86_64 proj-devel.x86_64
yum install proj-epsg.x86_64 proj-nad.x86_64
yum install http://repo.okay.com.mx/centos/8/x86_64/release/v8-6.7.17-8.el8.x86_64.rpm
sudo yum install nodejs-devel
yum list |grep lib
sudo yum install udunits2-devel

yum install libcurl.x86_64 libcurl-devel.x86_64
yum install libxml2.x86_64 libxml2-devel.x86_64
sudo yum install libjpeg-turbo-devel #jpeg
sudo yum install mpfr-devel
sudo yum install v8-devel.x86_64#装了好久才找到
sudo yum install https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/g/gdal-3.0.4-5.el8.x86_64.rpm
sudo rpm -i libdap-3.19.1-2.el8.x86_64.rpm
sudo rpm -i libdap-devel-3.19.1-2.el8.x86_64.rpm
sudo rpm -i libaec-1.0.2-3.el8.x86_64.rpm
sudo yum install netcdf.x86_64
sudo yum install gdal.x86_64
sudo yum install gdal.x86_64 gdal-devel.x86_64 gdal-libs.x86_64
sudo yum install proj.x86_64 proj-devel.x86_64
sudo yum install proj-epsg.x86_64 proj-nad.x86_64
sudo yum install proj-devel-4.8.0-4.1.x86_64.rpm
sudo yum install sqlite-devel
sudo yum install sqlite.x86_64
sudo yum install geos geos-devel
sudo yum install http://mirror.centos.org/centos/8/AppStream/x86_64/os/Packages/gtk2-devel-2.24.32-4.el8.x86_64.rpm
sudo yum install libXt-devel
sudo yum install gdal
sudo yum install https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/g/gdal-devel-3.0.4-5.el8.x86_64.rpm
sudo yum install https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/i/ImageMagick-6.9.10.86-1.el8.x86_64.rpm
sudo yum install https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages