如何在Debian 10上安装R

时间:2020-03-05 15:31:31  来源:igfitidea点击:

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

本文提供有关如何在Debian 10上安装R的信息。

准备工作

在继续本教程之前,请确保满足以下准备工作:

  • 具有至少1G 内存的Debian 10系统。如果计算机的内存少于1GB,则可以创建一个交换文件。
  • 我们以具有sudo特权的用户身份登录。

在Debian上安装R

Debian仓库中的R软件包通常已经过时了。
我们将从CRAN维护的存储库中安装R。

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

以下步骤描述了如何在Debian 10上安装R的最新稳定版本:

  • 安装通过HTTPS添加新存储库所需的软件包:
sudo apt install dirmngr apt-transport-https ca-certificates software-properties-common gnupg2
  • 运行以下命令以启用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/'
  • 更新软件包列表并安装R软件包:
sudo apt updatesudo apt install r-base
  • 通过打印R版本来验证安装:
R --version

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

R version 3.6.3 (2017-02-29) -- "Holding the Windsock"
Copyright (C) 2017 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

如果以root或者sudo身份启动“ R”二进制文件,则软件包将全局安装并可供所有系统用户使用。
要为用户设置个人库,请以普通用户身份调用二进制文件。

作为示例,我们将安装一个名为'stringr'的软件包,该软件包可快速,正确地实现常见的字符串操作。

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

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

安装'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()'安装它们。