如何在 CentOS 8 上安装 R
R 是一种开源编程语言和专门从事统计计算和图形表示的免费环境。它由 R 统计计算基金会支持,主要由统计学家和数据挖掘人员用于开发统计软件和执行数据分析。
本文介绍如何在 CentOS 8 上安装R。
先决条件
在继续本教程之前,请确保满足以下先决条件:
- 您的系统至少有 1G 内存 。否则,请创建一个交换文件。
- 您以具有 sudo 特权的用户身份登录。
在 Centos 上安装R
R 软件包不包含在 CentOS 8 核心存储库中。我们将从 EPEL 存储库安装 R :
要在 CentOS 8 上安装 R,请执行以下步骤:
-
启用 EPEL 和 PowerTools 存储库:
sudo dnf install epel-release sudo dnf config-manager --set-enabled PowerTools
-
通过键入以下命令安装 R:
sudo yum install R
R 安转包里包含了所有必需的 R 组件。
-
通过打印 R 版本来验证安装:
R --version
在撰写本文时,R 的最新稳定版本是版本 3.6.2 :
R version 3.6.2 (2019-12-12) -- " Dark and Stormy Night " Copyright (C) 2019 The R Foundation for Statistical Computing Platform: x86_64-redhat-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under the terms of the GNU General Public License versions 2 or 3. For more information about these matters see https://www.gnu.org/licenses/.
-
安装常见的 R 软件包所使用的库和工具:
sudo yum install make gcc gcc-c++ libcurl-devel libxml2-devel openssl-devel texlive-*
现在,您已经成功的在 CentOS 系统上安装了 R,并且可以开始使用它了。
从 CRAN 安装 R 软件包
R 如此受欢迎的主要原因之一是可以通过综合 R 存档网络 (CRAN) 获得大量软件包。
如果 R
安装二进制文件是 root 或 sudo 启动的,则程序包将全局安装并可供所有系统用户使用。要为您的用户设置个人库,请以常规用户身份调用二进制文件。
举例来说,我们将安装一个名为 stringr
的包,该包提供常见字符串操作的快速正确的实现。
首先以根用户身份打开 R 控制台:
sudo -i R
R version 3.6.2 (2019-12-12) -- "Dark and Stormy Night"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-redhat-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.
Natural language support but running in an English locale
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.
>
以下命令在 R 控制台中执行。
安装 stringr
软件包:
install.packages("stringr")
系统将要求您选择一个 CRAN 镜像:
Installing package into ‘/usr/lib64/R/library’
(as ‘lib’ is unspecified)
--- Please select a CRAN mirror for use in this session ---
Secure CRAN mirrors
选择离您最近的镜像。
安装将花费一些时间,一旦完成,请键入以下内容加载库:
library(stringr)
接下来,创建一个简单的名为 tutorial
的字符变量:
tutorial <- c("How", "to", "Install", "R", "on", "CentOS", "8")
运行以下函数,该函数将打印每个字符串的长度:
str_length(tutorial)
[1] 3 2 7 1 2 6 1
您可以在CRAN软件包页面中找到更多 R 软件包,并使用 install.packages()
进行安装。
结论
我们已经向您展示了在 CentOS 8 安装 R 的步骤和如何安装 R 软件包。