如何在 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 <marutter@gmail.com>" 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 的開源軟體,因為我們已經完成了伺服器端安裝並使用圖形測試了一些示例日期。
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP