介绍
R是一个开源编程语言,广泛用于开发统计软件,执行数据分析和可视化,R为特定研究领域提供了许多用户生成的包,这使得它适用于许多领域。
在本教程中,我们将安装 R 并展示如何从官方 全面 R 档案网络 (CRAN)中添加包。
前提条件
要跟随本教程,您将需要具有以下功能的 Debian 10 服务器:
- 至少 1GB RAM
具有
sudo
特权的非根用户
要了解如何实现此设置,请遵循我们的 Debian 10初始服务器设置指南。
一旦这些先决条件到位,你就准备好开始了。
步骤1 - 安装依赖性
由于 R 是一个快速移动的项目,最新的稳定版本并不总是从 Debian 存储库中可用,所以我们需要添加由 CRAN 维护的外部存储库。
要执行管理和下载证书的网络操作,我们需要安装dirmngr
,以便我们可以添加外部存储库。
1sudo apt install dirmngr --install-recommends
要向 Debian 添加 PPA 引用,我们需要使用add-apt-repository
命令. 对于可能无法使用此命令的安装,您可以通过安装software-properties-common
来将此工具添加到您的系统中:
1sudo apt install software-properties-common
最后,为了确保我们对安全协议有HTTPS支持,我们将安装以下工具:
1sudo apt install apt-transport-https
有了这些依赖性,我们已经准备好安装R。
步骤 2 - 安装 R
对于最新的 R 版本,我们将从 CRAN 存储库中安装。
<$>[注] 注: CRAN 维护其网络中的存储库,但并非所有外部存储库都是可靠的。
让我们先添加相应的 GPG 密钥。
1sudo apt-key adv --keyserver keys.gnupg.net --recv-key 'E19F5F87128899B192B1A2C2AD5F960A256A04AF'
当我们运行命令时,我们将收到以下输出:
1[secondary_label Output]
2Executing: /tmp/apt-key-gpghome.y6W4E0Gtfp/gpg.1.sh --keyserver keys.gnupg.net --recv-key E19F5F87128899B192B1A2C2AD5F960A256A04AF
3gpg: key AD5F960A256A04AF: 4 signatures not checked due to missing keys
4gpg: key AD5F960A256A04AF: public key "Johannes Ranke (Wissenschaftlicher Berater) <[email protected]>" imported
5gpg: Total number processed: 1
6gpg: imported: 1
一旦我们有了值得信赖的密钥,我们就可以添加存储库。 请注意,如果你不使用 Debian 10 (Buster),你可以查看每个版本的名称(支持的 R Project Debian 分支)。
1sudo add-apt-repository 'deb http://cloud.r-project.org/bin/linux/debian buster-cran35/'
现在,我们需要在此之后运行更新
,以便从新存储库中包含包宣言。
1sudo apt update
一旦完成运行,你被返回你的提示,我们准备用以下命令来安装R。
1sudo apt install r-base
如果被提示确认安装,请按y
继续。
截至写作时,CRAN 的最新稳定版本为 3.5.2,当您启动 R 时显示。
由于我们计划为系统上的每个用户安装一个示例包,我们将以 root 启动 R,以便图书馆自动向所有用户开放。
1sudo -i R
1[secondary_label Output]
2
3R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
4Copyright (C) 2018 The R Foundation for Statistical Computing
5Platform: x86_64-pc-linux-gnu (64-bit)
6
7R is free software and comes with ABSOLUTELY NO WARRANTY.
8You are welcome to redistribute it under certain conditions.
9Type 'license()' or 'licence()' for distribution details.
10
11 Natural language support but running in an English locale
12
13R is a collaborative project with many contributors.
14Type 'contributors()' for more information and
15'citation()' on how to cite R or R packages in publications.
16
17Type 'demo()' for some demos, 'help()' for on-line help, or
18'help.start()' for an HTML browser interface to help.
19Type 'q()' to quit R.
20
21>
这证实了我们已经成功安装了R并进入了其交互式壳。
步骤 3 – 从 CRAN 安装 R 包
R的优势部分在于其可用的丰富的附加包。为了演示目的,我们将安装 txtplot
,一个输出 ASCII 图表的库,其中包括 scatterplot,线程图,密度图,acf 和条形图表:
1install.packages('txtplot')
<$>[注] 注: 以下输出显示将安装该包的位置。
1[secondary_label Output]
2...
3Installing package into ‘/usr/local/lib/R/site-library’
4(as ‘lib’ is unspecified)
5. . .
这个全站路径是可用的,因为我们运行了R作为根,这是使包可供所有用户使用的正确位置。
安装完成后,我们可以加载txtplot:
1library('txtplot')
如果没有错误消息,图书馆已成功加载,现在让我们用一个示例来示范用轴标签的基本拼图功能。由R的数据集
包提供的示例数据包含(基于20世纪20年代的数据,汽车的速度和停止所需的距离):
1txtplot(cars[,1], cars[,2], xlab = 'speed', ylab = 'distance')
1[secondary_label Output]
2 +----+-----------+------------+-----------+-----------+--+
3 120 + * +
4 | |
5d 100 + * +
6i | * * |
7s 80 + * * +
8t | * * * * |
9a 60 + * * * * * +
10n | * * * * * |
11c 40 + * * * * * * * +
12e | * * * * * * * |
13 20 + * * * * * +
14 | * * * |
15 0 +----+-----------+------------+-----------+-----------+--+
16 5 10 15 20 25
17 speed
如果您有兴趣了解更多关于txtplot的信息,请从R翻译器中使用帮助(txtplot)
。
任何预编译的软件包都可以从 CRAN 安装使用 install.packages()
. 有关可用软件的更多信息,您可以通过 Available CRAN Packages By Name list找到由名称组成的官方软件包列表。
要退出 R,您可以键入 q()
. 除非您想要保存工作区图像,否则您可以按 n
。
结论
随着 R 成功安装在您的服务器上,您可能对 安装 RStudio 服务器的本指南感兴趣,以便将 IDE 带到您刚刚完成的基于服务器的部署中。
有关如何通过利用不同的工具来安装 R 包的更多信息,您可以阅读如何直接从 GitHub、BitBucket 或其他位置安装(https://andsky.com/tech/tutorials/how-to-install-r-packages-using-devtools-on-ubuntu-18-04)。