A previous version of this tutorial was written by Melissa Anderson.
本教程的先前版本由Melissa Anderson编写。
R is an open-source programming language that specializes in statistical computing and graphics. Supported by the R Foundation for Statistical Computing, it is widely used for developing statistical software and performing data analysis. An increasingly popular and extensible language with an active community, R offers many user-generated packages for specific areas of study, which makes it applicable to many fields.
R是一种开源编程语言,专门研究统计计算和图形。 在R统计计算基金会的支持下,它广泛用于开发统计软件和执行数据分析。 R具有活跃的社区,是一种日益流行和可扩展的语言,它为特定的研究领域提供了许多用户生成的软件包,这使其可应用于许多领域。
In this tutorial, we will install R and show how to add packages from the official Comprehensive R Archive Network (CRAN).
在本教程中,我们将安装R并演示如何从官方的Integrated R存档网络(CRAN)添加软件包。
To follow along with this tutorial, you will need an Ubuntu 18.04 server with:
要继续学习本教程,您将需要具有以下功能的Ubuntu 18.04服务器:
at least 1GB of RAM
至少 1GB的RAM
a non-root user with sudo
privileges
具有sudo
特权的非root用户
To learn how to achieve this setup, follow our manual initial server setup guide or run our automated script.
要了解如何实现此设置,请遵循我们的手动初始服务器设置指南或运行我们的自动化脚本 。
Once these prerequisites are in place, you’re ready to begin.
一旦满足这些先决条件,就可以开始了。
Because R is a fast-moving project, the latest stable version isn’t always available from Ubuntu’s repositories, so we’ll start by adding the external repository maintained by CRAN.
因为R是一个快速发展的项目,所以Ubuntu的存储库中并不总是提供最新的稳定版本,因此我们将从添加CRAN维护的外部存储库开始。
Note: CRAN maintains the repositories within their network, but not all external repositories are reliable. Be sure to install only from trusted sources.
注意: CRAN在其网络内维护存储库,但并非所有外部存储库都是可靠的。 确保仅从受信任的来源进行安装。
Let’s first add the relevant GPG key.
首先添加相关的GPG密钥。
When we run the command, we’ll receive the following output:
运行命令时,将收到以下输出:
Output
Executing: /tmp/apt-key-gpghome.4BZzh1TALq/gpg.1.sh --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
gpg: key 51716619E084DAB9: public key "Michael Rutter " imported
gpg: Total number processed: 1
gpg: imported: 1
Once we have the trusted key, we can add the repository.
一旦有了可信密钥,就可以添加存储库。
Note that if you’re not using 18.04, you can find the relevant repository from the R Project Ubuntu list, named for each release. Ubuntu 18.04 is named Bionic Beaver, and the most recent version of R is 4.0, hence the naming convention of ubuntu bionic-cran40
.
请注意,如果您不使用18.04,则可以从R Project Ubuntu列表 (为每个版本命名)中找到相关的存储库。 Ubuntu 18.04名为Bionic Beaver,R的最新版本是4.0,因此是ubuntu bionic-cran40
的命名约定。
Among the output that displays, you should identify lines similar to the following:
在显示的输出中,您应该标识类似于以下内容的行:
Output
...
Get:4 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease [3626 B]
...
Get:22 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ Packages [20.4 kB]
...
Now, we’ll need to run update
after this in order to include package manifests from the new repository.
现在,我们需要在此之后运行update
,以便包括来自新存储库的软件包清单。
Among the output should be a line similar to the following:
在输出中应该有一条类似于以下内容的行:
Output
...
Hit:1 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease
...
If the line above appears in the output from the update
command, we’ve successfully added the repository. We can be sure we won’t accidentally install an older version.
如果上面的行出现在update
命令的输出中,则说明我们已经成功添加了存储库。 我们可以确定不会意外安装旧版本。
At this point, we’re ready to install R with the following command.
至此,我们准备使用以下命令安装R。
If prompted to confirm installation, press y
to continue.
如果提示您确认安装,请按y
继续。
As of the time of writing, the latest stable version of R from CRAN is 4.0.1, which is displayed when you start R.
截至撰写本文时,CRAN的R的最新稳定版本是4.0.1,在您启动R时会显示该版本。
Since we’re planning to install an example package for every user on the system, we’ll start R as root so that the libraries will be available to all users automatically. Alternatively, if you run the R
command without sudo
, a personal library can be set up for your user.
由于我们计划为系统上的每个用户安装一个示例程序包,因此我们将以root身份启动R,以便所有用户可以自动使用这些库。 另外,如果您在不使用sudo
情况下运行R
命令,则可以为您的用户设置一个个人库。
Output
R version 4.0.0 (2020-04-24) -- "Arbor Day"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
...
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.
>
This confirms that we’ve successfully installed R and entered its interactive shell.
这确认我们已经成功安装了R并进入了它的交互式外壳。
Part of R’s strength is its available abundance of add-on packages. For demonstration purposes, we’ll install txtplot
, a library that outputs ASCII graphs that include scatterplot, line plot, density plot, acf and bar charts:
R的部分优势在于其丰富的附加软件包。 为了演示起见,我们将安装txtplot
,该库输出ASCII图,包括散点图,折线图,密度图,acf和条形图:
Note: The following output shows where the package will be installed.
注意:以下输出显示了软件包的安装位置。
Output
...
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
. . .
This site-wide path is available because we ran R as root. This is the correct location to make the package available to all users.
该站点范围的路径可用,因为我们以root身份运行R。 这是使软件包对所有用户可用的正确位置。
When the installation is complete, we can load txtplot
:
安装完成后,我们可以加载txtplot
:
If there are no error messages, the library has successfully loaded. Let’s put it in action now with an example which demonstrates a basic plotting function with axis labels. The example data, supplied by R’s datasets
package, contains the speed of cars and the distance required to stop based on data from the 1920s:
如果没有错误消息,则说明库已成功加载。 现在,通过一个示例来将其付诸实践,该示例演示带有轴标签的基本绘图功能。 由R的datasets
软件包提供的示例数据包含汽车的速度和基于1920年代的数据所需的停车距离 :
Output
+----+-----------+------------+-----------+-----------+--+
120 + * +
| |
d 100 + * +
i | * * |
s 80 + * * +
t | * * * * |
a 60 + * * * * * +
n | * * * * * |
c 40 + * * * * * * * +
e | * * * * * * * |
20 + * * * * * +
| * * * |
0 +----+-----------+------------+-----------+-----------+--+
5 10 15 20 25
speed
If you are interested to learn more about txtplot
, use help(txtplot)
from within the R interpreter.
如果您想了解有关txtplot
更多信息,请在R解释器中使用help(txtplot)
。
Any precompiled package can be installed from CRAN with install.packages()
. To learn more about what’s available, you can find a listing of official packages organized by name via the Available CRAN Packages By Name list.
可以使用install.packages()
从CRAN安装任何预编译的软件包。 要了解更多有关可用软件包的信息,您可以通过“ 可用的CRAN按名称列出软件包”列表找到按名称组织的正式软件包列表 。
To exit R, you can type q()
. Unless you want to save the workspace image, you can press n
when prompted.
要退出R,可以键入q()
。 除非要保存工作区图像,否则在出现提示时可以按n
。
With R successfully installed on your server, you may be interested in this guide on installing the RStudio Server to bring an IDE to the server-based deployment you just completed. You can also learn how to set up a Shiny server to convert your R code into interactive web pages.
在服务器上成功安装R后,您可能会对本指南的安装RStudio服务器感兴趣,以将IDE带入刚刚完成的基于服务器的部署中。 您还可以学习如何设置Shiny服务器,以将R代码转换为交互式网页。
For more information on how to install R packages by leveraging different tools, you can read about how to install directly from GitHub, BitBucket or other locations. This will allow you to take advantage of the very latest work from the active community.
有关如何利用各种工具安装R软件包的更多信息,您可以阅读有关如何直接从GitHub,BitBucket或其他位置进行安装的信息 。 这将使您能够利用活跃社区中的最新作品。
翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-r-on-ubuntu-18-04