在 Ubuntu 20.04 上安装 R 的最佳方式?

Best way to install R on Ubuntu 20.04?

有没有人有在 Ubuntu 20.04 上安装 R 的好方法?我似乎无法通过 apt 找到特定于 20.04 focal 的解决方案。

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04 LTS
Release:    20.04
Codename:   focal

编辑:在完成 link here 并将条目添加到 sources.list 之后,我 运行 遇到依赖性问题:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 r-base : Depends: r-base-core (>= 4.0.2-1.2004.0) but it is not going to be installed
          Depends: r-recommended (= 4.0.2-1.2004.0) but it is not going to be installed
          Recommends: r-base-html but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

根据@DirkEddelbuettel 的评论,我 运行 sudo apt install r-base-core r-recommended r-base-html 查看下一级依赖项:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 r-base-core : Depends: libblas3 but it is not installable or
                        libblas.so.3 but it is not installable
               Depends: liblapack3 but it is not installable or
                        liblapack.so.3 but it is not installable
               Depends: libtcl8.6 (>= 8.6.0) but it is not installable
               Depends: libtk8.6 (>= 8.6.0) but it is not installable
               Recommends: r-base-dev but it is not going to be installed
               Recommends: r-doc-html but it is not going to be installed
 r-recommended : Depends: r-cran-kernsmooth (>= 2.2.14) but it is not going to be installed
                 Depends: r-cran-mgcv (>= 1.1.5) but it is not going to be installed
                 Depends: r-cran-rpart (>= 3.1.20) but it is not going to be installed
                 Depends: r-cran-survival (>= 2.13.2-1) but it is not going to be installed
                 Depends: r-cran-matrix but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

我在尝试安装 r-base 时遇到了一个更根本的问题。我最终发现在添加 PPA 后我遇到了未满足的依赖项问题。我用这个 link here 来修复未满足的依赖关系的潜在问题,这反过来又让我可以进行 apt 安装并且没有任何安装问题。

这是我在 Rocker container r-ubuntu for the 20.04 image 中所做的:

  1. 安装software-properties-common可以说add-apt-repository

  2. 为 R 本身添加 rrutter4.0 PPA(与 CRAN 相同)
    add-apt-repository --enable-source --yes "ppa:marutter/rrutter4.0"

  3. 为超过 4k 的 CRAN 包添加 c4d4u.teams 回购:
    add-apt-repository --enable-source --yes "ppa:c2d4u.team/c2d4u4.0+"

  4. 运行 apt install r-base(还有一些)。

从狭义上讲,您只需要 2 个(因为在完整的 Ubuntu 系统上您可能不需要 1 个)和 4 个,但您也可以做 3 个。

你当然也可以 docker pull rocker/r-ubuntu:20.04 并获得预制的容器,但我同时使用:用于测试的容器,以及我 20.04 机器上的这些设置。

先决条件

您将需要一个 Ubuntu 20.04:

至少 1GB 内存 具有 sudo 权限的 root 用户/非 root 用户

正在安装 R

由于 R 是一个快速发展的项目,Ubuntu 的存储库并不总是提供最新的稳定版本,因此我们将从添加由 CRAN 维护的外部存储库开始。

注意:CRAN 在其网络内维护存储库,但并非所有外部存储库都是可靠的。请务必仅从受信任的来源安装。

我们先添加相关的GPG密钥。

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

请注意,如果您不使用 20.04,您可以从 R 项目 Ubuntu 列表中找到相关的存储库,以每个版本命名。 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 apt update

此时,我们已准备好使用以下命令安装 R。

sudo apt install r-base

如果提示确认安装,请按 y 继续。

终于进入R shell-

sudo -i R

这确认我们已经成功安装 R 并进入其交互 shell。