介绍
R 是一个流行的开源编程语言,专注于统计计算和图形。它被广泛用于开发统计软件和执行数据分析。R 易于扩展,社区以不断为特定研究领域添加用户生成的包而闻名,这使得它适用于许多领域。
在本教程中,我们将安装 R 并展示如何从官方 全面 R 档案网络 (CRAN)中添加包。
前提条件
要做到这一点,你需要一个 Ubuntu 16.04 服务器:
- 最少1GB的RAM
- 具有
sudo
特权的非根用户。 要了解如何设置此功能,请遵循我们的初始服务器设置指南(https://andsky.com/tech/tutorials/initial-server-setup-with-ubuntu-16-04)。
一旦这些先决条件已经到位,你就准备好开始了。
步骤 1 - 安装 R
R是一个快速移动的项目,最新的稳定版本并不总是从Ubuntu的存储库中可用,所以我们将首先添加由CRAN维护的外部存储库:
<$>[注] 注: CRAN 维护其网络中的存储库,但并非所有外部存储库都是可靠的。
1sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
1[secondary_label Output]
2E298A3A825C0D65DFD57CBB651716619E084DAB9
3gpg: requesting key E084DAB9 from hkp server keyserver.ubuntu.com
4gpg: key E084DAB9: public key "Michael Rutter <[email protected]>" imported
5gpg: Total number processed: 1
6gpg: imported: 1 (RSA: 1)
一旦我们在每个服务器的数据库中有信任密钥,我们就可以添加存储库。
1sudo add-apt-repository 'deb [arch=amd64,i386] https://cran.rstudio.com/bin/linux/ubuntu xenial/'
此后,我们需要运行更新
,以便从新的存储库中包含包宣言:
1sudo apt-get update
1[secondary_label Output]
2. . .
3Get:6 https://cran.rstudio.com/bin/linux/ubuntu xenial/ InRelease [3,590 B]
4Get:7 https://cran.rstudio.com/bin/linux/ubuntu xenial/ Packages [31.5 kB]
5. . .
如果上面的行出现在更新
命令的输出中,我们已经成功地添加了存储库,我们可以肯定我们不会意外地安装一个较旧的版本。
现在我们已经准备好安装R了:
1sudo apt-get install r-base
在此写作时,CRAN 的最新稳定版本为 3.3.1,当您启动 R. 由于我们计划为系统上的每个人安装示例包,我们将启动 R 作为根用户,以便图书馆自动向所有用户提供:
1sudo -i R
1[secondary_label Output]
2R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
3. . .
4Type 'demo()' for some demos, 'help()' for on-line help, or
5'help.start()' for an HTML browser interface to help.
6Type 'q()' to quit R.
7>
这证实了我们已经成功安装了R并进入了其交互式壳。
步骤 2 — 从 CRAN 安装 R 包
R的优势部分在于添加包的丰富性。为了演示目的,我们将安装txtplot,一个输出ASCII图表的库,包括scatterplot,线程图,密度图,acf和条形图:
1install.packages('txtplot')
作为安装过程的一部分,您将被允许选择要安装的镜子:
1[secondary_label Output]
2--- Please select a CRAN mirror for use in this session ---
3HTTPS CRAN mirror
4
51: 0-Cloud [https] 2: Algeria [https]
63: Australia (Melbourne) [https] 4: Australia (Perth) [https]
75: Austria [https] 6: Belgium (Ghent) [https]
87: Brazil (SP 1) [https] 8: Bulgaria [https]
99: Canada (MB) [https] 10: Chile [https]
1011: China (Beijing 4) [https] 12: Colombia (Cali) [https]
1113: Czech Republic [https] 14: Denmark [https]
1215: France (Lyon 1) [https] 16: France (Lyon 2) [https]
1317: France (Marseille) [https] 18: France (Paris 2) [https]
1419: Germany (Falkenstein) [https] 20: Germany (Münster) [https]
1521: Iceland [https] 22: Ireland [https]
1623: Italy (Padua) [https] 24: Japan (Tokyo) [https]
1725: Malaysia [https] 26: Mexico (Mexico City) [https]
1827: New Zealand [https] 28: Norway [https]
1929: Philippines [https] 30: Russia (Moscow) [https]
2031: Serbia [https] 32: Spain (A Coruña) [https]
2133: Spain (Madrid) [https] 34: Switzerland [https]
2235: Taiwan (Chungli) [https] 36: Turkey (Denizli) [https]
2337: UK (Bristol) [https] 38: UK (Cambridge) [https]
2439: UK (London 1) [https] 40: USA (CA 1) [https]
2541: USA (IA) [https] 42: USA (KS) [https]
2643: USA (MI 1) [https] 44: USA (TN) [https]
2745: USA (TX) [https] 46: USA (WA) [https]
2847: (HTTP mirrors)
29
30Selection: 1
我们已输入 1 for 0-Cloud,它将我们连接到RStudio提供的内容交付网络(CDN),以获得最接近地理位置的选项。这个镜子将被设置为默认为剩余的会话。
<$>[注] 注: 在镜子列表之前,以下输出显示了软件包正在安装的位置。
1[secondary_label Output]
2Installing package into ‘/usr/local/lib/R/site-library’
3(as ‘lib’ is unspecified)
4. . .
这个整个网站的路径是可用的,因为我们运行了R作为根,并且是正确的位置,使包可供所有用户使用。
安装完成后,我们可以加载txtplot:
1library('txtplot')
如果没有错误消息,图书馆已成功加载,现在让我们用一个示例来看到它在行动中,该示例显示了轴标签的基本插图功能。由R的数据集
包提供的示例数据包含了汽车的速度和需要停止的距离,基于20世纪20年代的数据:
1txtplot(cars[,1], cars[,2], xlab = "speed", ylab = "distance")
1[secondary_label Output]
2
3 +----+-----------+------------+-----------+-----------+--+
4 120 + * +
5 | |
6d 100 + * +
7i | * * |
8s 80 + * * +
9t | * * * * |
10a 60 + * * * * * +
11n | * * * * * |
12c 40 + * * * * * * * +
13e | * * * * * * * |
14 20 + * * * * * +
15 | * * * |
16 0 +----+-----------+------------+-----------+-----------+--+
17 5 10 15 20 25
18 speed
如果您有兴趣了解更多关于txtplot的信息,请使用帮助(txtplot)。
任何预编译的软件包都可以从 CRAN 安装使用 install.packages()
. 有关可用软件的更多信息,您可以在任何 镜子的 Packages 链接下找到由名称或发布日期组织的官方软件包列表。
结论
现在你已经成功安装了R,你可能对这本指南感兴趣 安装开源RStudio服务器,一个在远程Linux服务器上运行的R版本的接口,这将为你刚刚完成的基于服务器的部署带来一个IDE。