R环境安装+一键安装脚本
# 1、问题剖析
报错内容:
执行编译出现缺少
knitr
等环境。报错原因:
CentOS 7.9 默认未安装所需的 R 环境和相关依赖。
解决办法:
安装 R 语言,并安装相关依赖包。
How to do:
# 2、解决办法
# 2.1、通过 yum 安装
# 2.1.1 配置阿里云 EPEL 源
备份现有的 EPEL 源配置
sudo mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
sudo mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
1
2
2
下载并配置阿里云的 EPEL 源
sudo wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
1
# 2.1.2 安装 R
安装 R
sudo yum install -y R
1
验证安装
R --version
1
# 2.1.3 配置 R 镜像源
创建或编辑 .Rprofile
文件
在 R 的主目录下创建或编辑 .Rprofile
文件,以便每次启动 R 时自动使用阿里云的 CRAN 镜像。
echo 'options(repos = c(CRAN = "https://mirrors.aliyun.com/CRAN/"))' >> ~/.Rprofile
1
启动 R 控制台
R
1
验证镜像源配置
在 R 控制台中运行以下命令,确保镜像源配置正确:
getOption("repos")
1
输出应包含 "https://mirrors.aliyun.com/CRAN/"
。
# 2.1.4 安装 R 包
在 R 控制台中安装所需的 R 包
install.packages(c("knitr", "rmarkdown", "devtools", "e1071", "survival", "httr2", "gh", "htmlwidgets", "usethis", "pkgdown", "profvis", "roxygen2", "testthat"))
1
# 2.2、通过 tar 包安装
# 2.2.1 下载并解压 R 源代码包
下载 R 4.4.1 源代码包
首先,从指定的镜像仓库下载 R 4.4.1 的源代码包:
wget https://mirrors.ustc.edu.cn/CRAN/src/base/R-4/R-4.4.1.tar.gz
1
解压源代码包
下载完成后,解压该包:
tar -xzf R-4.4.1.tar.gz
1
# 2.2.2 安装 R
进入解压后的目录
cd R-4.4.1
1
安装依赖
在编译和安装 R 之前,需要安装一些必要的依赖包:
sudo yum install gcc gcc-c++ gcc-gfortran readline-devel libXt-devel bzip2-devel xz-devel pcre2-devel zlib-devel curl-devel libjpeg-devel libpng-devel cairo-devel pango-devel tcl-devel tk-devel
1
配置
配置 R 的编译环境:
./configure --enable-R-shlib --with-x
1
编译
开始编译 R:
make
1
安装
编译完成后,安装 R:
sudo make install
echo "export R_HOME=位置" | sudo tee -a /etc/profile
echo 'export PATH=$R_HOME/bin:$PATH' | sudo tee -a /etc/profile
source /etc/profile
1
2
3
4
2
3
4
# 2.2.3 验证安装
安装完成后,可以通过运行以下命令来验证 R 是否安装成功:
R --version
1
输出应显示 R 的版本信息,例如:
R version 4.4.1 (2023-06-01) -- "One Push"
1
# 2.2.4 配置 R 镜像源
为了加快 R 包的下载速度,可以配置 R 使用中国的 CRAN 镜像源。创建或编辑 .Rprofile
文件:
echo 'options(repos = c(CRAN = "https://mirrors.ustc.edu.cn/CRAN/"))' >> ~/.Rprofile
1
# 2.2.5 安装 R 包
启动 R 控制台
R
1
安装所需的 R 包
在 R 控制台中运行以下命令来安装所需的 R 包:
install.packages(c("knitr", "rmarkdown", "devtools", "e1071", "survival", "httr2", "gh", "htmlwidgets", "usethis", "pkgdown", "profvis", "roxygen2", "testthat"))
1
# 2.3 懒人脚本 - R
#!/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
set -e
echo "############## INSTALL R start #############"
# 定义变量
URL="https://mirrors.ustc.edu.cn/CRAN/src/base/R-4/R-4.4.1.tar.gz"
TAR_FILE="R-4.4.1.tar.gz"
DIR_NAME="R-4.4.1"
INSTALL_DIR="/usr/local/R-4.4.1"
# 检查 R 是否已经安装
if [ ! -d "$INSTALL_DIR" ]; then
# 更新系统并安装必要的依赖
sudo yum install -y gcc-gfortran readline-devel libXt-devel libX11-devel libpng-devel libjpeg-devel cairo-devel pango-devel bzip2-devel xz-devel curl-devel libicu-devel
# 下载 tar.gz 文件,如果文件不存在
if [ ! -f "$TAR_FILE" ]; then
wget $URL -O $TAR_FILE
else
echo "$TAR_FILE 已存在,跳过下载。"
fi
# 解压文件,如果目录不存在
if [ ! -d "$DIR_NAME" ]; then
tar -zxvf $TAR_FILE
else
echo "$DIR_NAME 已存在,跳过解压缩。"
fi
# 进入解压后的目录并安装
cd $DIR_NAME
# 配置、编译和安装
./configure --prefix=$INSTALL_DIR
make
sudo make install
# 清理下载的文件
cd ..
rm -rf $DIR_NAME
rm $TAR_FILE
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 的安装目录
R_HOME=$(Rscript -e "cat(R.home())")
# 确定 Rprofile.site 文件的位置
RPROFILE_SITE="$R_HOME/etc/Rprofile.site"
# 如果 Rprofile.site 文件不存在,则创建它
if [ ! -f "$RPROFILE_SITE" ]; then
echo "创建 $RPROFILE_SITE 文件。"
sudo touch "$RPROFILE_SITE"
fi
# 设置多个 CRAN 镜像源,阿里云放在首位
CRAN_MIRROR_SETTING='options(repos = c(
CRAN1 = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/",
CRAN2 = "https://mirrors.ustc.edu.cn/CRAN/",
CRAN3 = "https://mirrors.aliyun.com/CRAN/"
))'
# 检查 Rprofile.site 文件中是否已经包含 CRAN 镜像设置
if ! grep -q "mirrors.tuna.tsinghua.edu.cn" "$RPROFILE_SITE"; then
echo "添加 CRAN 镜像设置到 $RPROFILE_SITE 文件中。"
echo "$CRAN_MIRROR_SETTING" | sudo tee -a "$RPROFILE_SITE"
else
echo "CRAN 镜像设置已存在于 $RPROFILE_SITE 文件中。"
fi
# 安装所需的 R 包,处理依赖包问题
REQUIRED_PACKAGES=("knitr" "rmarkdown" "devtools" "e1071" "survival" "httr2" "gh" "htmlwidgets" "usethis" "pkgdown" "profvis" "roxygen2" "testthat")
for package in "${REQUIRED_PACKAGES[@]}"; do
Rscript -e "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
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
# 3、为 Spark 安装 R 包时出现 'evaluate'
、'httr2'
等包不存在时的解决办法
造成这一现象的原因是 CentOS 默认安装的 R 版本为 3.6.0。而在镜像仓库上,已经不存在适用于 3.6.0 版本的
evaluate
包,httr2
最低支持 4.0.0 版本。建议将 R 升级至 4.0 以上版本。以下是部分截图: