博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在Ubuntu 20.04上安装R
阅读量:2507 次
发布时间:2019-05-11

本文共 8174 字,大约阅读时间需要 27 分钟。

介绍 (Introduction)

An open-source programming language, R is widely used for performing data analysis and statistical computing. Supported by the R Foundation for Statistical Computing, it is 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 .

在本教程中,我们将安装R并演示如何从官方的添加软件包。

先决条件 (Prerequisites)

To follow along with this tutorial, you will need an Ubuntu 20.04 server with:

要继续学习本教程,您将需要具有以下功能的Ubuntu 20.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 .

要了解如何实现此设置,请遵循我们的 。

Once these prerequisites are in place, you’re ready to begin.

一旦满足这些先决条件,就可以开始了。

第1步-安装R (Step 1 — Installing R)

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密钥。

  • sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

When we run the command, we’ll receive the following output:

运行命令时,将收到以下输出:

Output   
Executing: /tmp/apt-key-gpghome.cul0ddtmN1/gpg.1.sh --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9gpg: key 51716619E084DAB9: public key "Michael Rutter
" importedgpg: Total number processed: 1gpg: imported: 1

Once we have the trusted key, we can add the repository.

一旦有了可信密钥,就可以添加存储库。

Note that if you’re not using 20.04, you can find the relevant repository from the , named for each release. Ubuntu 20.04 is referred to as Focal Fossa, and the most recent version of R is 4.0.0, hence the naming convention of the repository below — focal-cran40.

请注意,如果您未使用20.04,则可以从 (为每个版本命名)中找到相关的存储库。 Ubuntu 20.04被称为Focal Fossa,R的最新版本是4.0.0,因此下面存储库的命名约定为focal-cran40

  • sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'

    sudo add-apt-repository'deb https://cloud.r-project.org/bin/linux/ubuntu focus-cran40 /'

Among the output that displays, you should identify lines similar to the following:

在显示的输出中,您应该标识类似于以下内容的行:

Output   
...Get:7 https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/ InRelease [3622 B] Get:8 https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/ Packages [15.6 kB]...

Now, we’ll need to run update after this in order to include package manifests from the new repository.

现在,我们需要在此之后运行update ,以便包括来自新存储库的软件包清单。

  • sudo apt update

    sudo apt更新

Ensure that one of the lines of output is similar to the following:

确保输出行之一类似于以下内容:

Output   
...Hit:5 https://cloud.r-project.org/bin/linux/ubuntu focal-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。

  • sudo apt install r-base

    sudo apt安装r-base

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命令,则可以为您的用户设置一个个人库。

  • sudo -i R

    须藤-i R
Output   
R version 4.0.0 (2020-04-24) -- "Arbor Day"Copyright (C) 2020 The R Foundation for Statistical ComputingPlatform: 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并进入了它的交互式外壳。

步骤2 —从CRAN安装R软件包 (Step 2 — Installing R Packages from CRAN)

Part of R’s strength is its available abundance of add-on packages. For demonstration purposes, we’ll install , a library that outputs ASCII graphs that include scatterplot, line plot, density plot, acf and bar charts:

R的部分优势在于其丰富的附加软件包。 为了演示起见,我们将安装 ,该库输出ASCII图,包括散点图,折线图,密度图,acf和条形图:

  • install.packages('txtplot')

    install.packages('txtplot')

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 location will make the package available to all users.

该站点范围的路径可用,因为我们以root身份运行R。 此位置将使软件包对所有用户可用。

When the installation is complete, we can load txtplot:

安装完成后,我们可以加载txtplot

  • library('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 :

如果没有错误消息,则说明库已成功加载。 现在,通过一个示例来将其付诸实践,该示例演示带有轴标签的基本绘图功能。 由R的datasets软件包提供的示例数据包含 :

  • txtplot(cars[,1], cars[,2], xlab = 'speed', ylab = 'distance')

    txtplot(汽车[,1],汽车[,2],xlab ='速度',ylab ='距离')
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 .

可以使用install.packages()从CRAN安装任何预编译的软件包。 要了解更多有关可用软件包的信息,您可以通过“ 找到按名称组织的正式软件包 。

To exit R, you can type q(). You can press n when prompted, unless you want to save the workspace image.

要退出R,可以键入q() 。 出现提示时可以按n ,除非您要保存工作区图像。

结论 (Conclusion)

With R successfully installed on your server, you may be interested in this guide on to bring an IDE to the server-based deployment you just completed. You can also learn how to set up a to convert your R code into interactive web pages.

在服务器上成功安装R后,您可能会对本指南的感兴趣,以将IDE带入刚刚完成的基于服务器的部署中。 您还可以学习如何设置以将R代码转换为交互式网页。

For more information on how to install R packages by leveraging different tools, you can read about how to . This will allow you to take advantage of the very latest work from the active community.

有关如何利用各种工具安装R软件包的更多信息,您可以阅读有关如何进行 。 这将使您能够利用活跃社区中的最新作品。

翻译自:

转载地址:http://mphgb.baihongyu.com/

你可能感兴趣的文章
python学习笔记之多个装饰器
查看>>
自己创建一个android studio在线依赖compile
查看>>
【android】安卓开发apk列表
查看>>
JavaScript高级特性-实现继承的七种方式
查看>>
20121016学习笔记四
查看>>
EntityFramework 学习 一 Stored Procedure
查看>>
Sliverlight之 故事板
查看>>
Java 必知必会的 20 种常用类库和 API
查看>>
HDU 1087 Super Jumping! Jumping! Jumping!
查看>>
0007_初始模块和字节码
查看>>
[效率提升]如何管理好你的电脑文件
查看>>
C++实验二
查看>>
Sultan's Dowry Problem - 苏丹新娘问题
查看>>
SharePoint2010 富文本框添加图片功能的扩展
查看>>
零零碎碎的知识
查看>>
UNIX基础--用户和基本账户管理
查看>>
设计模式
查看>>
5.0以上机器XPOSED框架安装流程
查看>>
静态方法与非静态方法
查看>>
[转]iOS进阶路线以及进阶书籍
查看>>