在 Debian 10 上安装 R 教程

R 是一种开源编程语言和免费环境,专门从事统计计算和图形表示。它由 R 统计计算基金会支持,主要由统计学家和数据挖掘人员用于开发统计软件和执行数据分析。

本文讲述在 Debian 10 上安装 R 的过程。

先决条件

在继续本教程之前,请确保满足以下先决条件:

在 Debian 上安装 R

来自 Debian 仓库的R软件包通常是过时的。我们将从维护库 CRAN 安装 R。

要在 Debian 10 上安装 R,请按照下列步骤操作:

以下就是如何在 Debian 10 上安装 R 的最新稳定版本的详细步骤:

  1. 通过 HTTPS 安装添加新存储库所需的软件包:

    sudo apt install dirmngr apt-transport-https ca-certificates software-properties-common gnupg2
    
  2. 运行以下命令以启用 CRAN 存储库并将 CRAN GPG 密钥添加到系统中:

    sudo apt-key adv --keyserver keys.gnupg.net --recv-key 'E19F5F87128899B192B1A2C2AD5F960A256A04AF'
    sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/debian buster-cran35/'
    
  3. 更新软件包列表并安装 R 软件包:

    sudo apt update
    sudo apt install r-base
    
  4. 通过输出 R 版本来验证安装:

    R --version
    

    在撰写本文时,R的最新稳定版本是版本 3.6.3 :

    R version 3.6.3 (2020-02-29) -- " Holding the Windsock "
    Copyright (C) 2020 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 the terms of the
    GNU General Public License versions 2 or 3.
    For more information about these matters see
    https://www.gnu.org/licenses/.
    

从 CRAN 安装R软件包

R 之所以如此受欢迎的主要原因之一是可以通过综合 R 存档网络 (CRAN) 获得大量的软件包。

请先安装 build-essential , 它包含了编译 R 软件包所需工具的软件包:

sudo apt install build-essential

如果 R 执行文件是作为 root 或 sudo 启动的,则程序包将全局安装并供所有系统用户使用。要为您的用户设置个人库,请以常规用户身份调用二进制文件。

举例来说,我们将安装一个名为 stringr 的包,该包提供了常见的字符串操作。

以超级用户身份打开R控制台:

sudo -i R
R version 3.5.1 (2018-07-02) -- "Feather Spray"
Copyright (C) 2018 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 控制台中执行。

安装 stringr 软件包:

install.packages("stringr")

安装将花费一些时间。完成后,加载库:

library(stringr)

创建一个简单的字符向量,名为 tutorial

tutorial <- c("How", "to", "Install", "R", "on", "Debian", "9")

运行以下函数,该函数将打印字符串的长度:

str_length(tutorial)
[1] 3 2 7 1 2 6 1

您可以在 CRAN 软件包页面中找到更多 R 软件包,并使用 install.packages() 进行安装。

结论

我们已经向您展示了如何在 Debian 10 上安装 R 以及如何安装 R 相关的工具软件包。