在本文中,我们将学习如何在Ubuntu 16.01上安装和配置R。“ R”是一种开放源代码编程语言,可以专门用于统计计算和执行分析数据。“ R”所在的社区非常了解用户在特定研究领域中生成的软件包。我们将使用CRAN(全面的“ R”存档网络)安装“ R”软件包。
我们需要在Linux计算机上安装Ubuntu 16.04服务器,且RAM至少为1 GB。
在计算机上具有Sudo权限的非root用户。
由于“ 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. >
由于R有很多软件包或附加组件,因此在演示中的此处,我们将安装txtplot,这是一个ASCII图形软件包,还包括scatterplot,下面是从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 >
在上面的文章中,我们已经安装了R,它使用CRAN存储库,这是RStudio Server的开源软件,因为我们已经完成了服务器端的安装并使用图形测试了一些示例日期。