如何在 Ubuntu 16.04 上安装和配置“R”


在本文中,我们将学习如何在 Ubuntu 16.01 上安装和配置 R。“R”是一种开源编程语言,可专门用于统计计算和执行分析数据。其中,“R”因用户在特定研究领域生成的软件包而广为人知。我们将使用 CRAN(综合“R”归档网络)安装“R”软件包。

先决条件

  • 我们需要一台安装了 Ubuntu 16.04 服务器的 Linux 机器,至少需要 1 GB 的 RAM。

  • 具有 Sudo 权限的非 root 用户。

将 CRAN 存储库添加到机器并安装 R 软件包

由于“R”是最快发展的软件包,因此我们无法从 Ubuntu 存储库中获取稳定版本,因此我们需要将 CRAN 存储库添加到我们的本地机器。

$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
Output:
Executing: /tmp/tmp.YZv0REiwBU/gpg.1.sh --
keyserverkeyserver.ubuntu.com
--recv-key--
E298A3A825C0D65DFD57CBB651716619E084DAB9
gpg: requesting key E084DAB9 from hkp server keyserver.ubuntu.com
gpg: key E084DAB9: public key "Michael Rutter <[email protected]>" imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)

一旦受信任的密钥安装到服务器数据库中,我们就可以使用以下命令将存储库添加到机器。

$ sudo add-apt-repository 'deb [arch=amd64,i386] https://cran.rstudio.com/bin/linux/ubuntu xenial/'

添加存储库后,我们将使用以下命令更新机器

$ sudo apt-get update
Output:
Hit:1 http://in.archive.ubuntu.com/ubuntu xenial InRelease
Hit:2 http://deb.kamailio.org/kamailio jessie InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease
Hit:4 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease
Hit:5 https://deb.nodesource.com/node_6.x xenial InRelease
Hit:6 http://security.ubuntu.com/ubuntu xenial-security InRelease
Hit:7 https://cran.rstudio.com/bin/linux/ubuntu xenial/ InRelease
Reading package lists... Done
W: http://deb.kamailio.org/kamailio/dists/jessie/InRelease: Signature by key E79ACECB87D8DCD23A20AD2FFB40D3E6508EA4C8 uses weak digest algorithm (SHA1)Once the update is done and update the local machine we will now install the r-base package from the CRAN repository.

以下是安装 R 软件包的命令。

$ sudo apt-get install r-base
Output:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-headers-4.4.0-21 linux-headers-4.4.0-21-generic linux-image-4.4.0-21-generic
linux-image-extra-4.4.0-21-generic
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
bzip2-doc cdbs cpp-5 dh-translations fontconfig fontconfig-config fonts-dejavu-core g++-5 gcc-5
gcc-5-base gfortran gfortran-5 icu-devtools intltool libasan2 libatomic1 libauthen-sasl-perl
libblas-common libblas-dev libblas3 libbz2-dev libcairo2 libcc1-0 libcilkrts5 libcurl3 libdatrie1
libdrm-amdgpu1 libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libelf1 libencode-locale-perl
libfile-basedir-perl libfile-desktopentry-perl libfile-listing-perl libfile-mimeinfo-perl
libfont-afm-perl libfontconfig1 libfontenc1 libgcc-5-dev libgfortran-5-dev libgfortran3 libgl1-mesa-dri
libgl1-mesa-glx libglapi-mesa libgomp1 libgraphite2-3 libharfbuzz0b libhtml-form-perl
libhtml-format-perl libhtml-parser-perl libhtml-tagset-perl libhtml-tree-perl libhttp-cookies-perl
libhttp-daemon-perl libhttp-date-perl libhttp-message-perl libhttp-negotiate-perl libice6 libicu-dev
libio-html-perl libio-socket-ssl-perl libipc-system-simple-perl libitm1 libjbig0 libjpeg-dev
libjpeg-turbo8 libjpeg-turbo8-dev libjpeg8 libjpeg8-dev liblapack-dev liblapack3 libllvm3.8 liblsan0
liblwp-mediatypes-perl liblwp-protocol-https-perl liblzma-dev libmailtools-perl libmpx0 libnet-dbus-perl
libnet-http-perl libnet-smtp-ssl-perl libnet-ssleay-perl libpango-1.0-0 libpangocairo-1.0-0
…
…
…
Creating config file /etc/R/Renviron with new version
Setting up r-cran-boot (1.3-18-1cran1xenial0) ...
Setting up r-cran-cluster (2.0.5-1xenial0) ...
Setting up r-cran-foreign (0.8.67-1xenial0) ...
Setting up r-cran-mass (7.3-45-1xenial0) ...
Setting up r-cran-kernsmooth (2.23-15-2xenial0) ...
Setting up r-cran-lattice (0.20-34-1xenial0) ...
Setting up r-cran-nlme (3.1.128-2xenial0) ...
Setting up r-cran-matrix (1.2-7.1-1xenial0) ...
Setting up r-cran-mgcv (1.8-15-1cran1xenial0) ...
Setting up r-cran-survival (2.39-4-2xenial0) ...
Setting up r-cran-rpart (4.1-10-1) ...
Setting up r-cran-class (7.3-14-1xenial0) ...
Setting up r-cran-nnet (7.3-12-1xenial0) ...
Setting up r-cran-spatial (7.3-11-1xenial0) ...
Setting up r-cran-codetools (0.2-15-1cran1xenial0) ...
Setting up r-recommended (3.3.2-1xenial0) ...
Setting up r-base (3.3.2-1xenial0) ...
Setting up liblzma-dev:amd64 (5.1.1alpha+20120614-2ubuntu2) ...
Setting up r-doc-html (3.3.2-1xenial0) ...
Setting up x11-utils (7.7+3) ...
Setting up x11-xserver-utils (7.7+7) ...
Setting up libauthen-sasl-perl (2.1600-1) ...
Setting up r-base-html (3.3.2-1xenial0) ...
Setting up liblwp-protocol-https-perl (6.06-2) ...
Setting up libwww-perl (6.15-1) ...
Setting up libxml-parser-perl (2.44-1build1) ...
Setting up intltool (0.51.0-2) ...
Setting up dh-translations (129) ...
Setting up cdbs (0.4.130ubuntu2) ...
Setting up libxml-twig-perl (1:3.48-1) ...
Setting up libnet-dbus-perl (1.1.0-3build1) ...
Setting up r-base-dev (3.3.2-1xenial0) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for systemd (229-4ubuntu10) ...
Processing triggers for ureadahead (0.100.0-19) ...

安装软件包后,我们可以使用以下命令验证软件包

$ sudo -i R
Output:
R version 3.3.2 (2016-10-31) -- "Sincere Pumpkin Patch"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-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.
>

从 CRAN 存储库安装其他 R 软件包

由于 R 具有许多软件包或附加组件,此处演示将安装 txtplot,这是一个 ASCII 图形软件包,其中还包括散点图,以下是需要从 R 控制台运行的命令。

> install.packages('txtplot')
Output:
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
--- Please select a CRAN mirror for use in this session ---
HTTPS CRAN mirror
1: 0-Cloud [https]                           2: Algeria [https]
3: Australia (Melbourne) [https]             4: Australia (Perth) [https]
5: Austria [https]                           6: Belgium (Ghent) [https]
7: Brazil (RJ) [https]                       8: Brazil (SP 1) [https]
9: Bulgaria [https]                         10: Canada (MB) [https]
11: Chile [https]                           12: China (Beijing) [https]
13: China (Hefei) [https]                   14: China (Lanzhou) [https]
15: Colombia (Cali) [https]                 16: Czech Republic [https]
17: Denmark [https]                         18: France (Lyon 1) [https]
19: France (Lyon 2) [https]                 20: France (Marseille) [https]
21: France (Montpellier) [https]            22: France (Paris 2) [https]
23: Germany (Falkenstein) [https]           24: Germany (Münster) [https]
25: Iceland [https]                         26: India [https]
27: Ireland [https]                         28: Italy (Padua) [https]
29: Japan (Tokyo) [https]                   30: Malaysia [https]
31: Mexico (Mexico City) [https]            32: New Zealand [https]
33: Norway [https]                          34: Philippines [https]
35: Russia (Moscow) [https]                 36: Serbia [https]
37: Spain (A Coruña) [https]                38: Spain (Madrid) [https]
39: Switzerland [https]                     40: Taiwan (Chungli) [https]
41: Turkey (Denizli) [https]                42: UK (Bristol) [https]
43: UK (Cambridge) [https]                  44: UK (London 1) [https]
45: USA (CA 1) [https]                      46: USA (IA) [https]
47: USA (IN) [https]                        48: USA (KS) [https]
49: USA (MI 1) [https]                      50: USA (TN) [https]
51: USA (TX) [https]                        52: USA (WA) [https]
53: (HTTP mirrors)
Selection:1
trying URL 'https://cloud.r-project.org/src/contrib/txtplot_1.0-3.tar.gz'
Content type 'application/x-gzip' length 6152 bytes
==================================================
downloaded 6152 bytes
* installing *source* package ‘txtplot’ ...
** package ‘txtplot’ successfully unpacked and MD5 sums checked
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (txtplot)
The downloaded source packages are in
‘/tmp/RtmpnlpfWd/downloaded_packages’

安装附加组件后,我们可以加载库并使用一些示例软件包测试库。

> library('txtplot')

加载软件包后,我们将测试样本,其中样本数据集软件包包含汽车的速度以及基于 1920 年代数据的基础上制动停止的距离。

>txtplot(cars[,1], cars[,2], xlab = "speed", ylab = "distance")
Output:
+----+-----------+------------+-----------+-----------+--+
120                                                  + * +
|                                                        |
d 100                                   +              * +
i | * *                                                  |
s 80                                + * *                +
t | * * * *                                              |
a 60 + * * * * * +
n | * * * * *                                            |
c 40 + * * * * * * * +
e | * * * * * * *                                        |
20 + * * * * * +
| * * *                                                  |
0 +----+-----------+------------+-----------+---------+--+
5 10 15 20 25
speed
>

在以上文章中,我们安装了使用 CRAN 存储库的 R,它是来自 RStudio Server 的开源软件,因为我们已经完成了服务器端安装并使用图形测试了一些示例日期。

更新于: 2020 年 1 月 20 日

380 次查看

开启您的 职业生涯

通过完成课程获得认证

开始
广告