ONEKEY——安装R 4.4.2适用于-el7
# 一键安装脚本与环境准备
提示
R 语言作为数据分析与建模的主力工具,部署前建议提前准备好依赖环境,特别是在编译SPARK,常见依赖包缺失将直接导致编译失败。
环境要求 | 推荐值 | 补充说明 |
---|---|---|
操作系统 | CentOS 7/8 | 兼容部分 RHEL、Rocky Linux |
gcc/gfortran | 4.8+ | 推荐 devtoolset-7 或更高 |
磁盘空间 | 5GB+ | 源码解压+临时编译产物 |
内存 | 2GB+ | 推荐 4GB 起步 |
以下脚本默认拉取 TUNA 源进行 R 4.4.2 源码安装,支持大多数 CentOS 生产环境,无须手动处理依赖冲突。
#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author: JaneTTR
set -ex
echo "############## INSTALL R start #############"
# 定义变量
DOWNLOAD_DIR="/opt/modules" # 下载路径
URL="https://mirrors.tuna.tsinghua.edu.cn/CRAN/src/base/R-4/R-4.4.2.tar.gz"
TAR_FILE="${DOWNLOAD_DIR}/R-4.4.2.tar.gz" # 下载的完整路径
DIR_NAME="${DOWNLOAD_DIR}/R-4.4.2" # 解压后的目录
INSTALL_DIR="/usr/local/R-4.4.2" # 安装路径
# 创建下载目录(如果不存在)
if [ ! -d "$DOWNLOAD_DIR" ]; then
sudo mkdir -p "$DOWNLOAD_DIR"
sudo chown $(whoami):$(whoami) "$DOWNLOAD_DIR"
fi
# 检查R是否已经安装
if [ ! -d "$INSTALL_DIR" ]; then
# 更新系统并安装必要的依赖
sudo yum install -y gcc-gfortran \
readline-devel \
libicu-devel \
libXt-devel \
libX11-devel \
libpng-devel \
libjpeg-devel \
libtiff-devel \
cairo-devel \
pango-devel \
bzip2-devel \
xz-devel \
curl-devel \
freetype-devel \
zlib-devel \
pkgconfig \
texlive \
texlive-latex \
texlive-xetex \
texlive-collection-latex \
texlive-scheme-full \
tidy
# 下载tar.gz文件,如果文件不存在
if [ ! -f "$TAR_FILE" ]; then
curl -o $TAR_FILE $URL
else
echo "$TAR_FILE 已存在,跳过下载。"
fi
# 解压文件,如果目录不存在
if [ ! -d "$DIR_NAME" ]; then
tar -zxvf $TAR_FILE -C $DOWNLOAD_DIR
else
echo "$DIR_NAME 已存在,跳过解压缩。"
fi
# 进入解压后的目录并安装
cd $DIR_NAME
# 配置、编译和安装
./configure --prefix=$INSTALL_DIR
make
sudo make install
# 清理解压的文件(可选)
cd ..
rm -rf $DIR_NAME
else
echo "R 已安装在 $INSTALL_DIR,跳过编译。"
fi
# 检查系统路径是否已包含R安装路径,如果没有则添加到/etc/profile
if ! grep -q "R_HOME" /etc/profile; then
echo "export R_HOME=$INSTALL_DIR" | sudo tee -a /etc/profile
echo 'export PATH=$R_HOME/bin:$PATH' | sudo tee -a /etc/profile
else
echo "系统路径已包含R安装路径,跳过添加。"
fi
source /etc/profile
# 验证安装
R --version
echo "############## INSTALL R end #############"
echo "############## SETUP R_ENV start #############"
source /opt/rh/devtoolset-7/enable
# 安装所需的 R 包,处理依赖包问题
REQUIRED_PACKAGES=("knitr" "rmarkdown" "devtools" "e1071" "survival" "httr2" "gh" "htmlwidgets" "usethis" "pkgdown" "profvis" "roxygen2" "testthat")
CRAN_MIRRORS="c('https://mirrors.tuna.tsinghua.edu.cn/CRAN/', 'https://mirrors.ustc.edu.cn/CRAN/', 'https://mirrors.aliyun.com/CRAN/')"
for package in "${REQUIRED_PACKAGES[@]}"; do
Rscript -e "options(repos = $CRAN_MIRRORS); if (!requireNamespace('$package', quietly = TRUE)) tryCatch(install.packages('$package'), error = function(e) { cat('Error installing package:', '$package', '\n') })"
done
# 提示用户包安装完成
echo "所需的 R 包已安装完成。"
echo "############## SETUP R_ENV end #############"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 常见问题与最佳实践
注意
如遇依赖包无法自动拉取、编译过程 GCC 版本报错,建议参考本站 JDK/GCC/开发工具链安装文档,提前升级编译器环境。
- 依赖网络拉取失败:建议准备好离线依赖,或提前下载源码及 R 包本地安装。
- 内存不足:可通过
swap
临时扩容,但长期建议提升物理内存。 - 包缺失/安装慢:建议更换镜像源,或将包预先下载,减少线上拉取。
企业/集群场景下推荐统一规划 /opt/modules
及 /usr/local
目录,便于后续多版本维护与自动化升级。
# R 包批量安装及自定义
提示
脚本默认批量安装数据科学主流 R 包(如 knitr
、rmarkdown
等),如需定制,请直接修改 REQUIRED_PACKAGES
数组。